09 binary trees

31
Analysis of Algorithms Binary Trees Andres Mendez-Vazquez August 26, 2014 1 / 15

Upload: andres-mendez-vazquez

Post on 17-Jul-2015

43 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: 09 binary trees

Analysis of AlgorithmsBinary Trees

Andres Mendez-Vazquez

August 26, 2014

1 / 15

Page 2: 09 binary trees

Images/cinvestav-1.jpg

Outline

1 Binary search treesBinary search trees: ConceptsBinary search trees: Operations

2 Excercises

2 / 15

Page 3: 09 binary trees

Images/cinvestav-1.jpg

Outline

1 Binary search treesBinary search trees: ConceptsBinary search trees: Operations

2 Excercises

3 / 15

Page 4: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Concepts

DefinitionA binary search tree (BST) is a data structure where each node possesthree fields left, right and p.

They represent its left child, right child and parent.In addition, each node has the field key.

PropertyLet x be a node in a binary search tree. If y is a node in the left subtree ofx , then key [y ] ≤ key [x ]. Similarly, if y is a node in the right subree of x ,then key [x ] ≤ key [y ].

4 / 15

Page 5: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Concepts

DefinitionA binary search tree (BST) is a data structure where each node possesthree fields left, right and p.

They represent its left child, right child and parent.In addition, each node has the field key.

PropertyLet x be a node in a binary search tree. If y is a node in the left subtree ofx , then key [y ] ≤ key [x ]. Similarly, if y is a node in the right subree of x ,then key [x ] ≤ key [y ].

4 / 15

Page 6: 09 binary trees

Images/cinvestav-1.jpg

Outline

1 Binary search treesBinary search trees: ConceptsBinary search trees: Operations

2 Excercises

5 / 15

Page 7: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

This property allows to print the keys in sorted order!Inorder-tree-walk(x)

1 if x 6= NIL2 Inorder-tree-walk(x .left)3 print x .key4 Inorder-tree-walk(x .right)

6 / 15

Page 8: 09 binary trees

Images/cinvestav-1.jpg

Cost of inorder walk

Theorem 12.1If x is the root of an n-node subtree, then the call Inorder-tree-walk(x)takes Θ (n) time.

Proof:Let T (n) denote the time taken by Inorder-tree-walk(x) when called at theroot.

FirstSince Inorder-tree-walk(x) visit all the nodes then we have thatT (n) = Ω (n).Thus, you need to prove T (n) = O (n)?

7 / 15

Page 9: 09 binary trees

Images/cinvestav-1.jpg

Cost of inorder walk

Theorem 12.1If x is the root of an n-node subtree, then the call Inorder-tree-walk(x)takes Θ (n) time.

Proof:Let T (n) denote the time taken by Inorder-tree-walk(x) when called at theroot.

FirstSince Inorder-tree-walk(x) visit all the nodes then we have thatT (n) = Ω (n).Thus, you need to prove T (n) = O (n)?

7 / 15

Page 10: 09 binary trees

Images/cinvestav-1.jpg

Cost of inorder walk

Theorem 12.1If x is the root of an n-node subtree, then the call Inorder-tree-walk(x)takes Θ (n) time.

Proof:Let T (n) denote the time taken by Inorder-tree-walk(x) when called at theroot.

FirstSince Inorder-tree-walk(x) visit all the nodes then we have thatT (n) = Ω (n).Thus, you need to prove T (n) = O (n)?

7 / 15

Page 11: 09 binary trees

Images/cinvestav-1.jpg

Proof of inorder walk

FirstFor n = 0, the method takes a constant time T (0) = c for some c > 0.

Now for n > 0We have the following situation:

1 Left subtree has k nodes2 Right subtree has n − k − 1 nodes

8 / 15

Page 12: 09 binary trees

Images/cinvestav-1.jpg

Proof of inorder walk

FirstFor n = 0, the method takes a constant time T (0) = c for some c > 0.

Now for n > 0We have the following situation:

1 Left subtree has k nodes2 Right subtree has n − k − 1 nodes

8 / 15

Page 13: 09 binary trees

Images/cinvestav-1.jpg

Substitution MethodWe have finally

T (n) = T (k) + T (n − k − 1) + d

1 T (k) is the amount of work done in the left2 T (n − k − 1) is the amount of work done in the right3 d > 0 reflects an upper bound for the in-between work done for the

print.

We use the substitution method to prove that T (n) = O(n)This can be done if we can bound T (n) by bounding it by

(c + d)n + c (1)

Thus for n = 0

T (0) = c = (c + d)× 0 + c (2)9 / 15

Page 14: 09 binary trees

Images/cinvestav-1.jpg

Substitution MethodWe have finally

T (n) = T (k) + T (n − k − 1) + d

1 T (k) is the amount of work done in the left2 T (n − k − 1) is the amount of work done in the right3 d > 0 reflects an upper bound for the in-between work done for the

print.

We use the substitution method to prove that T (n) = O(n)This can be done if we can bound T (n) by bounding it by

(c + d)n + c (1)

Thus for n = 0

T (0) = c = (c + d)× 0 + c (2)9 / 15

Page 15: 09 binary trees

Images/cinvestav-1.jpg

Substitution MethodWe have finally

T (n) = T (k) + T (n − k − 1) + d

1 T (k) is the amount of work done in the left2 T (n − k − 1) is the amount of work done in the right3 d > 0 reflects an upper bound for the in-between work done for the

print.

We use the substitution method to prove that T (n) = O(n)This can be done if we can bound T (n) by bounding it by

(c + d)n + c (1)

Thus for n = 0

T (0) = c = (c + d)× 0 + c (2)9 / 15

Page 16: 09 binary trees

Images/cinvestav-1.jpg

Substitution Method

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 − (c + d) + c + d= (c + d) n + c

Thus

T (n) = Θ (n) (3)

10 / 15

Page 17: 09 binary trees

Images/cinvestav-1.jpg

Substitution Method

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 − (c + d) + c + d= (c + d) n + c

Thus

T (n) = Θ (n) (3)

10 / 15

Page 18: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

SearchingTree-search(x , k)

1 if x == NIL or k == x .key2 return x3 if k < x .key4 return Tree-search(x .left, k)

5 else return Tree-search(x .right, k)

Complexity

O (h) (4)

where h is the height of the tree ⇒ we look for well balanced trees.

11 / 15

Page 19: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

SearchingTree-search(x , k)

1 if x == NIL or k == x .key2 return x3 if k < x .key4 return Tree-search(x .left, k)

5 else return Tree-search(x .right, k)

Complexity

O (h) (4)

where h is the height of the tree ⇒ we look for well balanced trees.

11 / 15

Page 20: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

Minimum and maximumTree-minimum(x)

1 while x .left 6=NIL2 x = x .left3 return x

Complexity

O (h) (5)

where h is the height of the tree ⇒ we look for well balanced trees.

12 / 15

Page 21: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

Minimum and maximumTree-minimum(x)

1 while x .left 6=NIL2 x = x .left3 return x

Complexity

O (h) (5)

where h is the height of the tree ⇒ we look for well balanced trees.

12 / 15

Page 22: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

TREE-DELETE(T , z)1 if z.left == NIL2 Transplant(T , z, z.right)3 elseif z.right == NIL4 Transplant(T , z, z.left)5 else6 y=Tree-minimum(z.right)7 if y .p 6= z8 Transplant(T , y , y .right)9 y .right = z.right10 y .right.p = y11 Transplant(T , z, y)12 y .left = z.left

13 y .left.p = y

Case 1Basically if the element z to bedeleted has a NIL left childsimply replace z with thatchild!!!

13 / 15

Page 23: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

TREE-DELETE(T , z)1 if z.left == NIL2 Transplant(T , z, z.right)3 elseif z.right == NIL4 Transplant(T , z, z.left)5 else6 y=Tree-minimum(z.right)7 if y .p 6= z8 Transplant(T , y , y .right)9 y .right = z.right10 y .right.p = y11 Transplant(T , z, y)12 y .left = z.left

13 y .left.p = y

Case 2Basically if the element z to bedeleted has a NIL right childsimply replace z with thatchild!!!

13 / 15

Page 24: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

TREE-DELETE(T , z)1 if z.left == NIL2 Transplant(T , z, z.right)3 elseif z.right == NIL4 Transplant(T , z, z.left)5 else6 y=Tree-minimum(z.right)7 if y .p 6= z8 Transplant(T , y , y .right)9 y .right = z.right10 y .right.p = y11 Transplant(T , z, y)12 y .left = z.left

13 y .left.p = y

Case 3The z element has not emptychildren you need to find thesuccessor of it.

13 / 15

Page 25: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

TREE-DELETE(T , z)1 if z.left == NIL2 Transplant(T , z, z.right)3 elseif z.right == NIL4 Transplant(T , z, z.left)5 else6 y=Tree-minimum(z.right)7 if y .p 6= z8 Transplant(T , y , y .right)9 y .right = z.right10 y .right.p = y11 Transplant(T , z, y)12 y .left = z.left

13 y .left.p = y

Case 4if y .p 6= z then y .right takes theposition of y after all y .left ==NIL

I take z .right and make it thenew right of y

I make the(y .right == z .right).p equalto y

13 / 15

Page 26: 09 binary trees

Images/cinvestav-1.jpg

Binary search trees: Operations

TREE-DELETE(T , z)1 if z.left == NIL2 Transplant(T , z, z.right)3 elseif z.right == NIL4 Transplant(T , z, z.left)5 else6 y=Tree-minimum(z.right)7 if y .p 6= z8 Transplant(T , y , y .right)9 y .right = z.right10 y .right.p = y11 Transplant(T , z, y)12 y .left = z.left

13 y .left.p = y

Case 4put y in the position of zmake y .left equal to z .leftmake the (y .left == z .left).pequal to y

13 / 15

Page 27: 09 binary trees

Images/cinvestav-1.jpg

Support Operations

Transplant(T , u, v)1 if u.p == NIL2 T .root = v3 elseif u == u.p.left4 u.p.left = v5 else u.p.right = v6 if v 6=NIL

7 v .p = u.p

Case 1If u is the root then make theroot equal to v

14 / 15

Page 28: 09 binary trees

Images/cinvestav-1.jpg

Support Operations

Transplant(T , u, v)1 if u.p == NIL2 T .root = v3 elseif u == u.p.left4 u.p.left = v5 else u.p.right = v6 if v 6=NIL

7 v .p = u.p

Case 2if u is the left child make theleft child of the parent of uequal to v

14 / 15

Page 29: 09 binary trees

Images/cinvestav-1.jpg

Support Operations

Transplant(T , u, v)1 if u.p == NIL2 T .root = v3 elseif u == u.p.left4 u.p.left = v5 else u.p.right = v6 if v 6=NIL

7 v .p = u.p

Case 3Similar to the second case, butfor right child

14 / 15

Page 30: 09 binary trees

Images/cinvestav-1.jpg

Support Operations

Transplant(T , u, v)1 if u.p == NIL2 T .root = v3 elseif u == u.p.left4 u.p.left = v5 else u.p.right = v6 if v 6=NIL

7 v .p = u.p

Case 4If v 6= NIL then make the parentof v the parent of u

14 / 15

Page 31: 09 binary trees

Images/cinvestav-1.jpg

Excercises

From Cormen’s book, chapters 11 and 1211.1-211.2-111.2-211.2-311.3-111.3-312.1-312.1-512.2-512.2-712.2-912.3-3

15 / 15