binary search tree

Trees In Data Structure And Algorithm Using C, Code, Explanation, Types

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:

  1. Data Structure And Algorithms Using C Language Tutorial For Beginners.
  2. Stack Operation In Data Structure, Definition, Code, Push, Pop, Full.
  3. 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.

Trees-in-data-structure-
  1. 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.
  2. 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.
  3. 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

  1. General trees
  2. Forests
  3. Binary trees
  4. Binary search trees
  5. Expression trees
  6. Tournament trees

Binary Search Tree

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:

About The Author

Share Now