340 likes | 471 Views
Algorithms. Ch. 12: Binary Search Trees Ming-Te Chi. 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. 12.1 What is a binary search tree ?.
E N D
Algorithms Ch. 12: Binary Search Trees Ming-Te Chi Ch12 Binary Search Trees
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
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
Binary search Tree Ch12 Binary Search Trees
Tree traversal • Preorder • Inorder • Postorder Ch12 Binary Search Trees
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
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
Preorder tree walk • Postorder tree walk Ch12 Binary Search Trees
12.2 Querying a binary search tree Ch12 Binary Search Trees
TREE_SEARCH(x,k) TREE_SEARCH(x, k) 1 ifor 2 returnx 3 if 4 return TREE_SEARCH(x.left, k) 5 else return TREE_SEARCH(x.right,k) Ch12 Binary Search Trees
ITERATIVE_SEARCH(x,k) ITERATIVE_SEARCH(x,k) 1 While and 2 do if 3 then 4 else 5 returnx (more efficient) Ch12 Binary Search Trees
Q & A • What is the complexity of TREE-SEARCH(x,k)? Ch12 Binary Search Trees
MAXIMUM and MINIMUM • TREE_MINIMUM(x) 1 while x.left NIL 2 x = x.left 3 returnx • TREE_MAXIMUM(x) 1 while x.right NIL 2 x = x.right 3 returnx Ch12 Binary Search Trees
Q & A • What is the complexity of TREE-MINIMUM & TREE-MAXIMUM? Ch12 Binary Search Trees
Successor and predecessor • Successor: 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
TREE_SUCCESSOR Two cases: • Right subtree is empty: • Right subtree is not empty: • Minimum element in the right subtree. (node 17) Ch12 Binary Search Trees
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
TREE_SUCCESSOR TREE_SUCCESSOR 1 if 2 then returnTREE_MINIMUM(x.right) 3 4 whileand 5 6 7 returny Ch12 Binary Search Trees
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 heighth. Ch12 Binary Search Trees
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. • O(h) operations ? • left[x] <= x <= right[x] Ch12 Binary Search Trees
Tree-Insert(T, z) 1 y = NIL trace the parent of x (denoted by y) 2 x= T.roottrace the path 3 whilex NIL moves down until x is set to NIL 4 y= x 5 ifz.key < x.key 6 x= x.left 7 elsex= x.right 8 z.p = y z is inserted at x (p[z] is set to y) 9 ify = NIL 10 T.root[T] = z tree T was empty 11 elseifz.key < y.key 12 y.left= z 13 elsey.right= z Ch12 Binary Search Trees
Inserting an item with key 13 into a binary search tree Ch12 Binary Search Trees
Deletion: DELETE(T,z) • 4 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 right child • z has both a left and a right child, find z’s successor y • If y is z’s right child, replace z by y • 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
z has no left child • Replace z by its right child Ch12 Binary Search Trees
z has just one child, which is its left child • Replace z by its right child Ch12 Binary Search Trees
z has two children • find z’s successor y • If y is z’s right child, replace z by y Ch12 Binary Search Trees
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
TRANSPLANT Ch12 Binary Search Trees
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 heighth. Ch12 Binary Search Trees
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 logn, 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
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