Presentation is loading. Please wait.

Presentation is loading. Please wait.

12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P.2 12.1 What is a binary search tree? Binary-search property: Let x be a node in.

Similar presentations


Presentation on theme: "12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P.2 12.1 What is a binary search tree? Binary-search property: Let x be a node in."— Presentation transcript:

1 12.Binary Search Trees Hsu, Lih-Hsing

2 Computer Theory Lab. Chapter 12P.2 12.1 What is a binary search tree? 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 y.key  x.key. If y is a node in the right subtree of x, then x.key  y.key.

3 Computer Theory Lab. Chapter 12P.3 Binary search Tree

4 Computer Theory Lab. Chapter 12P.4 Inorder tree walk INORDER_TREE_WALK(x) 1if 2then INORDER_TREE_WALK(x.left) 3print x.key 4INORDER_TREE_WALK(x.right)

5 Computer Theory Lab. Chapter 12P.5 Theorem 12.1 If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x) takes  (n) time.

6 Computer Theory Lab. Chapter 12P.6 Preorder tree walk Postorder tree walk

7 Computer Theory Lab. Chapter 12P.7 12.2 Querying a binary search tree

8 Computer Theory Lab. Chapter 12P.8 TREE_SEARCH(x,k) 1if or 2then return x 3if 4then return TREE_SEARCH(x.left,k) 5else return TREE_SEARCH(x.right,k)

9 Computer Theory Lab. Chapter 12P.9 ITERATIVE_SEARCH(x,k) 1While or 2do if 3then 4then 5return x

10 Computer Theory Lab. Chapter 12P.10 MAXIMUM and MINIMUM TREE_MINIMUM(x) 1 while x.left  NIL 2 do x = x.left 3 return x TREE_MAXIMUM(x) 1 while x.right  NIL 2 do x = x.right 3 return x

11 SUCCESSOR and PREDECESSOR

12 Computer Theory Lab. Chapter 12P.12 SUCCESSOR and PREDECESSOR Given a node in a binary search tree, sometimes we need to find its successor in the sorted order determined by an inorder tree walk. If all keys are distinct, the successor of a node x is the node with the smallest key greater than x.key.The structure of a binary search tree allows us to determine the successor of a node without ever comparing keys. The following procedure returns the successor of a node x in a binary search tree if it exists, and NIL if x has the largest key in the tree:

13 Computer Theory Lab. Chapter 12P.13 TREE_SUCCESSOR 1 if 2 then return TREE_MINIMUM(x.right) 3 4 while and 5 do 6 7 return y

14 Computer Theory Lab. Chapter 12P.14 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.

15 12.3 Insertion and deletion

16 Computer Theory Lab. Chapter 12P.16 Insertion and deletion The operations of insertion and deletion cause the dynamic set represented by a binary search tree to change. The data structure must be modified to reflect this change, but in such a way that the binary-search-tree property continues to hold. As we shall see, modifying the tree to insert a new element is relatively straight-forward,but handling deletion is some- what more intricate.

17 Computer Theory Lab. Chapter 12P.17 Tree-Insert(T,z) 1 y = NIL 2 x = T.root 3 while x  NIL 4 do y = x 5if z.key < x.key 6 then x = x.left 7 else x = x.right 8 x.p = y Insertion

18 Computer Theory Lab. Chapter 12P.18 9 if y == NIL 10 then T.root = z  tree T was empty 11 else if z.key < y.key 12 then y.left = z 13else y.right = z

19 Computer Theory Lab. Chapter 12P.19 Inserting an item with key 13 into a binary search tree

20 Computer Theory Lab. Chapter 12P.20 Tree-Delete(T,z) 1 if z.left == NIL 2 TRANSPLANT(T,z,z.right) 3 elseif z.right== NIL 4 TRANSPLANT(T,z,z.left) 5else y = TREE-MINIMUM(z.right) 6if y.p  z 7 TRANSPLANT(T,y,y.right) 8 y,right = z.right Deletion

21 Computer Theory Lab. Chapter 12P.21 9 y.right.p = y 10 TRANSPLANT(T,z,y) 11 y.left = z.left 12 y.left.p = y

22 Computer Theory Lab. Chapter 12P.22 z has no children

23 Computer Theory Lab. Chapter 12P.23 z has only one child

24 Computer Theory Lab. Chapter 12P.24 z has two children

25 Computer Theory Lab. Chapter 12P.25 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.


Download ppt "12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P.2 12.1 What is a binary search tree? Binary-search property: Let x be a node in."

Similar presentations


Ads by Google