Tree

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.

Algorithm

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

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.

Algorithm

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      
0

7 thoughts 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?

    0
  2. Simply want tto say your article iis ass astonishing. Thee coearness inn your ost iss jjst
    great and i could assume you’re aan expert oon thiis subject.

    Fine wiuth your permission llet mme tto grab our RSS feed too keep uup too date wifh forthcoming post.
    Thahks a million and pleasde carry oon the rewarding work.

    0
  3. You are my inspiration, I own few web logs and occasionally run out from post :). “Follow your inclinations with due regard to the policeman round the corner.” by W. Somerset Maugham.

    0
  4. My developer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am concerned about switching to another platform. I have heard great things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be greatly appreciated!

    0

Leave a Comment

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