Docsity

Binary Trees - Algorithms Data Structure | CS 561, Study notes of Computer Science

Partial preview of the text

Download Binary Trees - Algorithms Data Structure | CS 561 and more Study notes Computer Science in PDF only on Docsity!

CS 561, Lecture 9

Jared Saia University of New Mexico

Today’s Outline

  • Binary Trees

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(x)≤key(y)

Search in BT

Tree-Search(x,k){ if (x=nil) or (k = key(x)){ return x; } if (k<key(x)){ return Tree-Search(left(x),k); }else{ return Tree-Search(right(x),k); } }

Analysis

  • Let h be the height of the tree
  • The run time is O(h)
  • Correctness???

Previous In-Class Exercise

  • Q1: What is the loop invariant for Tree-Search?
  • Q2: What is Initialization?
  • Q3: Maintenance?
  • Q4: Termination?

Loop Invariant Review

A useful tool for proving correctness is loop invariants. Three things must be shown about a loop invariant

  • Initialization: Invariant is true before first iteration of loop
  • Maintenance: If invariant is true before iteration i, it is also true before iteration i + 1
  • Termination: When the loop terminates, the invariant gives a property which can be used to show the algorithm is correct

Loop Invariant Review

  • When Initialization and Maintenance hold, the loop invari- ant is true prior to every iteration of the loop
  • Similar to mathematical induction: must show both base case and inductive step
  • Showing the invariant holds before the first iteration is like the base case. Showing the invariant holds from iteration to iteration is like the inductive step

Maintenance

  • Maintenance: Assume at the beginning of the procedure, it’s true that if key k exists in the tree that it is in the subtree rooted at node x. There are three cases that can occur during the procedure: - Case 1: key(x) is k. In this case, the procedure terminates and returns x, so the invariant continues to hold - Case 2: k<key(x). In this case, by the BST Property, all keys in the subtree rooted on the right child of x are greater than k (since key(x)>k). Thus, if k exists in the subtree rooted at x, it must exist in the subtree rooted at left(x). - Case 3:k>key(x). In this case, by the BST Property, All keys in the subtree rooted on the right child of x are less than k (since key(x)<k). Thus, if k exists in the subtree rooted at x, it must exist in the subtree rooted at right(x).

12

Termination

  • By the loop invariant, we know that when the procedure terminates, if k is in the tree, then it is in the subtree rooted at x. If k is in fact in the tree, then x will never be nil, and so the procedure will only terminate by returning a node with key k. If k is not in the tree, then the only way the procedure will terminate is when x is nil. Thus, in this case also, the procedure will return the correct answer.

Tree Min/Max

  • Tree Minimum(x): Return the leftmost child in the tree rooted at x
  • Tree Maximum(x): Return the rightmost child in the tree rooted at x

Successor

  • The successor of a node x is the node that comes after x in the sorted order determined by an in-order tree walk.
  • If all keys are distinct, the successor of a node x is the node with the smallest key greater than x

Tree-Successor

Tree-Successor(x){ if (right(x) != null){ return Tree-Minimum(right(x)); } y = parent(x); while (y!=null and x=right(y)){ x = y; y = parent(y); } return y; }

Successor Intuition

  • Case 1: If right subtree of x is non-empty, successor(x) is just the leftmost node in the right subtree
  • Case 2: If the right subtree of x is empty and x has a suc- cessor,x then successor(x) is the lowest ancestor of x whose left child is also an ancestor of x. (i.e. the lowest ancestor of x whose key is ≥ key(x))

Insertion

Insert(T,x)

  1. Let r be the root of T.
  2. Do Tree-Search(r,key(x)) and let p be the last node pro- cessed in that search
  3. If p is nil (there is no tree), make x the root of a new tree
  4. Else if key(x) ≤ p, make x the left child of p, else make x the right child of p

Deletion

  • Code is in book, basically there are three cases, two are easy and one is tricky
  • Case 1: The node to delete has no children. Then we just delete the node
  • Case 2: The node to delete has one child. Then we delete the node and “splice” together the two resulting trees

Analysis

  • Let Tl, Tr be the left and right subtrees of T respectively. Let n be the number of nodes in T
  • Then P (T ) = P (Tl) + P (Tr) + n − 1. Why?

Analysis

  • Let P (n) be the expected total depth of all nodes in a ran- domly built binary tree with n nodes
  • Note that for all i, 0 ≤ i ≤ n − 1, the probability that Tl has i nodes and Tr has n − i − 1 nodes is 1/n.
  • Thus P (n) = (^1) n ∑n− 1 i=0 (P^ (i) +^ P^ (n^ −^ i^ −^ 1) +^ n^ −^ 1)

Analysis

P (n) =

n

n∑− 1

i=

(P (i) + P (n − i − 1) + n − 1) (1)

n

n∑− 1

i=

(P (i) + P (n − i − 1)) +

n

n∑− 1

i=

n − 1)) (2)

n

n∑− 1

i=

(P (i) + P (n − i − 1)) + Θ(n) (3)

n

n∑− 1

k=

P (k)) + Θ(n) (4)

(5)

Analysis

  • We have P (n) = (^2) n( ∑n− 1 k=1 P^ (k)) + Θ(n)
  • This is the same recurrence for randomized Quicksort
  • In your hw (problem 7-2), you show that the solution to this recurrence is P (n) = O(n log n)

Take Away

  • P (n) is the expected total depth of all nodes in a randomly built binary tree with n nodes.
  • We’ve shown that P (n) = O(n log n)
  • There are n nodes total
  • Thus the expected average depth of a node is O(log n)

Take Away

  • The expected average depth of a node in a randomly built binary tree is O(log n)
  • This implies that operations like search, insert, delete take expected time O(log n) for a randomly built binary tree

Warning!

  • In many cases, data is not inserted randomly into a binary search tree
  • I.e. many binary search trees are not “randomly built”
  • For example, data might be inserted into the binary search tree in almost sorted order
  • Then the BST would not be randomly built, and so the expected average depth of the nodes would not be O(log n)

What to do?

  • A Red-Black tree implements the dictionary operations in such a way that the height of the tree is always O(log n), where n is the number of nodes
  • This will guarantee that no matter how the tree is built that all operations will always take O(log n) time
  • Next time we’ll see how to create Red-Black Trees