Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis of Algorithms

Similar presentations


Presentation on theme: "Analysis of Algorithms"— Presentation transcript:

1 Analysis of Algorithms
Chapter - 05 Binary Search Tree

2 Binary Search Trees (Chapter 13[12])

3 Preorder, Inorder, Postorder Walks
(Chapter 13[12].1)

4 Querying a Binary Search Tree
(Section 13[12].2)

5 Min, Max, Successor in a Binary Search Tree
(Section 13[12].2)

6 Insertion in a Binary Search Tree
(Section 13[12].3) Insert`Note: Tree-Insert begins at the root of the tree and traces a path downward. The pointer x traces the path, and the pointer y is maintained as the parent of x. after initialization, the while loop in line 3-7 causes these two pointer to move down the tree, going left or right depending on the comparison of key[z] with key[x], until x is set to NIL. This NIL occupies the position where we wish to place the input item z. lines 8-13 set the pointers that cause z to be inserted. It runs in O(h) time on a tree of height h.

7 Deletion in a Binary Search Tree
(Section 13[12].3) Deleting a node z from a binary search tree. In each case, the node actually removed is lightly shaded. (a) If z has no children, we just remove it.

8 Deletion in a Binary Search Tree
(Section 13[12].3) Deleting a node z from a binary search tree. In each case, the node actually removed is lightly shaded. (b) If z has only one child, we splice out z.

9 Deletion in a Binary Search Tree
(Section 13[12].3) Deleting a node z from a binary search tree. In each case, the node actually removed is lightly shaded. (c) If z has two children, we splice out its successor y, which has at most one child, and then replace the contents of z with the contents of y.

10 Tree-Delete(T,z) If left[z] =nil[T] or right[z]=nil[T] then yz else yTree-Successor(z) If left[y]nil[T] then xleft[y] else xright[y] P[x]p[y] If p[y]=nil[T] then root[T]x else if y=left[p[y]] then left[p[y]]x else right[p[y]] x If y z then key[z] key[y] {{if y has other fields, copy them, too. Return y

11 Tree Traversal

12

13

14

15 Trees - Searching

16 Trees - Searching

17 Trees - Searching

18 Trees - Searching

19 Trees - Searching

20 Left-, Right-Rotate: Implementation


Download ppt "Analysis of Algorithms"

Similar presentations


Ads by Google