Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -

Similar presentations


Presentation on theme: "CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -"— Presentation transcript:

1 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Binary Trees with additional structure. BST ADT Performance AVL Trees CSE 373, Copyright S. Tanimoto, Binary Search Trees -

2 (recall:) The Dictionary ADT
put(x, k) : Insert data element x with key k. get(k) : find and return the data element having key k. CSE 373, Copyright S. Tanimoto, Binary Search Trees -

3 Binary Search Trees: Motivation
We wish to implement the Dictionary ADT with the additional method “getAll” (return all data elements in ascending order of their keys). Keys are now assumed to be ordered (“comparable”). Hashing with linear open addressing requires (D + n log n) for getAll, whereas BSTs can do it in O(n). D = size of hash table. n = number of data elements. CSE 373, Copyright S. Tanimoto, Binary Search Trees -

4 Binary Search Trees: Definition
A Binary Search Tree (or BST, for short) is a binary tree with the following added requirements: Each node stores a data element e and a key k. The keys come from a totally ordered set (e.g., integers, or strings with lexicographic ordering). For each node, any and all keys in its left subtree are less than its own key, and any and all keys in its right subtree are greater than its own. (We typically assume that all the keys in a BST are distinct. If not, we should call the structure a BST with duplicates.) CSE 373, Copyright S. Tanimoto, Binary Search Trees -

5 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
BST Example 9.4 Sue Dow 3.7 John Smith 20.2 Chi Wang 3.2 Zoe Zebow 7.0 Mary Ives 21.0 Amy Berg CSE 373, Copyright S. Tanimoto, Binary Search Trees -

6 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Performance Put: Worst case O(n), expected case if tree is random O(log n) Get: same as Put. getAll: O(n). CSE 373, Copyright S. Tanimoto, Binary Search Trees -

7 AVL Trees invented by Adelson-Velskii and Landis.
An AVL tree is a binary tree such that: if it’s non-empty then its subtrees TL and TR satisfy | height(TL) - height(TR) |  and TL and TR are also AVL trees. An AVL search tree is an AVL tree that is also a BST CSE 373, Copyright S. Tanimoto, Binary Search Trees -

8 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Example AVL Tree 1 1 CSE 373, Copyright S. Tanimoto, Binary Search Trees -

9 Negative Example AVL Tree
1 1 CSE 373, Copyright S. Tanimoto, Binary Search Trees -

10 Example AVL Search Tree
F B 1 D G 1 E CSE 373, Copyright S. Tanimoto, Binary Search Trees -

11 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AVL Tree: Lookup cost Time to perform FIND(k) in AVL tree T is proportional to the depth of the node containing key k. Depth(k)  height(T). height(T)  c log2 n where c  (see Sahni, p.605). Therefore, the time for FIND(k) is O(log n) in both the worst case and expected case. The worst case time for hashing is (D + n) although the expected case time when the load factor is low is O(1). CSE 373, Copyright S. Tanimoto, Binary Search Trees -

12 AVL (Search) Tree (Easy) Insertion Example
F B 1 D G CAT 1 E CSE 373, Copyright S. Tanimoto, Binary Search Trees -

13 AVL (Search) Tree (Harder) Insertion Example
F Adding “BAD” causes A to go out of balance B 1 D G BAD CAT 1 E CSE 373, Copyright S. Tanimoto, Binary Search Trees -

14 AVL Tree Single Rotation Left
C B F A BAD 1 D G CAT 1 E CSE 373, Copyright S. Tanimoto, Binary Search Trees -

15 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Now insert EGG C B F A BAD 1 D G CAT E 1 EGG 1 CSE 373, Copyright S. Tanimoto, Binary Search Trees -

16 Prepare for Left rotation
C B F A BAD 1 D G CAT E 1 EGG 1 CSE 373, Copyright S. Tanimoto, Binary Search Trees -

17 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Left Rotation Done C B F A BAD 1 1 E G D EGG 1 CAT CSE 373, Copyright S. Tanimoto, Binary Search Trees -

18 Prepare for Right Rotation
C B F A BAD 1 1 E G D EGG 1 CAT CSE 373, Copyright S. Tanimoto, Binary Search Trees -

19 Right Rotation Done; Balance Restored
1 A BAD 1 D F CAT EGG 1 G CSE 373, Copyright S. Tanimoto, Binary Search Trees -

20 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Use of Balance Factors -1 C B E 1 +1 A BAD 1 D F CAT EGG 1 G 0 = balanced +1 = left heavy -1 = right heavy CSE 373, Copyright S. Tanimoto, Binary Search Trees -

21 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Insert FUN -2 C -1 B E 1 +1 -1 A BAD 1 D F CAT EGG 1 G +1 0 = balanced +1 = left heavy -1 = right heavy FUN CSE 373, Copyright S. Tanimoto, Binary Search Trees -

22 Prepare to Rotate left at C
-2 C -1 B E 1 +1 -1 A BAD 1 D F CAT EGG 1 G +1 0 = balanced +1 = left heavy -1 = right heavy FUN CSE 373, Copyright S. Tanimoto, Binary Search Trees -

23 CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Rebalanced E -1 C F 1 +1 +1 B D EGG 1 G A BAD 1 CAT FUN CSE 373, Copyright S. Tanimoto, Binary Search Trees -

24 General AVL Insertion Method
Find the place to insert the new key. (While descending the tree, remember the last node having balance factor = -1 or +1. Call it A.) If the key is already there, return. Otherwise insert the new key. If there is no A, go down the path from the root to the new key again, updating balance factors, and then return. Go back to A. If it had bf = 1 and the key went into its right subtree, or if it had bf = -1 and the key went into its left subtree, then set bf(A)=0, fix the bf values on the path from A to the new key, and return. Otherwise, if A had bf = 1 and the new key went into its left subtree, then perform either a LL rotation or a LR rotation depending upon whether the key went into the left or right subtree of A’s left subtree. If it had bf = -1 and the new key went into its right subtree, then perform either a RR rotation or a RL rotation depending upon whether the key went into the right or left subtree of A’s right subtree. Fix the bf values of the nodes involved in the rotation. Correct the bf values of the nodes on the path from A to the new key. CSE 373, Copyright S. Tanimoto, Binary Search Trees -

25 Single rotations classified according to which grandchild is too heavy
LL = single rotation right RR = single rotation left CSE 373, Copyright S. Tanimoto, Binary Search Trees -

26 Double rotations classified according to which grandchild is too heavy
LR = double rotation right RL = double rotation left CSE 373, Copyright S. Tanimoto, Binary Search Trees -

27 LL Rotation: Schematic
Z Y X Z Y T4 X T4 T3 T1 T2 T3 T1 T2 CSE 373, Copyright S. Tanimoto, Binary Search Trees -

28 LR Rotation: Schematic
Z Y X Z X T4 Y T4 T1 T2 T3 T1 T2 T3 CSE 373, Copyright S. Tanimoto, Binary Search Trees -

29 Deletion from an AVL Tree
(Assume that we have used the key to find the node q to be deleted.) Delete the key from the node q it’s in. Let p be the parent of q. If q had only one child, make it the new child of p and delete q. If q had two children, move the key of the left child into q and delete the left child node of q. Since the deletion may have caused the balance factors to change on the nodes on the path from the root to p, we ascend back along this path from p to the root, fixing the balance factors. At each node, we determine the new balance factor and if the node is unbalanced, perform one of the 4 rotations. If we reach a node whose height ends up the same as it was before the deletion, then the balance factors of its ancestors will also keep their old values, and the deletion operation is complete. If we reach the root and rebalance it, then the deletion operation is also complete. CSE 373, Copyright S. Tanimoto, Binary Search Trees -


Download ppt "CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -"

Similar presentations


Ads by Google