Binary Search Tree Sorting and Searching in a Dynamic Set S.V. N. (vishy) Vishwanathan University of California, Santa Cruz [email protected] February 29, 2016 S.V. N. Vishwanathan (UCSC) CMPS101 1 / 23 Binary Search Trees Outline 1 Binary Search Trees S.V. N. Vishwanathan (UCSC) CMPS101 2 / 23 Binary Search Trees What is a Binary Search Tree? Binary tree (each node has 0, 1, or 2 children) Each node contains key satellite data left right parent The keys satisfy the binary search tree 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 y .key ≥ x.key . S.V. N. Vishwanathan (UCSC) CMPS101 3 / 23 Binary Search Trees Examples S.V. N. Vishwanathan (UCSC) CMPS101 4 / 23 Binary Search Trees Inorder Tree Walk inorder-tree-walk(x) 1 2 3 4 if x 6= nil inorder-tree-walk(x.left) print x.key inorder-tree-walk(x.right) S.V. N. Vishwanathan (UCSC) CMPS101 5 / 23 Binary Search Trees Search tree-search(x, k) 1 2 3 4 5 if x == nil or k == x.key return x if k < x.key return tree-search(x.left, k) else return tree-search(x.right, k) S.V. N. Vishwanathan (UCSC) CMPS101 6 / 23 Binary Search Trees Iterative Search iterative-tree-search(x, k) 1 2 3 4 5 while x 6= nil and k 6= x.key if k < x.key x = x.left else x = x.right return x S.V. N. Vishwanathan (UCSC) CMPS101 7 / 23 Binary Search Trees Search S.V. N. Vishwanathan (UCSC) CMPS101 8 / 23 Binary Search Trees Minimum tree-minimum(x) 1 2 3 while x.left 6= nil x = x.left return x S.V. N. Vishwanathan (UCSC) CMPS101 9 / 23 Binary Search Trees Maximum tree-maximum(x) 1 2 3 while x.right 6= nil x = x.right return x S.V. N. Vishwanathan (UCSC) CMPS101 10 / 23 Binary Search Trees Minimum and Maximum S.V. N. Vishwanathan (UCSC) CMPS101 11 / 23 Binary Search Trees Successor Given a node x, its successor is the node with the smallest key greater than x.key It is the next node which occurs after x in the sorted order determined by an inorder tree walk S.V. N. Vishwanathan (UCSC) CMPS101 12 / 23 Binary Search Trees Successor Look at node 15 and 13 S.V. N. Vishwanathan (UCSC) CMPS101 13 / 23 Binary Search Trees Successor tree-successor(x) 1 2 3 4 5 6 7 if x.right 6= nil return tree-minimum(x.right) y = x.parent while y 6= nil and x == y .right x =y y = y .parent return y S.V. N. Vishwanathan (UCSC) CMPS101 14 / 23 Binary Search Trees Time Complexity Theorem We can implement the dynamic-set operations search, minimum, maximum, successor, and predecessor so that each one runs in O(h) time on a binary search tree of height h S.V. N. Vishwanathan (UCSC) CMPS101 15 / 23 Binary Search Trees Insertion tree-insert(T , z) 1 2 3 4 5 6 7 8 9 10 11 12 13 y = nil x = T .root while x 6= nil y =x if z.key < x.key x = x.left else x = x.right z.parent = y if y == nil T .root = z // tree T was empty elseif z.key < y .key y .left = z else y .right = z S.V. N. Vishwanathan (UCSC) CMPS101 16 / 23 Binary Search Trees Insertion Look at node 13 S.V. N. Vishwanathan (UCSC) CMPS101 17 / 23 Binary Search Trees Deletion: Three Cases z has no children. Delete z and modify its parent z has one child. Elevate child to take z’s position z has two children. Find z’s successor y . We know that y has no left child. Why? y must lie in the right subtree of z Replace z with y However have to be careful if y is the right child of z S.V. N. Vishwanathan (UCSC) CMPS101 18 / 23 Binary Search Trees Deletion S.V. N. Vishwanathan (UCSC) CMPS101 19 / 23 Binary Search Trees Deletion: Split to Four Cases z has no left child. Replace z by its right child (which may be a nil z has a left child. Replace z by its left child. z has both left and right children. Find z’s successor y y must lie in the right subtree of z and has no left child If y is z’s right child, then replace z by y Otherwise, replace y by its right child, and replace z by y S.V. N. Vishwanathan (UCSC) CMPS101 20 / 23 Binary Search Trees Pseudo-code 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) 5 else y = tree-minimum(z.right) 6 if y .parent 6= z 7 transplant(T , y , y .right) 8 y .right = z.right 9 y .right.parent = y 10 transplant(T , z, y ) 11 y .left = z.left 12 y .left.parent = y S.V. N. Vishwanathan (UCSC) CMPS101 21 / 23 Binary Search Trees Pseudo-code transplant(T , u, v ) 1 2 3 4 5 6 7 if u.parent == nil T .root = v elseif u == u.parent.left u.parent.leftgetsv else u.parent.right = v if v 6= nil v .parent = u.parent S.V. N. Vishwanathan (UCSC) CMPS101 22 / 23 Binary Search Trees Questions? S.V. N. Vishwanathan (UCSC) CMPS101 23 / 23
© Copyright 2026 Paperzz