Tree
Tree
2. Siblings:- Siblings mean that nodes which have the same parent node. In the above
image, 17 and 72 are siblings because they have 50 in common.
3. Internal Node:- Internal Node means that a node which has at least a single child. In
the above image, 17, 72, 12, 23, 54 are internal nodes.
4. External Node:- External Node means that a node which has no children. It is also
known as leaf. In the above image, 67 and 76 are external nodes.
8. Path:- Path is a combination of nodes and edges connected with each other. In the
above image, 50 to 19 is a path.
9. Depth:- You can calculate depth by the number of edges from node to the root of the
tree.
Applications of trees:-
Following are some of the main applications of trees.
Used to manipulate hierarchical data or information.
Through trees, Searching is very easy.
Router Algorithms.
Used to manipulate sorted lists of data.
With this, you can form multi stage decision making.
Binary Tree in C:-
A tree is called binary when its elements have at most two children. In a binary tree, each
element should have only 2 children and these are known as left and right.
Representation of Binary Tree in C:-
The value of root is NULL when a tree is empty. It works on O(logN) for insert, search
and delete operations.
struct node
{
int data;
struct node *left_child;
struct node *right_child;
};
In the above example, the *left_child is the pointer to the left child which can or cannot
be NULL and the *right_child is the pointer to the right child which can or cannot be
NULL.
With the help of a binary search tree, you can easily find an element in a huge set
because it is fast and efficient.
Using binary trees, you can implement heapsort.
To store information in databases, your best way is to make use of binary trees.
struct node
{
int data;
struct node *left_child;
struct node *right_child;
};
2. Creating Nodes in a binary tree:-
It is like creating data elements in linked lists. A binary tree is created by inserting the
root node and its child nodes. Following is the code to create nodes in a binary tree:-
The above code will search for the value of a node whether the node of the same value
exists or not.
4. Deletion of a binary tree:-
You can delete a binary tree by removing the child nodes and the root node. Below is the
code snippet to delete a binary tree.
Pre-Order:- It displays in order. First root node, then left node and then right node.
In-Order:- It displays first left node, then root node and then right node.
Post-Order:- It displays first left node, then right node and then root node.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};
struct node *new_node(int value)
{
struct node *tmp = (struct node *)malloc(sizeof(struct node));
tmp->value = value;
tmp->left_child = tmp->right_child = NULL;
return tmp;
}
void print(struct node *root_node) // displaying the nodes!
{
if (root_node != NULL)
{
print(root_node->left_child);
printf("%d \n", root_node->value);
print(root_node->right_child);
}
}
struct node* insert_node(struct node* node, int value) // inserting nodes!
{
if (node == NULL) return new_node(value);
if (value < node->value)
{
node->left_child = insert_node(node->left_child, value);
}
else if (value > node->value)
{
node->right_child = insert_node(node->right_child, value);
}
return node;
}
int main()
{
printf("TechVidvan Tutorial: Implementation of a Binary Tree in C!\n\n");
struct node *root_node = NULL;
root_node = insert_node(root_node, 10);
insert_node(root_node, 10);
insert_node(root_node, 30);
insert_node(root_node, 25);
insert_node(root_node, 36);
insert_node(root_node, 56);
insert_node(root_node, 78);
print(root_node);
return 0;
}
Output:
Implementation of a Binary Tree in C!
10
25
30
36
56
78
--------------------------------