Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch. 12: Binary Search Trees Ming-Te Chi

Similar presentations


Presentation on theme: "Ch. 12: Binary Search Trees Ming-Te Chi"— Presentation transcript:

1 Ch. 12: Binary Search Trees Ming-Te Chi
Algorithms Ch. 12: Binary Search Trees Ming-Te Chi Ch12 Binary Search Trees

2 12.0 What is a binary tree? Binary tree: Empty, or
Composed by 3 disjoint set of nodes: root left subtree (binary tree), could be empty. right subtree (binary tree ), could be empty. Ch12 Binary Search Trees

3 12.1 What is a binary search tree?
Binary tree with the binary-search property. Binary-search property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y]  key[x]. If y is a node in the right subtree of x, then key[y] ≥ key[x]. Ch12 Binary Search Trees

4 Binary search Tree Ch12 Binary Search Trees

5 Tree traversal Preorder Inorder Postorder Ch12 Binary Search Trees

6 Inorder tree walk (traversal)
INORDER_TREE_WALK(x) 1 if 2 INORDER_TREE_WALK(x.left) 3 print x.key[x] 4 INORDER_TREE_WALK(x.right) Ch12 Binary Search Trees

7 Theorem 12.1 If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x) takes (n) time. Proof: Divide and conquer (recurrence) Substitution method. Ch12 Binary Search Trees

8 Preorder tree walk Postorder tree walk Ch12 Binary Search Trees

9 12.2 Querying a binary search tree
Ch12 Binary Search Trees

10 TREE_SEARCH(x, k) TREE_SEARCH(x, k) 1 if or 2 return x 3 if
4 return TREE_SEARCH(x.left, k) 5 else return TREE_SEARCH(x.right,k) Ch12 Binary Search Trees

11 ITERATIVE_SEARCH(x, k)
1 While and 2 do if then else 5 return x (more efficient) Ch12 Binary Search Trees

12 Q & A What is the complexity of TREE-SEARCH(x,k)?
Ch12 Binary Search Trees

13 MAXIMUM and MINIMUM TREE_MINIMUM(x) TREE_MAXIMUM(x)
1 while x.left  NIL 2 x = x.left 3 return x TREE_MAXIMUM(x) 1 while x.right  NIL 2 x = x.right Ch12 Binary Search Trees

14 Q & A What is the complexity of TREE-MINIMUM & TREE-MAXIMUM?
Ch12 Binary Search Trees

15 Successor and predecessor
The successor of a node x is the smallest key greater than key[x]. Predecessor: The predecessor of a node x is the (write your own definition.) Ch12 Binary Search Trees

16 TREE_SUCCESSOR Two cases: Right subtree is empty:
(Node 15, X=13) Right subtree is not empty: Minimum element in the right subtree. (node 17, when x=15) Ch12 Binary Search Trees

17 TREE_SUCCESSOR Right subtree is empty:
(the successor is the lowest ancester of x whose left child is also an ancestor of x) Go up the tree from x until we see a node that is the left child of its parent. Remarks: x has no parent (x is the root), no successor. (no element is greater then x) x has parent: x is the left child of its parent, the parent is the successor x is the right child of its parent (x >= parent[x]), moves up one level. (ref. Figure 12.2 on page 257, node 13.) Ch12 Binary Search Trees

18 TREE_SUCCESSOR TREE_SUCCESSOR 1 if 2 then return TREE_MINIMUM(x.right)
3 4 while and 5 6 7 return y Ch12 Binary Search Trees

19 Theorem 12.2 The dynamic-set operations, SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR can be made to run in O(h) time on a binary search tree of height h. Ch12 Binary Search Trees

20 12.3 Insertion and deletion
The dynamic-set operations, INSERT and DELETE change the structure of the binary-search-tree. The property of the BST remains. left[x] <= x <= right[x] O(h) operations ? Ch12 Binary Search Trees

21 Tree-Insert(T, z) 1 y = NIL  trace the parent of x (denoted by y)
2 x = T.root  trace the path 3 while x  NIL  moves down until x is set to NIL 4 y = x 5 if z.key < x.key x = x.left 7 else x = x.right 8 z.p = y  z is inserted at x (p[z] is set to y) 9 if y = NIL T.root[T] = z  tree T was empty 11 else if z.key < y.key y.left = z 13 else y.right = z Ch12 Binary Search Trees

22 Inserting an item with key 13 into a binary search tree
Ch12 Binary Search Trees

23 Deletion: DELETE(T, z) 3 cases z has no child. (simple)
Set (P[z])’s z-child to NIL z has just one child. (left or right) Spice out z z has both a left and a right child: find z’s successor y Spice out z’s successor y (y has no left child) Replace z’s data by y’s data Ch12 Binary Search Trees

24 DELETE(T,z) - reorganization
3 cases z has no left child. Replace z by its right child z has just one child, which is its left child Replace z by its left child z has both a left and a right child, find z’s successor y 1. If y is z’s right child, replace z by y 2. Otherwise, y lies within z’s right subtree, but is not ’’s right child. Replace y, by its own right child, and then we replace z by y. Ch12 Binary Search Trees

25 z has no left child Replace z by its right child
Ch12 Binary Search Trees

26 z has just one child, which is its left child
Replace z by its left child Ch12 Binary Search Trees

27 z has two children find z’s successor y
If y is z’s right child, replace z by y Ch12 Binary Search Trees

28 z has two children Otherwise, y lies within z’s right subtree, but is not z’s right child. Replace y, by its own right child, and then we replace z by y. Ch12 Binary Search Trees

29 TRANSPLANT Transplant(T, z, y) Ch12 Binary Search Trees

30 Ch12 Binary Search Trees

31 Theorem 12.3 The dynamic-set operations, INSERT and DELETE can be made to run in O(h) time on a binary search tree of height h. Ch12 Binary Search Trees

32 Remarks on binary-search-tree
All basic operations take time proportional to the height of the tree. A complete binary tree with n nodes, the height is log n, the operations runs in O(log n) worst-case time. Complete tree: all leaves have the same depth and all internal nodes have the same degree. If the tree is linear chain of n nodes, the same operations take O(n) worst-case time. (No better than linked list) Ch12 Binary Search Trees

33 Remarks on binary-search-tree
Expected height of randomly build binary search tree is O(log n). (ref. §12.4) In real life, randomly build binary search tree can not be expected. Variations of binary search tree could achieve this O(log n) height. Red-black tree is one of the examples. Ch12 Binary Search Trees


Download ppt "Ch. 12: Binary Search Trees Ming-Te Chi"

Similar presentations


Ads by Google