Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.

Similar presentations


Presentation on theme: "CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007."— Presentation transcript:

1 CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007

2 2 Tree Calculations Recall: height is max number of edges from root to a leaf Find the height of the tree... t Can you do this in less than O(n) time?

3 3 Tree Calculations Example A E B DF C G IH KJL M L N How high is this tree?

4 4 Binary Trees Binary tree is –a root –left subtree (maybe empty) –right subtree (maybe empty) Representation: A B DE C F HG JI Data right pointer left pointer

5 5 Binary Tree: Representation A right pointer left pointer A B DE C F B right pointer left pointer C right pointer left pointer D right pointer left pointer E right pointer left pointer F right pointer left pointer

6 6 Binary Tree: Special Cases A B DE C GF IH A B DE C F A B DE C GF Full Tree Complete TreePerfect Tree

7 7 Binary Tree: Some Numbers! For binary tree of height h: –max # of leaves: –max # of nodes: –min # of leaves: –min # of nodes:

8 8 More Recursive Tree Calculations: Tree Traversals A traversal is an order for visiting all the nodes of a tree Three types: Pre-order:Root, left subtree, right subtree In-order:Left subtree, root, right subtree Post-order:Left subtree, right subtree, root + * 24 5 (an expression tree)

9 9 Traversals void traverse(BNode t){ if (t != NULL) traverse (t.left); print t.element; traverse (t.right); } Which kind of traversal is this?

10 10 ADTs Seen So Far Stack –Push –Pop Queue –Enqueue –Dequeue What about decreaseKey? Priority Queue –Insert –DeleteMin

11 11 The Dictionary ADT Data: –a set of (key, value) pairs Operations: –Insert (key, value) –Find (key) –Remove (key) The Dictionary ADT is sometimes called the “Map ADT” blerner Ben Lerner, OH: Tues, 10:45—12:00 CSE 212 ioannis Ioannis Giotis, OH: M 11:00 CSE 430 gyanit Gyanit Singh, OH: M 11:00 CSE 430 insert(blerner, ….) find(ioannis) ioannis Ioannis Giotis, …

12 12 A Modest Few Uses Sets Dictionaries Networks: Router tables Operating systems: Page tables Compilers: Symbol tables Probably the most widely used ADT!

13 13 Implementations Unsorted Linked-list Unsorted array Sorted array insert delete find

14 14 Binary Search Tree Data Structure 4 121062 115 8 14 13 79 Structural property –each node has  2 children –result: storage is small operations are simple average depth is small Order property –all keys in left subtree smaller than root’s key –all keys in right subtree larger than root’s key –result: easy to find any given key What must I know about what I store?

15 15 Example and Counter-Example 3 1171 84 5 4 181062 115 8 20 21 BINARY SEARCH TREE NOT A BINARY SEARCH TREE 7 15

16 16 Find in BST, Recursive Node Find(Object key, Node root) { if (root == NULL) return NULL; if (key < root.key) return Find(key, root.left); else if (key > root.key) return Find(key, root.right); else return root; } 2092 155 10 307 17 Runtime:

17 17 Find in BST, Iterative Node Find(Object key, Node root) { while (root != NULL && root.key != key) { if (key < root.key) root = root.left; else root = root.right; } return root; } 2092 155 10 307 17 Runtime:

18 18 Insert in BST 2092 155 10 307 17 Runtime: Insert(13) Insert(8) Insert(31) Insertions happen only at the leaves – easy!

19 19 BuildTree for BST Suppose keys 1, 2, 3, 4, 5, 6, 7, 8, 9 are inserted into an initially empty BST. Runtime depends on the order! –in given order –in reverse order –median first, then left median, right median, etc.

20 20 Bonus: FindMin/FindMax Find minimum Find maximum 2092 155 10 307 17

21 21 Deletion in BST 2092 155 10 307 17 Why might deletion be harder than insertion?

22 22 Delete Operation Delete is a bit trickier…Why? Suppose you want to delete 10 Strategy: –Find 10 –Delete the node containing 10 Problem: When you delete a node, what do you replace it by? 94 1097 524 11 17

23 23 Delete Operation Problem: When you delete a node, what do you replace it by? Solution: –If it has no children, by NULL –If it has 1 child, by that child –If it has 2 children, by the node with the smallest value in its right subtree (the successor of the node) 94 1097 524 11 17

24 24 Delete “5” - No children Find 5 node Then Free the 5 node and NULL the pointer to it 94 1097 524 11 17 94 1097 524 11 17

25 25 Delete “24” - One child Find 24 node Then Free the 24 node and replace the pointer to it with a pointer to its child 94 1097 524 11 17 94 1097 524 11 17

26 26 Delete “10” - two children Find 10, Copy the smallest value in right subtree into the node Then recursively Delete node with smallest value in right subtree Note: it does not have two children 94 1097 524 11 17 94 1197 524 11 17

27 27 Delete “11” - One child Remember 11 node Then Free the 11 node and replace the pointer to it with a pointer to its child 94 1197 524 11 17 94 1197 524 11 17

28 28 Runtimes Find? Insert? Delete? What is the average height of a BST? What is the maximum height? What happens when we insert nodes in sorted order?

29 29 Balanced BST Observation BST: the shallower the better! For a BST with n nodes –Average height is O(log n) –Worst case height is O(n) Simple cases such as insert(1, 2, 3,..., n) lead to the worst case scenario Solution: Require a Balance Condition that 1.ensures depth is O(log n) – strong enough! 2.is easy to maintain – not too strong!

30 30 Potential Balance Conditions 1.Left and right subtrees of the root have equal number of nodes 2.Left and right subtrees of the root have equal height

31 31 Potential Balance Conditions 3.Left and right subtrees of every node have equal number of nodes 4.Left and right subtrees of every node have equal height

32 32 The AVL Balance Condition Left and right subtrees of every node have equal heights differing by at most 1 Define: balance(x) = height(x.left) – height(x.right) AVL property: –1  balance(x)  1, for every node x Ensures small depth –Will prove this by showing that an AVL tree of height h must have a lot of (i.e. O(2 h )) nodes Easy to maintain –Using single and double rotations

33 33 The AVL Tree Data Structure 4 121062 115 8 141379 Structural properties 1.Binary tree property 2.Balance property: balance of every node is between -1 and 1 Result: Worst case depth is O(log n) Ordering property –Same as for BST 15


Download ppt "CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007."

Similar presentations


Ads by Google