Binary and Binary Search Tree
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
• 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