930 likes | 1.07k Views
Binary Search Tree. A running demonstration of binary search data structure and algorithms. 87. Insert. First value is the 'Root' of the tree. 87. 50. Insert. Since 50 is Less than 87, we move to the left sub-tree. **Since no sub-tree, 50 is now added as the left leaf**. 87. 50. 27.
E N D
Binary Search Tree A running demonstration of binary search data structure and algorithms
87 Insert First value is the 'Root' of the tree.
87 50 Insert Since 50 is Less than 87, we move to the left sub-tree **Since no sub-tree, 50 is now added as the left leaf**
87 50 27 Insert Since 27 is Less than 87, we move to the left sub-tree Since 27 is Less than 50, we move to the left sub-tree **Since no sub-tree, 27 is now added as the left leaf**
87 50 111 27 Insert Since 111 is Greater than 87, we move to the right sub-tree **Since no sub-tree, 111 is now added as the right leaf**
87 50 111 27 99 Insert Since 99 is Greater than 87, we move to the right sub-tree Since 99 is Less than 111, we move to the left sub-tree **Since no sub-tree, 99 is now added as the left leaf**
87 50 111 27 99 42 Insert Since 42 is Less than 87, we move to the left sub-tree Since 42 is Less than 50, we move to the left sub-tree Since 42 is Greater than 27, we move to the right sub-tree **Since no sub-tree, 42 is now added as the right leaf**
87 50 111 27 99 42 90 Insert Since 90 is Greater than 87, we move to the right sub-tree Since 90 is Less than 111, we move to the left sub-tree Since 90 is Less than 99, we move to the left sub-tree **Since no sub-tree, 90 is now added as the left leaf**
87 50 111 27 99 42 90 105 Insert Since 105 is Greater than 87, we move to the right sub-tree Since 105 is Less than 111, we move to the left sub-tree Since 105 is Greater than 99, we move to the right sub-tree **Since no sub-tree, 105 is now added as the right leaf**
87 50 111 27 58 99 42 90 105 Insert Since 58 is Less than 87, we move to the left sub-tree Since 58 is Greater than 50, we move to the right sub-tree **Since no sub-tree, 58 is now added as the right leaf**
87 50 111 27 58 99 42 90 105 32 Insert Since 32 is Less than 87, we move to the left sub-tree Since 32 is Less than 50, we move to the left sub-tree Since 32 is Greater than 27, we move to the right sub-tree Since 32 is Less than 42, we move to the left sub-tree **Since no sub-tree, 32 is now added as the left leaf**
87 50 111 27 58 99 42 68 90 105 32 Insert Since 68 is Less than 87, we move to the left sub-tree Since 68 is Greater than 50, we move to the right sub-tree Since 68 is Greater than 58, we move to the right sub-tree **Since no sub-tree, 68 is now added as the right leaf**
87 50 111 27 58 99 42 68 90 105 32 43 Insert Since 43 is Less than 87, we move to the left sub-tree Since 43 is Less than 50, we move to the left sub-tree Since 43 is Greater than 27, we move to the right sub-tree Since 43 is Greater than 42, we move to the right sub-tree **Since no sub-tree, 43 is now added as the right leaf**
87 50 111 27 58 99 42 68 90 105 32 43 60 Insert Since 60 is Less than 87, we move to the left sub-tree Since 60 is Greater than 50, we move to the right sub-tree Since 60 is Greater than 58, we move to the right sub-tree Since 60 is Less than 68, we move to the left sub-tree **Since no sub-tree, 60 is now added as the left leaf**
87 50 111 27 58 99 42 68 90 105 32 43 60 70 Insert Since 70 is Less than 87, we move to the left sub-tree Since 70 is Greater than 50, we move to the right sub-tree Since 70 is Greater than 58, we move to the right sub-tree Since 70 is Greater than 68, we move to the right sub-tree **Since no sub-tree, 70 is now added as the right leaf**
87 50 111 27 58 99 42 51 68 90 105 32 43 60 70 Insert Since 51 is Less than 87, we move to the left sub-tree Since 51 is Greater than 50, we move to the right sub-tree Since 51 is Less than 58, we move to the left sub-tree **Since no sub-tree, 51 is now added as the left leaf**
87 50 111 27 58 99 1 42 51 68 90 105 32 43 60 70 Insert Since 1 is Less than 87, we move to the left sub-tree Since 1 is Less than 50, we move to the left sub-tree Since 1 is Less than 27, we move to the left sub-tree **Since no sub-tree, 1 is now added as the left leaf**
87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 Insert Since 11 is Less than 87, we move to the left sub-tree Since 11 is Less than 50, we move to the left sub-tree Since 11 is Less than 27, we move to the left sub-tree Since 11 is Greater than 1, we move to the right sub-tree **Since no sub-tree, 11 is now added as the right leaf**
87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 Predecessor The node to delete is 50 Predecessor is found by finding the right most node of the 50 nodes left sub-tree
87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 Sucsessor Successor is found by finding the left most node of the 50 nodes right sub-tree
87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 How to delete 50? There is a left child so we find predecessor To learn more about finding predecessor insert <Fpred>starting value</Fpred> to data file
87 43 111 27 58 99 1 42 51 68 90 105 11 32 60 70 Delete There is a left child so we find predecessor This is how the new tree would appear
87 43 111 27 58 99 1 42 51 68 90 105 11 32 60 70 How to delete 105? This is a Leaf case We just remove this node from the tree
87 43 111 27 58 99 1 42 51 68 90 11 32 60 70 Delete This is a Leaf case This is how the new tree would appear
87 43 111 27 58 99 1 42 51 68 90 11 32 60 70 How to delete 87? There is a left child so we find predecessor To learn more about finding predicessor insert <Fpred>starting value</Fpred> to data file
70 43 111 27 58 99 1 42 51 68 90 11 32 60 Delete There is a left child so we find predecessor This is how the new tree would appear
70 43 111 27 58 99 1 42 51 68 90 11 32 60 How to delete 111? There is only one child, so replace the node with its only child
70 43 99 27 58 90 1 42 51 68 11 32 60 After 111 is replaced by its only child There is only one child, so replace the node with its only child
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree
70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree