Binary Search Tree in Data Structure

39,942 views 126 slides Jul 29, 2015
Slide 1
Slide 1 of 126
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126

About This Presentation

Content of slide
Tree
Binary tree Implementation
Binary Search Tree
BST Operations
Traversal
Insertion
Deletion
Types of BST
Complexity in BST
Applications of BST


Slide Content

Click to add Title e-Infochips Institute of Training Research and Academics Limited Binary Search Tree Guided By:- Mrs. Darshana Mistry Presented By:- Dharita Chokshi Disha Raval Himani Patel

Outlines Tree Binary tree Implementation Binary Search Tree BST Operations Traversal Insertion Deletion Types of BST Complexity in BST Applications of BST

Trees Tree Each node can have 0 or more children A node can have at most one parent Binary tree Tree with 0–2 children per node Also known as Decision Making Tree

Trees Terminology Root  no parent Leaf  no child Interior  non-leaf Height  distance from root to leaf (H)

Why is h important? The Tree operations like insert, delete, retrieve etc. are typically expressed in terms of the height of the tree h . So, it can be stated that the tree height h determines running time!

Binary Tree Implementation Class Node { int data ; // Could be int, a class, etc Node * left , * right ; // null if empty void insert ( int data ) { … } void delete ( int data ) { … } Node * find ( int data ) { … } … }

Binary Search Tree Key property is v alue at node Smaller values in left subtree Larger values in right subtree Example X > Y X < Z Y X Z

Binary Search Tree Examples Binary search trees Not a binary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45

Difference between BT and BST A binary tree is simply a tree in which each node can have at most two children. A binary search tree is a binary tree in which the nodes are assigned values, with the following restrictions : No duplicate values. The left subtree of a node can only have values less than the node The right subtree of a node can only have values greater than the node and recursively defined The left subtree of a node is a binary search tree. The right subtree of a node is a binary search tree.

Binary Tree Search Algorithm TREE-SEARCH( x,k ) If x==NIL or k==x.key return x If k < x.key return TREE-SEARCH( x.left,k ) else return TREE-SEARCH( x.right,k )

BST Operations F our basic BST operations 1 2 3 4 Traversal Search Insertion Deletion

BST Traversal

Preorder Traversal 23 18 12 20 44 35 52 Root Left Right

Postorder Traversal 12 20 18 35 52 44 23 Left Right Root

Inorder Traversal 12 18 20 23 35 44 52 Produces a sequenced list Left Root Right

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 8 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 8 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 8 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 8 10 Inorder Traversal

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 1 2 3 4 5 6 8 10 Inorder Traversal

Binary Tree Insertion

1 10 8 4 6 3 2 5 Binary Tree Insertion

1 10 8 4 6 3 2 5 Binary Tree Insertion

1 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 Binary Tree Insertion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 Binary Tree Insertion

Binary Tree Deletion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 2 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 2 5 8 4 6 3 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 5 8 4 6 3 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 5 8 4 6 3 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 5 8 4 6 3 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 8 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 4 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 5 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 5 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 5 6 3 5 Binary Tree Deletion

1 10 1 10 4 6 3 5 5 6 3 Binary Tree Deletion

1 10 1 10 4 6 3 5 5 6 3 Binary Tree Deletion

1 10 1 10 6 3 5 5 6 3 Binary Tree Deletion

Types of BST

AVL Tree AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes .

Red Black Tree Every node has a color either red or black. Root of tree is always black. There are no two adjacent red nodes (A red node cannot have a red parent or red child ). Every path from root to a NULL node has same number of black nodes.

Splay Tree Automatically moves frequently accessed elements nearer to the root for quick to access

Complexity in BST Operation Average Worst Case Best Case Search O(log n) O(n) O(1) Insertion O(log n) O(n) O(1) Deletion O(log n) O(n) O(1)

Applications of BST Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries . Storing a set of names, and being able to lookup based on a prefix of the name. (Used in internet routers.) Storing a path in a graph, and being able to reverse any subsection of the path in O(log n) time . ( Useful in travelling salesman problems). Finding square root of given number allows you to do range searches efficiently.

Thank you