Download presentation
Presentation is loading. Please wait.
Published byTobias Ray Modified over 8 years ago
1
DAST Tirgul 6
2
What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property: Let x be a node in a BST. 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]. Different BSTs can represent the same set of values. The worst-case running time for most operations in search-tree is proportional to the height of the tree.
3
Examples of BST 5 73 52 8 2 3 5 7 5 8 A BST on 6 nodes with height 2. A less efficient BST with height 4 that contains the same keys.
4
BST Delete TREE_DELETE (tree T, node z) If z is a leaf we change it’s parent so it will point to null and not to z. If z has only one child x we make x the child of z’s parent instead of z. If z has 2 children we delete y the successor of z and insert it’s value into z. We can delete y since it has no left son why?
5
BST Delete TREE_DELETE (tree T, node z) if left(z)=null or right(z)=null –child left(z) –If child = null child =right(z) –If z = left( parent (z) ) left( parent ( z ) ) child –Else right( parent ( z ) ) child –delete z else –y successor(z) –value(z) = value(y) –TREE_DELETE(T,y)
6
defenitions d(u,v)-The distance between u and v two nodes in a tree is the number of edges in the path from u to v. a b e c d f d(a,f)=3
7
defenitions D(T)-The diameter of a tree d is max(d(u,v)) for all pairs of nodes u,v in the tree. a b e c d f D(T)=d(a,e)=4
8
Finding the diameter of a binary tree Diameter(Node n) –If n = null Return (-1,-1) –(Left_h, Left_d) = Diameter(left(n)) –(Right_h, Right_d) = Diameter(right(n)) –Return (max(Left_h, Right_h)+1, max(Right_d, Left_d, Left_h+ Right_h+2))
9
Finding the diameter of a binary tree a b e c d f 0,0 1, 1 4,4 2,3 0,0 3,3
10
Complexity Diameter(Node n) –If n = null Return (-1,-1) –(Left_h, Left_d) = Diameter(left(n)) –(Right_h, Right_d) = Diameter(right(n)) –Return (max(Left_h, Right_h)+1, max(Right_d, Left_d, Left_h+ Right_h+2) O(N) Visits every node in the tree.
11
Correctness proof intuition The algorithm is correct since the diameter of the tree is the maximum between: –The diameter of the right subtree. –The diameter of the left subtree. –The distance from the root to the most deep left leaf + the root to the most deep right leaf. (this is the sum of the heights of he right and left subtrees +2).
12
AVL tree In an AVL tree, the heights of the two child subtrees of any node differ by at most one 5 73 52 8
13
AVL with height Each node in the tree holds it’s height. 5/2 7/13/1 5/02/0 8/0
14
Finding the diameter of an AVL with height Diameter(Node n) –If n = null Return 0 –Else return (right(n).height + left(n).height+2).
15
Correctness proof Claim-the diameter of a tree of height h is at most 2h. Proof by induction on h: Basis: h=0 (diameter is 0). Assume for h-1 we will prove for h.
16
Correctness proof –If the diameter is the diameter of the right or left subtree. Than from the induction assumption we know that it is at most 2(h-1)<2h. –If the diameter is sum of the heights of the right and left subtrees +2 the diameter is at most 2h. Since the maximal height of the subtrees is h-1. D(T)=2(h-1)+2=2h.
17
Correctness proof We will want to show that in an AVL the sum of the heights of the right and left subtrees +2 is larger than the diameter of the right and left subtree.
18
Correctness proof From the claim we know that the diameter of both children is at most 2(h-1). The sum of the heights of the subtrees is at least (h-1)+(h-2). So we see that h-1+h-2+2=2h-1>2(h-1).
19
Unbalanced trees-Question from exercise 2 The recurrence of this algorithm is
20
Unbalanced trees The recurssion tree The height of the tree is log 5/4 n since it is equal to x in the equation n(4/5) x =1. why? Why not n(3/5) x =1?
21
Unbalanced trees
22
Unbalanced trees
23
Unbalanced trees From the induction assumption we know level l does at most cn 2 work. So level l+1 does at most cn 2 work since it contains only pairs of children of nodes from level l.
24
Unbalanced trees We will want to prove that T(n)=O(n 2 logn) So we will prove by induction that T(n)<dnlogn.
25
Unbalanced trees Basis: T(0)=O(1). We will assume for m<n and prove for n.
26
Unbalanced trees This is true if -dn 2 (log(5)- (9/25)log(3)-(16/25)log(4))+cn 2 <0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.