Binary Tree with implementation using C.

Data Structure

Binary Tree:

A binary tree each node has at most two children generally referred as left child and right child from root node.

 

Components of Each node:

 

  • Pointer to left subtree
  • Pointer to right subtree
  • Data element

The topmost node in the tree is called the root. An empty tree is represented by NULL pointer.

A tree which has two nodes corresponding their parent node is known as binary tree.

Binary tree term as two types.

1)      Full Binary tree.

A tree which has two nodes corresponding their parent node is known as full binary tree.

2)      Complete Binary tree.

3)      A tree which has left nodes corresponding their parent node is known as complete binary tree.

 

 Common Terminologies of Binary Tree:

  • Root: Topmost node in a tree.
  • Parent: Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent.
  • Child: A node directly connected to another node when moving away from the root.
  • Leaf/External node: Node with no children.
  • Internal node: Node with atleast one children.
  • Depth of a node: Number of edges from root to the node.
  • Height of a node: Number of edges from the node to the deepest leaf. Height of the tree is the height of the root.
binary tree

Binary Tree

In the  binary tree root node is A. The tree has 10 nodes with 5 internal nodes A,B,C,E,G and 5 external nodes D,F,H,I,J. The height of the tree is 3. B is the parent of D and E while D and E are children of B.

C implementation Process:

1st need to create node struct where data, *left , *right declared;

2nd need to create new node function which take an item and return new node, where firstly need i) memory allocation same as datatype size.

If failed to memory allocation show error or assign data into new node,if left or right child no value then put them as NULL.

3rd  to add child left and right need two function both take two input one for node pointer another for left or right child assigned.

4th create create_tree() function by create_node ,add_left_child() and right_child() function. This function return root.

5th main() function receive root data or value will print.

C Program For binary tree.

#include <stdio.h>
/*create a data type node */
typedef struct node Node;
struct node{
int data;
Node  *left;
Node  *right;
};

/*Create each node and corresponding child.Allocate memory for new node if new node empty. show error either keep item data variable,initialize left and right child into NULL then return new */

Node *create_node (int item)

{
Node *new_node = (Node *) malloc(sizeof(Node));
if(new_node == NULL)
{
printf(“Error ! Could not create an tree\n”);
exit(1);
}
new_node -> data = item;
new_node -> left= NULL;
new_node ->right =NULL;
return (new_node);
}
/*add left node corresponding parent node */

void add_left_child( Node *node, Node *child )

{
node->left =child;
}
/*add right node corresponding parent node */
void add_right_child( Node *node, Node *child )

{
node->right =child;
}Node *seven=create_node(7);/*create root node: 7 */
Node *nine=create_node(9);/*create root node: 9 */
add_left_child(two,seven);//*create root node: 7 */
add_right_child(two,nine);/*create root node: 9 */
Node *one = create_node(1); /*create parent node 1 */
Node *six = create_node(6);/*create parent node:6 */
add_left_child(seven,one);/*add parent node seven left:1*/
add_right_child(seven,six);/*add parent seven node right:6*/
Node *five=create_node(5) ;/* similar process rest of all*/
Node *ten = create_node(10); add_left_child(six,five);
add_right_child(six,ten);
Node *eight = create_node(8); add_right_child(nine,eight);
Node *three=create_node(3);
Node *four=create_node(4);
add_left_child(eight,three); add_right_child(eight,four);
return two;/* return root node*/
} /*print root node 2; */

int main()

{

Node *root =create_tree();

printf(“%d”,root->data);

return 0;

}

Recent Articles on Binary Tree in the blog   Topic :

  1. Introduction Of Binary Tree
  2. Traversals such as


			

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like Oracle, MySQL, MS Access, dBase, FoxPro etc. Database Languages: Refers to the languages used to...