Hello guys, welcome back to my blog. In this article, I will discuss trees in data structure and algorithm using c, what are trees, code for trees in the data structure, types of trees algorithms, etc.
If you need an article on some other topics then comment us below in the comment box. You can also catch me @ Instagram – Chetan Shidling.
Also, read:
- Data Structure And Algorithms Using C Language Tutorial For Beginners.
- Stack Operation In Data Structure, Definition, Code, Push, Pop, Full.
- Linked List In Data Structure, Explanation, Algorithm, Code, Questions.
Trees In Data Structure
One tree is represented as a set of one or more nodes where one node is designated as the root of the tree and all the surviving nodes can be partitioned into non-empty sets any of which is a sub-tree of the root. Image shows a tree where node A is the root node and nodes B, C, and D remain children of the root node and set sub-trees of the tree rooted at node A.
- Root node: The root node A is the topmost node in the tree. If A = NULL, then it indicates the tree is empty. Sub-trees If the root node A is non NULL, then the trees T1, T2, and T3 are described as the sub-trees of A.
- Leaf node: A node that has no children is designated the leaf node or the terminal node. Path A series of continuous edges is called a path. For example, in an image, the path of the root node A to node I is given as A, D, and I.
- Ancestor node: An ancestor of a node is any antecedent node on the path from the root to that node. The root node does not have any ancestors. In the tree given in the image, but the nodes A, C, and G are the ancestors of node K.
Types of trees
- General trees
- Forests
- Binary trees
- Binary search trees
- Expression trees
- Tournament trees
Binary Search Tree
A binary search tree, also known as an ordered binary tree, is a variant of a binary tree in which the nodes are arranged in an order.
Binary search tree code in data structure and algorithm?
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
int main()
{
int n, i, item;
struct node *p, *q, *root;
printf("Enter the no of nodes\n");
scanf("%d",&n);
printf("Enter the all elements\n");
for(i=0; i<n; i++)
{
p = (struct node*)malloc(sizeof(struct node));
scanf("%d",&item);
p->data = item;
p->left = NULL;
p->right = NULL;
if(i==0)
root = p;
else
{
q=root;
while(1)
{
if(p->data > q->data)
{
if(q->right == NULL)
{
q->right=p;
break;
}
else
{
q=q->right;
}
}
else{
if(q->left == NULL)
{
q->left = p;
break;
}
else
{
q=q->left;
}
}
}
}
}
printf("All elements are stored in the tree");
return 0;
}
Output:
Enter the no of nodes
8
Enter the all elements
2
34
1
6
4
8
97
56
All elements are stored in the tree
In a binary search tree, the value that is smaller compared to the root is stored on the left side and the values that are greater than the root are stored on the right side as you can see in the above code.
I hope this article may help you all a lot. Thank you for reading.
Also, read:
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- App Developers, Skills, Job Profiles, Scope, Companies, Salary
- Applications Of Artificial Intelligence (AI) In Renewable Energy
- Applications Of Artificial Intelligence, AI Applications, What Is AI
- Applications Of Data Structures And Algorithms In The Real World
- Array Operations In Data Structure And Algorithms Using C Programming
- Artificial Intelligence Scope, Companies, Salary, Roles, Jobs
- AWS Lambda, Working, Cost, Advantages, Disadvantages
- AWS Technical Interview Questions, Top 200+ AWS Questions
- Battery Management Systems Using Artificial Intelligence
- Best Engineering Branch For Future
- Best Programming Languages For Electrical and Electronics Engineers
- Big Data, Evolution Of Big Data, Benefits Of Big Data, Opportunities
- Bit Operation In C Programming With Example & Applications
- Blockchain Projects For Computer Science Engineers
- Blockchain Technology, History, Working, Applications, Advantages
- Brain Computer Interfaces Technology, Beyond AI, ML, IoT, Blockchain
- C Language Interview Questions On Programs With Output
- C Program On Arrays With Output For Placement Exams