Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 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 7 30
Deletion Deleting a node with key 5 (z has two children)
Deletion 32
Analysis of BST sort The expected time to built the tree is asymptotically the same as the running time of quicksort 33
Red Black Trees 34
Balanced search trees 35
Red-black trees 36
Example of a red-black tree 37
Example of a red-black tree 38
Example of a red-black tree 39
Example of a red-black tree 40
Example of a red-black tree 41
Height of a red-black tree 42
Height of a red-black tree 43
Height of a red-black tree 44
Height of a red-black tree 45
Height of a red-black tree 46
Height of a red-black tree 47
Proof (continued) 48
Query operations 49
Modifying operations 50
Rotations 51
Insertion into a red-black tree 52
Insertion into a red-black tree 53
Insertion into a red-black tree 54
Insertion into a red-black tree 55
Insertion into a red-black tree 56
Pseudocode 57
Graphical notation 58
Case 1 59
Case 2 60
Case 3 61
Analysis 62