Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk
Binary search tree sort
Binary-search-tree property
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.
Binary-search-tree property
Inorder-tree walk During this type of walk, we visit the root of a subtree between the left subtree visit and right subtree visit.
Preorder-tree walk In this case we visit the root node before the nodes in either subtree.
Preorder-tree walk We visit the root node after the nodes in its subtrees.
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
Sorting by binary-search- tree(contnd.)
Searching for a key Search for 13
Searching for a key
Searching for minimum Minimum
Searching for minimum
Searching for maximum Maximum
Searching for maximum
Searching for successor The successor of a node x is the node with the smallest key greather than key [ x ]
Searching for successor Successor of 15 Case 1: The right subtree of node x is nonempty
Searching for successor Successor of 13 Case 2: The right subtree of node x is empty
Searching for successor
Insertion Insering an item with key 13
Insertion
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 (z has two children) 10 z y 7 13
Deletion Deleting a node with key 5 (z has two children) 10 z y 7
Deletion Deleting a node with key 5 (z has two children)
Deletion
Analysis of BST sort The expected time to built the tree is asymptotically the same as the running time of quicksort
Hight of a randomly built binary search tree
Convex functions
Jensens’s inequality Proof: We assume that f has a Taylor expansion with µ=E(X).
Analysis of BST height
Analysis(contd.)
Exponential height recurrence
Solving the recurrence
Solving the recurrence (contnd.)