Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Definition Of Binary Search Tree

• A binary tree.
• Each node has a (key, value) pair.
• For every node x, all keys in the left
subtree of x are smaller than that in x.
• For every node x, all keys in the right
subtree of x are greater than or equal to
that in x.
Example Binary Search Tree
20

10 40

6 15 30

2 8 25
Insertion in BST
20

10 40

6 15 30

2 8 25 35

Insert 35.
Insertion in BST
20

10 40

6 15 30

2 8 25 35

7 Insert 7
Insertion in BST
20

10 40

6 15 30

18 25 35
2 8
Insert 18
7
Creation of BST

Ques 1:

Ques 2:
Creation of BST
Search (Root, key)

/* return a pointer to the node that


contains key If there is no such contains key. If there is no such
node, return NULL */

Step 1. If ( Root = = NULL)


return NULL
End If
Step 2. If (key = = Root.Data)
return Root
End If
Step 3. If (key < Root.Data)
return search (Root.Left, key)
Else
return search (Root.Right, key)
End If
Step 4. Exit
Deletion in BST

Three cases:
Element is a leaf.
 Element is a degree 1 node.
 Element is a degree 2 node.
Delete From A Leaf
20

10 40

6 15 30

18 25 35
2 8

7 Delete a leaf element 7


Delete From A Leaf (contd.)
20

10 40

6 15 30

18 25 35
2 8

7
Delete a leaf element 35
Delete From A Degree 1 Node

20

10 40

6 15 30

18 25 35
2 8

7 Delete 40
Delete From A Degree 1 Node (contd.)

20

10 40

6 15 30

18 25 35
2 8

7 Delete 15
Delete From A Degree 2 Node

20

10 40

6 15 30

18 25 35
2 8

7
Delete 10
Delete From A Degree 2 Node
20

10 40

6 15 30

18 25 35
2 8

Replace with largest key in left subtree (or


smallest in right subtree).
Delete From A Degree 2 Node
20

10 40

6 15 30

18 25 35
2 8

Replace with largest key in left subtree (or


smallest in right subtree).
Delete From A Degree 2 Node

20

8 40

6 15 30

18 25 35
2 8

Replace with largest key in left subtree (or


smallest in right subtree).
Delete From A Degree 2 Node
20

8 40

6 15 30

18 25 35
2 8

7
Another Delete From A Degree 2 Node

20

10 40

6 15 30

18 25 35
2 8

Delete 20
Delete From A Degree 2 Node
20

10 40

6 15 30

18 25 35
2 8

Replace with largest in left subtree.


Delete From A Degree 2 Node
20

10 40

6 15 30

18 25 35
2 8

Replace with largest in left subtree.


Delete From A Degree 2 Node

18

10 40

6 15 30

18 25 35
2 8

Replace with largest in left subtree.


Insertion BST (Root, data)

1.Create New node, temp.


temp.data = data
temp.left = NULL
temp.right = NULL

2. // If tree is empty
If (Root = = NULL)
Root = temp
Else
a. current = Root
b. parent = NULL
c. Repeat while(1) do
parent = current
If (data < parent.data) then //go to left of the tree
current = current.left
//insert to the left
If(current = = NULL) then
parent.left= temp;
return;
Else //go to right of the tree

current = current.right
//insert to the right
If (current = = NULL) then
parent.right = temp
return
End If
EndIf
End Loop
insert (node, data)
Applications of Binary Search Trees

1.Databases: Many databases use BSTs to store and search for data. For example, MySQL and
SQLite use BSTs to store indexes of the data they contain, making queries much faster.

2.File Systems: File systems use BSTs to store and organize files on a hard drive. For example,
the Windows NTFS file system uses BSTs to store the Master File Table (MFT), which
contains information about all the files on the hard drive.

3.Computer Networks: BSTs can be used to store routing tables in computer networks. This
allows routers to quickly find the best route for a packet to take from one network to another.

4.Sorting: BSTs can be used as a sorting algorithm. By inserting elements in a BST and then
traversing it in-order, the elements can be sorted in O(nlogn) time.

5.Language Modeling: BSTs can be used to store n-grams in natural language processing
tasks. This allows for efficient searching of the most common n-grams, making language
modeling tasks faster.

6.Machine Learning: BSTs can be used in decision tree-based machine learning algorithms,
where the decision tree is a binary search tree that makes decisions based on the input features.

7.Game Trees: BSTs can be used to represent game trees in game theory, allowing for efficient
searching of possible game states and moves.

You might also like