The nodes in a tree are linked by edges. We shall specifically address binary trees or binary search trees.

A binary tree is a type of data structure that is used for data storage. A binary tree has the unique property that each node can only have two offspring. A binary tree combines the advantages of an ordered array and a linked list, since search is as fast as in a sorted array and insertion and deletion operations are as fast as in a linked list.

Binary Tree

Important Terms

Following are the important terms with respect to tree.

  • Path − Path refers to the sequence of nodes along the edges of a tree.
  • Root − The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node.
  • Parent − Any node except the root node has one edge upward to a node called parent.
  • Child − The node below a given node connected by its edge downward is called its child node.
  • Leaf − The node which does not have any child node is called the leaf node.
  • Subtree − Subtree represents the descendants of a node.
  • Visiting − Visiting refers to checking the value of a node when control is on the node.
  • Traversing − Traversing means passing through nodes in a specific order.
  • Levels − Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.
  • keys − Key represents a value of a node based on which a search operation is to be carried out for a node.

Binary Search Tree Representation

Binary Search tree exhibits a special behavior. A node’s left child must have a value less than its parent’s value and the node’s right child must have a value greater than its parent value.

Binary Search Tree

We’re going to implement tree using node object and connecting them through references.

Tree Node

The code to write a tree node would be similar to what is given below. It has a data part and references to its left and right child nodes.

struct node {
   int data;   
   struct node *leftChild;
   struct node *rightChild;

In a tree, all nodes share a common construct.

BST Basic Operations

The basic operations that can be performed on a binary search tree data structure, are the following −

  • 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 tree is formed by the very first insertion. Following that, anytime an element is to be placed, first determine its suitable placement. Begin your search at the root node, and if the data is less than the key value, go to the left subtree and enter the data. Otherwise, find an empty position in the appropriate subtree and insert the data.


If root is NULL 
   then create root node

If root exists then
   compare the data with
   while until insertion position is located

      If data is greater than
         goto right subtree
         goto left subtree

   insert data
end If      

Search Operation

When looking for an element, begin by searching from the root node, then if the data is less than the key value, search for the element in the left subtree. Otherwise, look for the element in the appropriate subtree. For each node, use the same algorithm.


If is equal to
   return root
   while data not found

      If data is greater than
         goto right subtree
         goto left subtree
      If data found
         return node
   return data not found
end if      

1 thought on “Tree”

  1. When I read an article on this topic, safetoto the first thought was profound and difficult, and I wondered if others could understand.. My site has a discussion board for articles and photos similar to this topic. Could you please visit me when you have time to discuss this topic?


Leave a Comment

Your email address will not be published. Required fields are marked *

2 × 2 =