Title: Binary Search Tree
1Binary Search Tree
- A running demonstration of binary search data
structure and algorithms
287
Insert
First value is the 'Root' of the tree.
387
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
487
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
587
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
687
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
787
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
887
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
987
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
1087
50
111
27
99
58
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
1187
50
111
27
99
58
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
1287
50
111
27
99
58
42
90
105
68
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
1387
50
111
27
99
58
42
90
105
68
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
1487
50
111
27
99
58
42
90
105
68
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
1587
50
111
27
99
58
42
90
105
68
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
1687
50
111
27
99
58
42
90
105
68
51
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
1787
50
111
27
99
58
42
90
105
68
51
1
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
1887
50
111
27
99
58
42
90
105
68
51
1
32
43
60
70
11
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
1987
50
111
27
99
58
42
90
105
68
51
1
32
43
60
70
11
Predecessor
The node to delete is 50
Predecessor is found by finding the right most
node of the 50 nodes left sub-tree
2087
50
111
27
99
58
42
90
105
68
51
1
32
43
60
70
11
Sucsessor
Successor is found by finding the left most node
of the 50 nodes right sub-tree
2187
50
111
27
99
58
42
90
105
68
51
1
32
43
60
70
11
How to delete 50?
There is a left child so we find predecessor
To learn more about finding predecessor insert
ltFpredgtstarting valuelt/Fpredgt to data file
2287
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
2387
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
2487
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
2587
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
ltFpredgtstarting valuelt/Fpredgt to data file
2670
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
2770
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
2870
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
29PreOrder Traversal
3070
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.V
isit the right sub-tree
3170
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.V
isit the right sub-tree
3270
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.V
isit the right sub-tree
3370
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.V
isit the right sub-tree
3470
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.V
isit the right sub-tree
3570
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.V
isit the right sub-tree
3670
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.V
isit the right sub-tree
3770
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.V
isit the right sub-tree
3870
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.V
isit the right sub-tree
3970
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.V
isit the right sub-tree
4070
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.V
isit the right sub-tree
4170
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.V
isit the right sub-tree
4270
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.V
isit the right sub-tree
4370
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.V
isit the right sub-tree
44InOrder Traversal
4570
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.V
isit the right sub-tree
4670
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.V
isit the right sub-tree
4770
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.V
isit the right sub-tree
4870
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.V
isit the right sub-tree
4970
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.V
isit the right sub-tree
5070
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.V
isit the right sub-tree
5170
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.V
isit the right sub-tree
5270
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.V
isit the right sub-tree
5370
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.V
isit the right sub-tree
5470
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.V
isit the right sub-tree
5570
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.V
isit the right sub-tree
5670
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.V
isit the right sub-tree
5770
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.V
isit the right sub-tree
5870
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.V
isit the right sub-tree
59PostOrder Traversal
6070
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6170
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6270
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6370
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6470
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6570
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6670
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6770
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6870
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
6970
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
7070
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
7170
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
7270
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
7370
43
111
27
58
99
1
42
51
68
90
11
32
60
PostOrder Traversal
1.Visit the left sub-tree 2.Visit the right sub
-tree
3.Visit the root
74Breadth First Traversal
75Visit
70
43
111
27
58
99
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
7670
Visit
43
111
27
58
99
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
7770
Visit
43
111
27
58
99
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
7870
43
111
Visit
27
58
99
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
7970
43
111
Visit
27
58
99
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8070
43
111
Visit
27
58
99
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8170
43
111
27
58
99
Visit
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8270
43
111
27
58
99
Visit
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8370
43
111
27
58
99
Visit
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8470
43
111
27
58
99
Visit
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8570
43
111
27
58
99
Visit
1
42
51
68
90
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8670
43
111
27
58
99
1
42
51
68
90
Visit
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8770
43
111
27
58
99
1
42
51
68
90
Visit
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
8870
43
111
27
58
99
1
42
51
68
90
Visit
11
32
60
Breadth First Traversal
1.Visit the root 2.Visit both Children 3.Visit
Their Children
89Size
901
2
12
3
8
13
4
6
9
10
14
5
7
11
Size
Size is the amount of nodes in a tree
Size 14
91Height
9270
1
43
111
2
27
58
99
3
1
42
51
68
90
4
11
32
60
5
Height
Hight is the number of Levels between
the root and lowest leaf
Height 5