Binary Search Tree - PowerPoint PPT Presentation

About This Presentation
Title:

Binary Search Tree

Description:

Binary Search Tree by Sean Long 70 43 27 1 11 42 32 58 51 68 60 111 99 90 PostOrder Traversal 1.Visit the left sub-tree 2.Visit the right sub-tree – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 93
Provided by: long53
Category:
Tags: binary | search | tree

less

Transcript and Presenter's Notes

Title: Binary Search Tree


1
Binary Search Tree
  • A running demonstration of binary search data
    structure and algorithms

2
87
Insert
First value is the 'Root' of the tree.
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
87
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
11
87
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
12
87
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
13
87
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
14
87
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
15
87
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
16
87
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
17
87
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
18
87
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
19
87
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
20
87
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
21
87
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
22
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
23
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
24
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
25
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
ltFpredgtstarting valuelt/Fpredgt to data file
26
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
27
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
28
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
29
PreOrder Traversal
30
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.V
isit the right sub-tree
31
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.V
isit the right sub-tree
32
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.V
isit the right sub-tree
33
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.V
isit the right sub-tree
34
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.V
isit the right sub-tree
35
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.V
isit the right sub-tree
36
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.V
isit the right sub-tree
37
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.V
isit the right sub-tree
38
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.V
isit the right sub-tree
39
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.V
isit the right sub-tree
40
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.V
isit the right sub-tree
41
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.V
isit the right sub-tree
42
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.V
isit the right sub-tree
43
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.V
isit the right sub-tree
44
InOrder Traversal
45
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.V
isit the right sub-tree
46
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.V
isit the right sub-tree
47
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.V
isit the right sub-tree
48
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.V
isit the right sub-tree
49
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.V
isit the right sub-tree
50
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.V
isit the right sub-tree
51
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.V
isit the right sub-tree
52
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.V
isit the right sub-tree
53
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.V
isit the right sub-tree
54
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.V
isit the right sub-tree
55
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.V
isit the right sub-tree
56
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.V
isit the right sub-tree
57
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.V
isit the right sub-tree
58
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.V
isit the right sub-tree
59
PostOrder Traversal
60
70
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
61
70
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
62
70
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
63
70
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
64
70
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
65
70
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
66
70
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
67
70
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
68
70
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
69
70
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
70
70
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
71
70
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
72
70
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
73
70
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
74
Breadth First Traversal
75
Visit
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
76
70
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
77
70
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
78
70
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
79
70
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
80
70
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
81
70
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
82
70
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
83
70
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
84
70
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
85
70
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
86
70
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
87
70
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
88
70
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
89
Size
90
1
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
91
Height
92
70
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
Write a Comment
User Comments (0)
About PowerShow.com