1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees
2 Binary Trees A Binary tree is a linked data structure. Each node contains data (including a key and satellite data), and pointers left, right and p. Left points to the left child of the node. Right points to the right child of the node. p points to the parent of the node. If a child is missing, the pointer is NIL. If a parent is missing, p is NIL. The root of the tree is the only node for which p is NIL. Nodes for which both left and right are NIL are leaves.
3 Binary Search Trees A binary search tree has keys that 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 key[y] <= key[x]. If y is a node in the right subtree of x, then key[y] >= key[x]. Examples: A. B
4 Traversing a Binary Tree In-order traversal means that we first traverse the left subtree, then print the key of the current node, and finally traverse the right subtree. In-Order-Tree-Walk(x) if x != NIL then In-Order-Tree-Walk(left[x]) Print key[x] In-Order-Tree-Walk(right[x]) Note that the in-order traversal of a binary search tree prints out the keys in sorted order (see trees A and B on previous slide).
5 Other tree traversal orders Pre-order traversal: print key[x] traverse left subtree traverse right subtree Post-order traversal: traverse left subtree traverse right subtree print key[x]
6 Running time of Traversal For a tree of size n (that has n nodes): If the left subtree has k nodes, then the right subtree has n-k-1 nodes. T(n) = T(k) + T(n-k-1) + d Substitution Method: Assume T(n) = (c+d)n + c T(0) = c For n > 0 T(n) = ? = T(k) + T(n-k-1) + d = ((c+d)k + c) + ((c+d)(n - k - 1) + c) + d = (c + d) n + c Therefore, T(n) = (n)
7 Searching a binary search tree Tree-Search(x, k){Search from node, x, for key, k} if x = NIL or k = key[x] then return x if k < key[x] then return Tree-Search(left[x], k) else return Tree-Search(right[x], k) Example: a) Search for 7 b) Search for 11 Running time?
8 Finding the minimum and maximum To find the minimum key value, we follow the leftmost path from root to NIL. Similarly, to find the maximum key value, we follow the rightmost path from root to NIL. Tree-Minimum while left[x] != NIL do x left[x] return x Tree-Maximum while right[x] != NIL do x right[x] return x Running Time?
9 Finding the Successor to a node Idea: We want to find the node with the smallest key greater than key[x]. We can do this without comparing keys! Two cases for finding the successor: Case 1: The right subtree of x is not empty. The successor of x is the minimum of the right subtree. Case 2: The right subtree of x is empty. The successor of x (if it exists) is the lowest ancestor of x whose left child is also an ancestor x. (i.e. x is in the rightmost path of the left subtree of successor of x)
10 Successor Pseudocode Example: Case 1: Successor of 6 is 7 Case 2: Successor of 8 is 10 Tree-Successor(x) if right[x] != NIL then return Tree-Minimum(right[x]) y p[x] while y != NIL and x = right[y] do x y y p[y] return y
11 Inserting into a Binary Search Tree Problem: Insert a new node, z, with key[z] = v, left[z]=right[z]=p[z] = NIL, into its proper position on the tree. Idea: Follow the tree from root to leaf, going right or left depending on whether key[z] is greater than or less than the key of the current node Example: Insert node with v = 9 Running time?
12 Pseudocode for Tree-Insert Tree-Insert(T, z) y NIL x root(T){Pointer to root node} while x != NIL do{Find Position} y x{Store current node before moving on} {what goes here?} p[z] y{Previous node, y, is now parent} if y = NIL then{Tree was empty} root[T] z else if key[z] < key[y]{Insert in left branch of parent} left[y] z else{Insert in right branch of parent} right[y] z