650 likes | 806 Views
O(lg n) Search Tree. Tree T is a search tree made up of n elements: x 0 x 1 x 2 x 3 … x n-1 No function (except transverse) takes more than O(lg n) in the worst case. Functions: createEmptyTree() returns a newly created empty binary tree
E N D
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the worst case. Functions: createEmptyTree() returns a newly created empty binary tree delete(T, p) removes the node pointed to by p from the tree T insert(T, x) returns T with x added in the proper location search(T, key) returns a pointer to the node in T that has a key that matches key returns null if x is not found traverse(T) prints the contents of T in order isEmptyTree(T) returns true if T is empty and false if it is not
Homework 5 • Describe how to implement a search tree that has a worst time search, insert, and delete time of no more than O(lg n). This tree should have no number of element limit. • Do the six Search Tree functions. • Discuss how you would implement this if there was a maximum to the number of elements.
AVL Tree 0 54 0 +1 21 72 +1 -1 0 5 30 60 84 10 25 79 86
AVL Tree • The five functions are the same. • Except that the tree needs to be rebalanced after insertion or deletion. • Keep track of the path used to insert/delete. • Balance starting at the parent of the leaf inserted or deleted. • Work up to the root.
Single Rotation +2 +1
Double Rotation +2 -1
Single Rotation +2 +1
Single Rotation A +2 B +1
Single Rotation B A
Single Rotation B A +2 +1
Single Rotation B A 0 0
Single Rotation +2 +1
Single Rotation 0 +1
Single Rotation +1 0
Single Rotation +1 0
Single Rotation +1 0
Single Rotation 0 0
Double Rotation +2 -1
Double Rotation +1 0 0
Double Rotation1 A +2 C -1 B -1
Double Rotation1 B C A +2 -1 -1
Double Rotation1 A +2 C -1 B -1
Double Rotation1 A +2 C -1 B -1
Double Rotation1 A +1 C +1 B -1
Double Rotation1 A +1 B C +1 -1
Double Rotation1 A +1 B C +1 -1
Double Rotation1 A +1 B -1 C +1
Double Rotation1 A +2 B +1 C +1
Double Rotation1 A 0 B +1 C +1
Double Rotation1 A B 0 +1 C +1
Double Rotation1 B +1 A 0 C +1
Double Rotation1 B 0 A 0 C +1
Double Rotation1 B 0 A C 0 +1
Double Rotation2 A +2 C -1 B +1
Double Rotation2 A +2 C -1 B +1
Double Rotation2 A +2 C 0 B +1
Double Rotation2 A +2 B C 0 +1
Double Rotation2 A +2 B +2 C 0