Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.

Similar presentations


Presentation on theme: "Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key."— Presentation transcript:

1 Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key

2 Inorder Traversal 1. Traverse the left subtree. 2. Visit the node. 3. Traverse the right subtree. a b c d e Traversal order: bdaec (n) Running time: T(n) = T(k) + T(n–k – 1) + d visit time d size k size n–k–1 c b a d e

3 Postorder Traversal 1. Traverse the left subtree. 2. Traverse the right subtree. 3. Visit the node. a b c d e Traversal order: dbeca c e a b d

4 Preorder Traversal 1. Visit the node. 2. Traverse the left subtree. 3. Traverse the right subtree. a b c d e Traversal order: abdce e c d b a

5 Another Traversal Example 15 3 6 2 4 13 7 9 20 18 17 Preorder: Inorder: Postorder: 15, 6, 3, 2, 4, 7, 13, 9, 18, 17, 20 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20 2, 4, 3, 9, 13, 7, 6, 17, 20, 18, 15

6 Binary Search Trees Storage of elements for efficient access. The binary-search-tree property: If node y in left subtree of node x, then key[y] < key[x]. Supporting dynamic set operations. If node y in right subtree of node x, then key[y] key[x].

7 A BST Example 50 30 25 35 10 20 31 37 55 5360 62 Search for 37 Search time O(h) where h is tree height

8 Inorder Traversal of BST 50 30 25 35 10 20 31 37 55 5360 62 Prints out keys in sorted order: 10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62

9 Successor and Predecessor 50 30 25 35 10 20 31 37 55 5360 62 The predecessor of x is the rightmost node in its left subtree: The successor of x is the leftmost node in its right subtree x 10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62 23 y The successor of y is lowest ancestor whose left child is y or an ancestor of y no right subtree

10 BST Operations Preview 25 10 37 153065 Insert 8 Delete 25 10 25 158 37 30 65 The successor of 25 65 3710 30 15

11 Insertion Example: insert z = 32 25 40 20 12 35 x Compare 32 and 25 traverse the right subtree Compare 32 and 35, traverse the left subtree insert 32 as left child 40 35 12 20 25 x y 32 35 12 20 25 40 y z

12 Deletion (A) Case A: node x (to be deleted) is a leaf node Operation: update the parent node to have an empty subtree. 25 15 40 20 17 45 30 x

13 Deletion (B) Case B: node x has a left child but no right child Operation: attach the left subtree of x to the parent 25 15 40 20 17 45 30 x 17

14 Deletion (C) Case C: node x has a right child but no left child Operation: attach the right subtree of x to the parent 25 15 40 20 17 45 30 45 30 17 20 40 25 x

15 Deletion (D) Case D: node x has two children r x A C D y z B 2. Unlink y from the tree. 3. Connect ys right subtree to its parent. 4. Finally, connect y at the deleted node. r x A C D y z B Operation: 1. Select as the replacement (of x) node y, the successor of x. y has the smallest value greater than that of x. y

16 Deletion (D) - an Example 40 30 25 1028 35 33 65 50 replacement of 30 34 40 33 35 25 10 28 65 34 50 10, 25, 28, 30, 33, 34, 35, 40, 50, 6510, 25, 28, 33, 34, 35, 40, 50, 65

17 Running Time All basic operations on a binary search tree of height h run in O(h) time. Main question: How to guarantee h = O(lg n)? One answer: red-black trees! 2 2 4 2 4 6 2 4 6 2n2n … Insert 4 Insert 6 A worst case :


Download ppt "Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key."

Similar presentations


Ads by Google