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.
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.
/*create a data type node */
typedef struct node Node;
/*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”);
new_node -> data = item;
new_node -> left= NULL;
new_node ->right =NULL;
/*add left node corresponding parent node */
void add_left_child( Node *node, Node *child )
/*add right node corresponding parent node */
void add_right_child( Node *node, Node *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);
Node *eight = create_node(8); add_right_child(nine,eight);
return two;/* return root node*/
} /*print root node 2; */
Node *root =create_tree();
Recent Articles on Binary Tree in the blog Topic :
- Introduction Of Binary Tree
- Traversals such as