200 likes | 225 Vues
Learn the intricacies of Binary Search Trees - from Preorder, Inorder, and Postorder Walks to Min, Max, and Successor queries, and efficient Insertion and Deletion algorithms. Dive into Tree Traversal and understand the process of Searching and Rotating nodes in a BST.
E N D
Analysis of Algorithms Chapter - 05 Binary Search Tree
Binary Search Trees (Chapter 13[12])
Preorder, Inorder, Postorder Walks (Chapter 13[12].1)
Querying a Binary Search Tree (Section 13[12].2)
Min, Max, Successor in a Binary Search Tree (Section 13[12].2)
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.
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.
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.
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.
Tree-Delete(T,z) • If left[z] =nil[T] or right[z]=nil[T] • then yz • else yTree-Successor(z) • If left[y]nil[T] • then xleft[y] • else xright[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