1 foundations of software design fall 2002 marti hearst lecture 17: binary search trees; heaps
Post on 22-Dec-2015
215 views
TRANSCRIPT
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
6
Depth-first Search
From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html
Full tree
DFS (to see it, run inPresentationMode)
Process first all nodes at the deepest levelof the tree
8
Breadth-first Search
From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html
Full tree
BFS (to see it, run inPresentationMode)
Process all nodes atdepth 1, then all thenodes at depth 2, and so on..
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
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
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.html
13Slide copyright 1999 Addison Wesley Longman
A heap is a certain kind of complete binary tree.
When a completebinary tree is built,
its first node must bethe root.
When a completebinary tree is built,
its first node must bethe root.
RootHeaps
14Slide copyright 1999 Addison Wesley Longman
Left childof theroot
The second node isalways the left child
of the root.
The second node isalways the left child
of the root.
15Slide copyright 1999 Addison Wesley Longman
Right childof the
root
The third node isalways the right child
of the root.
The third node isalways the right child
of the root.
16Slide copyright 1999 Addison Wesley Longman
The next nodesalways fill the next
level from left-to-right..
The next nodesalways fill the next
level from left-to-right..
17Slide copyright 1999 Addison Wesley Longman
The next nodesalways fill the next
level from left-to-right.
The next nodesalways fill the next
level from left-to-right.
18Slide copyright 1999 Addison Wesley Longman
The next nodesalways fill the next
level from left-to-right.
The next nodesalways fill the next
level from left-to-right.
19Slide copyright 1999 Addison Wesley Longman
The next nodesalways fill the next
level from left-to-right.
The next nodesalways fill the next
level from left-to-right.
21Slide copyright 1999 Addison Wesley Longman
Each node in a heapcontains a key thatcan be compared toother nodes' keys.
Each node in a heapcontains a key thatcan be compared toother nodes' keys.
19
4222127
23
45
35
22Slide copyright 1999 Addison Wesley Longman
The "heap property"requires that each
node's key is >= thekeys of its children
The "heap property"requires that each
node's key is >= thekeys of its children
19
4222127
23
45
35
23Slide 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
26Slide copyright 1999 Addison Wesley Longman
The parent has a key that is >= new node, or
The node reaches the root.
The process of moving the new node upward is called reheapification upward.
19
4222135
23
45
42
27
27Slide copyright 1999 Addison Wesley Longman
Move the last node onto the root.
19
4222135
23
45
42
27
Removing the Top of the Heap
29Slide 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
30Slide 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
31Slide 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
32Slide copyright 1999 Addison Wesley Longman
The children all have keys <= the out-of-place node, or
The node reaches the leaf.
The process of pushing the new node downward is called reheapification downward.
19
4222127
23
42
35
33Slide copyright 1999 Addison Wesley Longman
• Store the data from the nodes in a partially-filled array.
An array of dataAn array of data
2127
23
42
35
Implementing a Heap
34Slide copyright 1999 Addison Wesley Longman
Data from the root goes in the first location of the array.
An array of dataAn array of data
2127
23
42
35
42
35Slide copyright 1999 Addison Wesley Longman
Data from the next row goes in the next two array locations.
An array of dataAn array of data
2127
23
42
35
42 35 23
36Slide copyright 1999 Addison Wesley Longman
Data from the next row goes in the next two array locations.
An array of dataAn array of data
2127
23
42
35
42 35 23 27 21
37Slide copyright 1999 Addison Wesley Longman
Data from the next row goes in the next two array locations.
An array of dataAn array of data
2127
23
42
35
42 35 23 27 21
We don't care what's inWe don't care what's inthis part of the array.this part of the array.
38Slide 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 dataAn array of data
2127
23
42
35
42 35 23 27 21
39Slide 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
42 35 23 27 21