11-Binary Search Trees

# 11-Binary Search Trees - Binary Search Trees Define a BST...

• Notes
• 15
• 100% (1) 1 out of 1 people found this document helpful

This preview shows pages 1–4. Sign up to view the full content.

Binary Search Trees Define a BST. Show the connection with inorder numbering of nodes and sorted order. Simple search, insertion, deletion, and growing algorithms. Behavior under random inputs - each of the n! input sequences of integers 1 to n are equally likely. Tree built using simple growing algorithm and then searched with each key equally likely to be the search key. How many comparisons on the average? Example for n = 3. 123 132 213 231 312 321 1 1 2 2 3 3 \ \ / \ / \ / / 2 3 1 3 1 3 1 2 \ / \ / 3 2 2 1 1+2+3 1+3+2 2+1+2 2+1+2 2+3+1 3+2+1 3 3 3 3 3 3 Average number of comparisons = 1/6(4x6/3 + 2x5/3) = 34/18 = 17/9. Average path length = [(1+2+3)x4 + (1+2+2)x2]/6=34/6=17/3 In general, let C n be: the average number of comparisons for n integers. Then C n = ∑ P(i is at root) [P(key is i)x1 + P(key < i)x(1+ C i-1 ) + P(key > i)x(1+ C n-i )] i=1,n = ∑ 1/n [(1/n)x1 + ((i-1)/n)x(1+ C i-1 ) + ((n-i)/n)x(1+ C n-i )] i=1,n = ∑ 1/n [(1+i-1+n-i)/n+((i-1)/n)C i-1 + ((n-i)/n)C n-i ] i=1,n = 1 + 2/n 2 ∑ (i-1)C i-1 = 1 + 2/n 2 ∑ iC i i=1,n i=1,n-1 Actually, C n = 2((n+1)/n)H n - 3 where H n = 1+1/2+1/3+ . . . +1/n =O(ln n). So, C 1 =1, C 2 =3/2, C 3 =17/9, C 4 = 53/24, C 5 = 62/25, etc Hence, C n ≈ 1.39 lg n for large n. Suppose that instead, we want to determine the average path length which represents the average number of comparisons to search for all the integers (i.e. n searches - one for each i from 1 to n). - what will be the average path length D n ? Whatever key is being searched for, each integer k has probability 1/n of being the root of the tree. Suppose k is at the root, then key contributes 1 to the average path length and the k-1 keys < k contributes 1+ D k-1 . Similarly, the n-k keys > k contribute 1+D n-k . Hence, D 0 = 0 and

This preview has intentionally blurred sections. Sign up to view the full version.

for n>0, D n = n + 1/n ∑ (D k-1 + D n-k ). k=1, n So, D 1 =1, D 2 =3, D 3 =17/3, D 4 = 53/6, D 5 = 62/5, etc It should be apparent that D n = nC n so D n = 2(n+1)H n - 3n ≈ 1.39nlgn for large n This is encouraging for random input! In fact, Cormen et al show that the average depth of a tree built this way from random input is O(log n). However, even so, it is possible that the tree can have depth n in the worst case! Balanced trees - Fibonacci trees A binary tree is balanced if the depths of the subtrees of each node of the tree differ by at most one. Examples: (a) Balanced (b) Unbalanced What makes balanced trees important?
The maximum number of nodes that a balanced binary tree with depth d is a complete binary tree with 2 d -1 nodes. . A binary tree is a Fibonacci tree if it is balanced and has the minimum number of nodes among all balanced binary trees with its depth. Fibonacci trees represent the worst case for balanced trees, since they contain the fewest nodes (and hence the least storage capacity) among all balanced trees with a specified depth. The binary tree below is a Fibonacci tree of depth 4. This is a Fibonacci tree because it is balanced, and no balanced binary tree with a depth of 4 has fewer than 7 nodes. Any complete binary tree of depth 4, for example, will not be a Fibonacci tree because, although balanced, it will have more than the minimum number of nodes (7) for this depth.

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• James Korsh

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern