Binary Tree

Binary tree is one of widely used tree data structure in computer science. It is the simplest form of tree node where each node can have max of two children, known as left and right. Binary tree has numerous practical usages where one of the most widely used scenarios are items need to be arranged in sorted manner to optimize lookup efficiency. We will cover more topics in our next sections.

Binary Tree

Nodes in Binary Tree

This is the simplest form of tree structure with two pointers; one left and one right to point to the sub tree nodes. For a particular node, minimum child node can be zero and maximum children can be two.

Binary Tree Node

Binary Tree data structure

Designing a a data structure for a node is done by taking two pointer of type node. One to hold left child and another to point to right child. Node often contains a member variable to hold the value of its own. The data structure for a node of a binary tree looks like

struct node {
  struct node * left;
  struct node * right;
  int value;
};

Binary Tree node Allocation

When any node has no further child down the hierarchy, we put these pointers as NULL. Now if a node has no child then both node->left and node ->right is set to NULL. We generally create a new node setting both left and right to NULL. Later we asign left child pointer and then right child pointer once a new node comes for addition.

struct node * alloc_new_node (int value)
{
  struct node * new_node;
  /* Allocation with malloc */
  new_node  = (struct node *) malloc (sizeof(struct node));
  new_node ->value = <value>;
  new_node ->left = NULL;
  new_node ->right = NULL;
  return new_node;
10  }
11  struct node * alloc_new_node (int value)
12  {
13    struct node * new_node;
14    /* Allocation with calloc */
15    new_node  = (struct node *) calloc (1, sizeof(struct node));
16    new_node ->value = values 
17  }

Binary Tree node Addition

Now this new allocated node can be attached to an existing node in either in right side or left side. There will be a comparison between the value of this node to the parent node where it is getting added. If value is less than parent it can be added to left child else this should be added in right. This is the logic often used in binary search tree algorithm. We will discuss this more in our next topic.

The above logic is true when only root exists in the tree. But adding a node to populated binary tree is more complex. We should consider this logic right from root recursively till the condition matches.

struct node *node add_new_node (struct node *node, int value)
{
  if (node == NULL) { 
    node = add_new_node(value);
    return node;
  } else if (value = node -> value) {
    return NULL;
  } else if (value < node -> value) {
    if(node ->left == NULL) 
10        node ->left  = add_new_node(value);
11      else 
12        add_new_node (node ->left, value);
13    } else if (value > node -> value) {
14      if(node ->right == NULL) 
15        node ->right  = add_new_node(value);
16      else 
17        add_new_node (node ->right, value);
18    }
19  }

Printing a Binary Tree

If we observe a binary tree we would find that lesser values are located at right side and greater values are at right side. This is the way we arranged it during the population time and thus tree elements are sorted in ascending order. Now printing entire binary tree would be in the same way as it has been arranged. We would consider printing left child first and then the right child. Printing is also recursive in nature and we should consider left child repeatedly till we reach most left node and print it one after another with this same logic.

void print_tree (struct node *node)
{
  if (node) {
    if(node ->left) {
      print_tree (node ->left);
    }
    printf("%d", node->value);
    if(node->right) {
      print_tree (node->right);
10      }
11    }
12  }

Total number of nodes

Computation starts from the root node and it goes recursively to the sub tree till it reaches the leaf node. In this way computation adds 1 for the node and further calls the same function for left and right sub trees. Leaf node does not have further sub child thus we return zero and last call returns. Finally top most parent call accumulates all the count and returns to the caller.

int get_node_count (struct node* node) { 
  if (node==NULL) { 
    return(0); 
  } else { 
    return(get_node_count(node->left) + 1 + get_node_count(node->right)); 
  } 
}

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

reverse single linked list, delete single linked list, Stack, Tree and Binary tree, Binary tree, traverse Binary tree, Binary search tree, double Binary tree, mirror Binary tree,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan