Binary Search Trees Lecture 5 1
Binary search tree sort 2
Binary-search-tree property 3
Binary Search Tree can be implemented as a linked data structure in which each node is an object with three pointer fields. The three pointer fields left, right and p point to the nodes corresponding to the left child, right child and the parent respectively. NIL in any pointer field signifies that there exists no corresponding child or parent. The root node is the only node in the BTS structure with NIL in its p field. 4
Binary-search-tree property 5
Inorder-tree walk During this type of walk, we visit the root of a subtree between the left subtree visit and right subtree visit. 6
Preorder-tree walk In this case we visit the root node before the nodes in either subtree. 7
Postorder-tree walk We visit the root node after the nodes in its subtrees. 8
Sorting by binary-search-tree NIL 3PRINT 2 4 NIL 3 PRINT NIL 3PRINT 5 4NIL 2 1 2NIL 3PRINT 8 4NIL 5 3 PRINT NIL 3PRINT
Sorting by binary-search-tree 10
Sorting by binary-search-tree (contnd.) 11
Searching for a key Search for 13 12
Searching for a key 13
Searching for minimum Minimum 14
Searching for minimum 15
Searching for maximum Maximum 16
Searching for maximum 17
Searching for successor The successor of a node x is the node with the smallest key greather than key [ x ] 18
Searching for successor Successor of 15 Case 1: The right subtree of node x is nonempty 19
Searching for successor Successor of 13 Case 2: The right subtree of node x is empty 20
Searching for successor Successor of 4 search until y.left = x Case 2: The right subtree of node x is empty 21
Searching for successor 22 Go upper until y.left = x
Insertion Inserting an item with key 13 23
Insertion 24
Deletion Deleting an item with key 13 (z has no children) z
Deletion Deleting an item with key 13 (z has no children)
Deletion z Deleting a node with key 16 (z has only one child )
Deletion Deleting a node with key 16 (z has only one child )
Deletion Deleting a node with key 5 (6 successor of 5) (z has two children) 10 z y
Deletion Deleting a node with key 5 (z has two children) 10 z y
Deletion Deleting a node with key 5 (z has two children)
Deletion 32