CE 221 Data Structures and Algorithms
Chapter 4: Trees (BST)
Text: Read Weiss, §4.3
1 Izmir University of Economics
CE 221 Data Structures and Algorithms Chapter 4: Trees (BST) Text: - - PowerPoint PPT Presentation
CE 221 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, 4.3 Izmir University of Economics 1 The Search Tree ADT Binary Search Trees An important application of binary trees is in searching. Let us assume that
1 Izmir University of Economics
2 Izmir University of Economics
assume that each node in the tree stores an item. Assume for simplicity that these are distinct integers (deal with duplicates later).
tree is that for every node, X, in the tree, the values of all the items in the left subtree are smaller than the item in X, and the values of items in the right subtree are larger than the item in X.
The tree on the left is a binary search tree, but the tree on the right is not. The tree on the right has a node with key 7 in the left subtree of a node with key 6 (which happens to be the root).
Izmir University of Economics 3
Izmir University of Economics 4
Izmir University of Economics 5 Previous slide
Izmir University of Economics 6
46 }
Izmir University of Economics 7
false if there is no such node. The structure of the tree makes this simple. If t is NULL , then we can just return . Otherwise, we make a recursive call on either the left or the right subtree of the node referenced by t.
Izmir University of Economics 8
left as long as there is a left child. The stopping point is the smallest element.
branching is to the right child.
is carefully handled.
findMax, since we are only working with a
because a statement such as t.right=t.right.right will make changes.
Izmir University of Economics 9
The insertion routine is conceptually simple. To insert x into tree t, proceed down the tree as you would with a contains. If x is found, do nothing (or "update" something). Otherwise, insert x at the last spot on the path traversed. Duplicates can be handled by keeping an extra field in the node indicating the frequency of occurrence. If the key is only part of a larger record, then all of the records with the same key might be kept in an auxiliary data structure, such as a list or another search tree.
Insert node 5
Izmir University of Economics 10
Izmir University of Economics 11
consider 3 possibilities. (1) If the node is a leaf, it can be deleted immediately. (2) If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node. Notice that the deleted node is now unreferenced and can be disposed of
Delete node 4
Izmir University of Economics 12
(3) The complicated case deals with a node with two
node with the smallest key of the right subtree (easy) and recursively delete that node (which is now empty). Because the smallest node in the right subtree cannot have a left child, the second delete is an easy one.
Delete node 2
13 Izmir University of Economics
Inefficient, since calls highlighted in yellow result in two passes down the tree to find and delete the smallest node in the right subtree.
Izmir University of Economics 14
else if (t.left != null && t.right != null){ BinaryNode<AnyType> tmp,prev; /* declare references */ tmp = t.right; /* point to smallest in the right */ prev = t.right; /* point to parent of tmp */ while (tmp.left != null){ /* find smallest of right */ prev = tmp; tmp = tmp.left; } t.element = tmp.element; /* replace with smallest */ if (tmp == prev) /* t.right is smallest */ t.right = tmp.right; /* skip over tmp */ else /* connect left of prev to right of tmp */ prev.left = tmp.right; } ... Efficient Version
Izmir University of Economics 15
Izmir University of Economics 16
time where d is the depth of the node containing the accessed
tree, thus operating on a tree that is now roughly half as large.
O(log N) assuming all insertion sequences are equally likely.
internal path length. Let’s calculate the average internal path length over all possible insertion sequences.
17 Izmir University of Economics
does not hold for binary trees though. Let’s, then, average: 1 ) ( ) / 2 ( ) ( ), 1 ) 1 ( ) ( ( ) / 1 ( ) (
1 1
N i D N N D N i i N i N D i D N N D
N i N i
expected depth of any node is O(log N).
Izmir University of Economics 18
1 ) ( ) / 2 ( ) ( ), 1 ) 1 ( ) ( ( ) / 1 ( ) (
1 1
N i D N N D N i i N i N D i D N N D
N i N i
) 1 ( 2 ) 1 ( 2 ) 1 ( ) 1 ( ) ( ) 2 )...( 2 )( 1 ( ) ( 2 ) 1 ( ) 1 ( ) 1 ..( )......... 1 ( ) ( 2 ) (
2 1
N N D N D N N ND N N i D N D N N N i D N ND
N i N i
N i
i i i D N N D D D N N N N N D N N D N N N N N D N N D N N D N N ND
2
) 1 ( 1 2 2 / ) 1 ( ) 1 /( ) ( 3 * 2 1 * 2 2 / ) 1 ( 3 / ) 2 ( ... ) 1 ( ) 2 ( 2 ) 1 /( ) 2 ( / ) 1 ( ) 1 ( ) 1 ( 2 / ) 1 ( ) 1 /( ) ( ) 1 ( 2 ) 1 ( ) 1 ( ) ( ...(sum the equations side by side) .....(divide by N(N+1)) .....(subtract (2) from (1))
Izmir University of Economics 19
N i N i N i
i i i N N D i i i D N N D
2 2 2
) 1 ( 1 2 1 1 2 ) 1 /( ) ( ) 1 ( 1 2 2 / ) 1 ( ) 1 /( ) (
N i
i i N N N N D
2
) 1 1 1 ( 2 )) 1 /( 1 / 1 ... 4 / 1 3 / 1 ( 2 ) 1 /( ) ( ) 1 /( 2 1 ) 1 /( 2 ) 2 / 3 (log 2 ) 1 /( ) ( )) 1 /( 1 2 / 1 ( 2 )) 1 /( 1 ) 2 / 3 ((log 2 ) 1 /( ) ( )) 1 /( 1 2 / 1 ( 2 )) 1 /( 1 ) / 1 ... 4 / 1 3 / 1 (( 2 ) 1 /( ) ( N N N N N D N N N N N D N N N N N D
e e
4 ) 1 ( 2 ) 1 ( 4 log ) 1 ( 2 ) ( ) 1 /( 4 2 4 log 2 ) 1 /( ) ( N N N N N D N N N N D
e e
) log ( ) ( ) log log ( ) ( ) log ( ) ( 4 ) 1 ( 2 ) 1 ( 4 log ) 1 ( 2 ) (
2 2
N N O N D e N N O N D N N O N D N N N N N D
e e
20 Izmir University of Economics
nodes at expected depth 9.98.
Izmir University of Economics 21
) ( N
After a quarter-million random insert/remove pairs, right-heavy tree
unbalanced and average depth becomes 12.51.
deeper than the right (a deleted node is replaced with a node from the right). The exact effect of this still unknown, but if insertions and deletions are alternated Ɵ(N2) times, expected depth is .In the absence of deletions or when lazy deletion is used; average running times for BST
Izmir University of Economics 22