Download presentation
Presentation is loading. Please wait.
1
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps
2
2 Our Binary Tree Defined recursively as consisting of –Data (in this example: int and String, but can be anything!) –A lefthand Binary Tree –A righthand Binary Tree
3
3 Simple Binary Tree Code
4
4
5
5
6
6 Depth-first Search From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html Full tree DFS (to see it, run in Presentation Mode) Process first all nodes at the deepest level of the tree
7
7 Depth First Search val
8
8 Breadth-first Search From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html Full tree BFS (to see it, run in Presentation Mode) Process all nodes at depth 1, then all the nodes at depth 2, and so on..
9
9 Breadth First Traversal We can reuse our Queue!!
10
10 Binary Search Trees Differs from Binary trees in that –Each internal node has a key –The key is used to give an order to the nodes –Nodes often store data But sometimes stored only in leaf (B+Trees) –The keys are stored in order such that Keys to the left of a node are less than keys to the right of that node –More formally, For each internal node v with key k –Keys stored in nodes in the left subtree of v are <= k –Keys stored in nodes in the right subtree are >= k
11
11 Searching in a Binary Search Tree What is the running time? –Equivalent to the height of the tree O(h) –This can be O(n) in an unrestricted tree –We have ways to maintain the shape of the tree to make it O(log n) (AVL trees)
12
12 Updating the Binary Tree Inserting is pretty straightforward –Add node at the “bottom” of the tree –O(h) Deleting gets more complex –If the node w to be removed has two internal nodes below it, Find the first node that follows w via an inorder travesal This is y, the leftmost internal node in the right subtree of w Move the item stored at y into w’s position Remove the now orphaned leaf that had been below y as well as y –O(h) Nice applet: –http://www.seanet.com/users/arsen/avltree.htmlhttp://www.seanet.com/users/arsen/avltree.html
13
13 Slide copyright 1999 Addison Wesley Longman A heap is a certain kind of complete binary tree. When a complete binary tree is built, its first node must be the root. When a complete binary tree is built, its first node must be the root. Root Heaps
14
14 Slide copyright 1999 Addison Wesley Longman Left child of the root The second node is always the left child of the root. The second node is always the left child of the root.
15
15 Slide copyright 1999 Addison Wesley Longman Right child of the root The third node is always the right child of the root. The third node is always the right child of the root.
16
16 Slide copyright 1999 Addison Wesley Longman The next nodes always fill the next. level from left-to-right. The next nodes always fill the next. level from left-to-right.
17
17 Slide copyright 1999 Addison Wesley Longman The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.
18
18 Slide copyright 1999 Addison Wesley Longman The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.
19
19 Slide copyright 1999 Addison Wesley Longman The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.
20
20 Slide copyright 1999 Addison Wesley Longman
21
21 Slide copyright 1999 Addison Wesley Longman Each node in a heap contains a key that can be compared to other nodes' keys. Each node in a heap contains a key that can be compared to other nodes' keys. 19 4222127 23 45 35
22
22 Slide copyright 1999 Addison Wesley Longman The "heap property" requires that each node's key is >= the keys of its children The "heap property" requires that each node's key is >= the keys of its children 19 4222127 23 45 35
23
23 Slide copyright 1999 Addison Wesley Longman ¶Put the new node in the next available spot. ·Move the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 4222127 23 45 35 42 Adding a Node to a Heap
24
24 Slide copyright 1999 Addison Wesley Longman 19 4222142 23 45 35 27
25
25 Slide copyright 1999 Addison Wesley Longman 19 4222135 23 45 42 27
26
26 Slide copyright 1999 Addison Wesley Longman 4The parent has a key that is >= new node, or 4The node reaches the root. ÚThe process of moving the new node upward is called reheapification upward. 19 4222135 23 45 42 27
27
Slide copyright 1999 Addison Wesley Longman ¶Move the last node onto the root. 19 4222135 23 45 42 27 Removing the Top of the Heap
28
28 Slide copyright 1999 Addison Wesley Longman 19 4222135 23 27 42
29
29 Slide copyright 1999 Addison Wesley Longman ¶Move the last node onto the root. ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4222135 23 27 42
30
30 Slide copyright 1999 Addison Wesley Longman ¶Move the last node onto the root. ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4222135 23 42 27
31
31 Slide copyright 1999 Addison Wesley Longman ¶Move the last node onto the root. ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4222127 23 42 35
32
32 Slide copyright 1999 Addison Wesley Longman 4The children all have keys <= the out-of- place node, or 4The node reaches the leaf. ØThe process of pushing the new node downward is called reheapification downward. 19 4222127 23 42 35
33
33 Slide copyright 1999 Addison Wesley Longman Store the data from the nodes in a partially-filled array. An array of data 2127 23 42 35 Implementing a Heap
34
34 Slide copyright 1999 Addison Wesley Longman Data from the root goes in the first location of the array. An array of data 2127 23 42 35 42
35
35 Slide copyright 1999 Addison Wesley Longman Data from the next row goes in the next two array locations. An array of data 2127 23 42 35 423523
36
36 Slide copyright 1999 Addison Wesley Longman Data from the next row goes in the next two array locations. An array of data 2127 23 42 35 423523 2721
37
37 Slide copyright 1999 Addison Wesley Longman Data from the next row goes in the next two array locations. An array of data 2127 23 42 35 423523 2721 We don't care what's in this part of the array.
38
38 Slide copyright 1999 Addison Wesley Longman The links between the tree's nodes are not actually stored as pointers, or in any other way. The only way we "know" that "the array is a tree" is from the way we manipulate the data. An array of data 2127 23 42 35 423523 2721
39
39 Slide copyright 1999 Addison Wesley Longman If you know the index of a node, then it is easy to figure out the indexes of that node's parent and children. Formulas are given in the book. [0] [0] [1] [2] [3] [4] 2127 23 42 35 423523 2721
40
40 The Worst Pun in CS: Heap, Array!!! © Marti Hearst, 1984
41
41 Next Time: Hash Tables
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.