Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 21 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 y.key <= x.key If y is a node in the right subtree of x, then y.key >= x.key Examples: A B. 3 10 5 6 12 8 4 8 14 10 6 5 7 13

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 In-Order-Tree-Walk(x.left) Print x.key In-Order-Tree-Walk(x.right) 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 x.key traverse left subtree traverse right subtree Post-order traversal:

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) = Q(n)

7 Searching a binary search tree
Tree-Search(x, k) //Search from node, x, for key, k 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) Example: a) Search for 7 b) Search for 11 10 Running time? 6 12 4 8 14 5 7

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 x.left != NIL x = x.left return x Tree-Maximum while x.right != NIL x = x.right 10 6 12 4 8 14 5 7 Running Time?

9 Finding the Successor to a node
Idea: We want to find the node with the smallest key greater than x.key. 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 10 Example: Case 1: Successor of 6 is 7
12 4 8 14 Tree-Successor(x) if x.right != NIL return Tree-Minimum(x.right) y = x.p while y != NIL and x == y.right x = y y = y.p return y 5 7

11 Inserting into a Binary Search Tree
Problem: Insert a new node, z, with z.key = v, z.left = z.right = z.p = NIL, into its proper position on the tree. Idea: Follow the tree from root to leaf, going right or left depending on whether z.key is greater than or less than the key of the current node. 10 Example: Insert node with v = 9 Running time? 6 12 4 8 14 5 7

12 Pseudocode for Tree-Insert
Tree-Insert(T, z) y = NIL x = T.root //Pointer to root node while x != NIL //Find Position y = x //Store current node before moving on //what goes here? z.p = y //Previous node, y, is now parent if y == NIL //Tree was empty T.root = z else if z.key < y.key //Insert in left branch of parent y.left = z else //Insert in right branch of parent y.right = z


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees"

Similar presentations


Ads by Google