0% found this document useful (0 votes)
13 views23 pages

Binary Search Trees

The document provides an overview of Binary Search Trees (BST), explaining their structure and operations including search, insertion, and deletion. It highlights the advantages of BSTs over regular binary trees by ensuring that nodes in the left subtree contain smaller values and those in the right subtree contain larger values. The document also includes algorithms for searching, inserting, and deleting nodes in a BST, along with examples for better understanding.

Uploaded by

helper bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views23 pages

Binary Search Trees

The document provides an overview of Binary Search Trees (BST), explaining their structure and operations including search, insertion, and deletion. It highlights the advantages of BSTs over regular binary trees by ensuring that nodes in the left subtree contain smaller values and those in the right subtree contain larger values. The document also includes algorithms for searching, inserting, and deleting nodes in a BST, along with examples for better understanding.

Uploaded by

helper bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

TREES

BINARY SEARCH
TREES

By:-
CHARANPREET KAUR
BINARY SEARCH TREE
 In a binary tree, every node can have
maximum of two children but there is no
order of nodes based on their values.
 In binary tree, the elements are
arranged as they arrive to the tree, from
top to bottom and left to right.
 A binary tree has the following time
complexities...
 Search Operation - O(n)
 Insertion Operation - O(1)
 Deletion Operation - O(n)
 To enhance the performance of binary
tree, we use special type of binary
tree known as Binary Search Tree.
Binary search tree mainly focus on
the search operation in binary tree.
Binary search tree can be defined as
follows...
 In a binary search tree, all the nodes
in left subtree of any node contains
smaller values and all the nodes in
right subtree of that contains larger
values as shown in following figure...
EXAMPLE
 The following tree is a Binary Search Tree. In
this tree, left subtree of every node contains
nodes with smaller values and right subtree
of every node contains larger values.

 Every Binary Search Tree is a binary


tree but all the Binary Trees need not
to be binary search trees.
EXAMPLE
 Construct a Binary Search Tree by
inserting the following sequence of
numbers...
10,12,5,4,20,8,7,15 and 13
 Above elements are inserted into a Binary
Search Tree as follows...
OPERATIONS ON A BINARY SEARCH TREE
 The following operations are performed
on a binary search tree...
1. Search
2. Insertion
3. Deletion
SEARCHING A KEY
 Whenever an element is to be searched,
start searching from the root node. If the
key is present at root, we return root.
 Then if the data is less than the key value
of the root, search for the element in the
left subtree.
 Otherwise, search for the element in the
right subtree.
ALGORITHM:
Search for data in the tree N
1. If (if N is NULL or N contains data)
1. Return N
2. Else If (data greater than contents
of N)
1. Return the results of Searching the
N:right
3. Else If (data lesser than contents
of N)
1. Return the results of Searching the N:left
Illustration to search 6 in below tree:
1. Start from root.
2. Compare the inserting element with
root, if less than root, then recurse for
left, else recurse for right.
3. If element to search is found anywhere,
return true, else return false.
INSERTION OF A KEY
 A new key is always inserted at leaf.
 We start searching a key from root till we hit
a leaf node.
 Once a leaf node is found, the new node is
added as a child of the leaf node.
ALGORITHM:
1. If TPtr is NULL
1. Set TPtr= Create a Node with
contents data
2. Else If data is greater than
contents of TPtr
1. Set TPtr->Right= Insert (TPtr->Right,
data)
3. Else If data is lesser than contents
of TPtr
1. Set TPtr->Left= Insert (TPtr->Left,
data)
4. Return TPtr
Illustration to insert 2 in below tree:
1. Start from root.
2. Compare the inserting element with
root, if less than root, then recurse for
left, else recurse for right.
3. After reaching end,just insert that node
at left(if less than current) else right.
DELETING A KEY
 When we delete a node, three
possibilities arise:
 1) Node to be deleted is leaf: Simply
remove from the tree.
 2) Node to be deleted has only one
child: Copy the child to the node and
delete the child
3) Node to be deleted has two
children: Find the successor of the node to
be deleted i.e successor is the RC of the
node. Then find the extreme left child of the
successor, swap its content with the deleted
node.

 The important thing to note is, at any point of


deletion, the rules of the BST should not be
violated.
 Refer to the handwritten pdf
notes of trees for more examples
and algorithms.
 All he algos for the BST are very
clearly defined in the notes
given.

You might also like