0% found this document useful (0 votes)
25 views

Binary and Binary Search Tree

Uploaded by

navdeep242006
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Binary and Binary Search Tree

Uploaded by

navdeep242006
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Binary and binary search tree

Data Structure
Binary tree
• Tree represents the nodes connected by edges. We
will discuss binary tree or binary search tree
specifically.
• Binary Tree is a special data structure used for data
storage purposes. A binary tree has a special
condition that each node can have a maximum of
two children. A binary tree has the benefits of both
an ordered array and a linked list as search is as
quick as in a sorted array and insertion or deletion
operation are as fast as in linked list.
Binary search tree
• A binary search tree follows some order to
arrange the elements. In a Binary search tree,
the value of left node must be smaller than
the parent node, and the value of right node
must be greater than the parent node. This
rule is applied recursively to the left and right
subtrees of the root.
In the above figure, we can observe that the root node
is 40, and all the nodes of the left subtree are smaller
than the root node, and all the nodes of the right
subtree are greater than the root node.
Advantages of Binary search tree

• Searching an element in the Binary search tree


is easy as we always have a hint that which
subtree has the desired element.
• As compared to array and linked lists,
insertion and deletion operations are faster in
BST.
Example of creating a binary search tree
• Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12,
20, 50
• First, we have to insert 45 into the tree as the root of the
tree.
• Then, read the next element; if it is smaller than the root
node, insert it as the root of the left subtree, and move to
the next element.
• Otherwise, if the element is larger than the root node,
then insert it as the root of the right subtree.
• Now, let's see the process of creating the Binary search
tree using the given data element.
Step 1 - Insert 45.

Step 2 - Insert 15.


As 15 is smaller than 45, so insert it as the root node of the
left subtree.
• Step 3 - Insert 79.
• As 79 is greater than 45, so insert it as the root node
of the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as
the right subtree of 79.
Step 5 - Insert 10.
10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.
• Step 6 - Insert 55.
55 is larger than 45 and smaller than 79, so it
will be inserted as the left subtree of 79.
• Step 7 - Insert 12.
• 12 is smaller than 45 and 15 but greater than
10, so it will be inserted as the right subtree of
10.
• Step 8 - Insert 20.
• 20 is smaller than 45 but greater than 15, so it
will be inserted as the right subtree of 15.
• Step 9 - Insert 50.
• 50 is greater than 45 but smaller than 79 and
55. So, it will be inserted as a left subtree of
55.
BST Basic Operations

• Insert − Inserts an element in a tree/create a


tree.
• Search − Searches an element in a tree.
• Preorder Traversal − Traverses a tree in a pre-
order manner.
• Inorder Traversal − Traverses a tree in an in-
order manner.
• Postorder Traversal − Traverses a tree in a post-
order manner.
Insert Operation

• The very first insertion creates the tree.


Afterwards, whenever an element is to be
inserted, first locate its proper location. Start
searching from the root node, then if the data
is less than the key value, search for the empty
location in the left subtree and insert the data.
Otherwise, search for the empty location in
the right subtree and insert the data.
Algorithm(Insert operation)

• If root is NULL
• then create root node
• return
• If root exists then
• compare the data with node.data
• while until insertion position is located
• If data is greater than node.data
• goto right subtree
• else
• goto left subtree
• endwhile
• insert data
• end If
Search Operation

• Whenever an element is to be searched, start


searching from the root node, then if the data
is less than the key value, search for the
element in the left subtree. Otherwise, search
for the element in the right subtree. Follow
the same algorithm for each node.
Algorithm(Search operation)

• If root.data is equal to search.data


• return root
else
• while data not found
• If data is greater than node.data
• goto right subtree
• Else
• goto left subtree
• If data found
• return node
• endwhile
• return data not found
• end if

You might also like