tree traversal in c

Solutions on MaxInterview for tree traversal in c by the best coders in the world

showing results for - "tree traversal in c"
Maite
26 Oct 2020
1#include <stdio.h>
2#include <stdlib.h>
3
4struct node {
5  int item;
6  struct node* left;
7  struct node* right;
8};
9
10// Inorder traversal
11void inorderTraversal(struct node* root) {
12  if (root == NULL) return;
13  inorderTraversal(root->left);
14  printf("%d ->", root->item);
15  inorderTraversal(root->right);
16}
17
18// preorderTraversal traversal
19void preorderTraversal(struct node* root) {
20  if (root == NULL) return;
21  printf("%d ->", root->item);
22  preorderTraversal(root->left);
23  preorderTraversal(root->right);
24}
25
26// postorderTraversal traversal
27void postorderTraversal(struct node* root) {
28  if (root == NULL) return;
29  postorderTraversal(root->left);
30  postorderTraversal(root->right);
31  printf("%d ->", root->item);
32}
33
34// Create a new Node
35struct node* createNode(value) {
36  struct node* newNode = malloc(sizeof(struct node));
37  newNode->item = value;
38  newNode->left = NULL;
39  newNode->right = NULL;
40
41  return newNode;
42}
43
44// Insert on the left of the node
45struct node* insertLeft(struct node* root, int value) {
46  root->left = createNode(value);
47  return root->left;
48}
49
50// Insert on the right of the node
51struct node* insertRight(struct node* root, int value) {
52  root->right = createNode(value);
53  return root->right;
54}
55
56int main() {
57  struct node* root = createNode(1);
58  insertLeft(root, 12);
59  insertRight(root, 9);
60
61  insertLeft(root->left, 5);
62  insertRight(root->left, 6);
63
64  printf("Inorder traversal \n");
65  inorderTraversal(root);
66
67  printf("\nPreorder traversal \n");
68  preorderTraversal(root);
69
70  printf("\nPostorder traversal \n");
71  postorderTraversal(root);
72}
Josefina
04 Jun 2020
1# include <stdio.h>
2# include <conio.h>
3# include <stdlib.h>
4 
5typedef struct BST {
6   int data;
7   struct BST *lchild, *rchild;
8} node;
9 
10void insert(node *, node *);
11void inorder(node *);
12void preorder(node *);
13void postorder(node *);
14node *search(node *, int, node **);
15 
16void main() {
17   int choice;
18   char ans = 'N';
19   int key;
20   node *new_node, *root, *tmp, *parent;
21   node *get_node();
22   root = NULL;
23   clrscr();
24 
25   printf("\nProgram For Binary Search Tree ");
26   do {
27      printf("\n1.Create");
28      printf("\n2.Search");
29      printf("\n3.Recursive Traversals");
30      printf("\n4.Exit");
31      printf("\nEnter your choice :");
32      scanf("%d", &choice);
33 
34      switch (choice) {
35      case 1:
36         do {
37            new_node = get_node();
38            printf("\nEnter The Element ");
39            scanf("%d", &new_node->data);
40 
41            if (root == NULL) /* Tree is not Created */
42               root = new_node;
43            else
44               insert(root, new_node);
45 
46            printf("\nWant To enter More Elements?(y/n)");
47            ans = getch();
48         } while (ans == 'y');
49         break;
50 
51      case 2:
52         printf("\nEnter Element to be searched :");
53         scanf("%d", &key);
54 
55         tmp = search(root, key, &parent);
56         printf("\nParent of node %d is %d", tmp->data, parent->data);
57         break;
58 
59      case 3:
60         if (root == NULL)
61            printf("Tree Is Not Created");
62         else {
63            printf("\nThe Inorder display : ");
64            inorder(root);
65            printf("\nThe Preorder display : ");
66            preorder(root);
67            printf("\nThe Postorder display : ");
68            postorder(root);
69         }
70         break;
71      }
72   } while (choice != 4);
73}
74/*
75 Get new Node
76 */
77node *get_node() {
78   node *temp;
79   temp = (node *) malloc(sizeof(node));
80   temp->lchild = NULL;
81   temp->rchild = NULL;
82   return temp;
83}
84/*
85 This function is for creating a binary search tree
86 */
87void insert(node *root, node *new_node) {
88   if (new_node->data < root->data) {
89      if (root->lchild == NULL)
90         root->lchild = new_node;
91      else
92         insert(root->lchild, new_node);
93   }
94 
95   if (new_node->data > root->data) {
96      if (root->rchild == NULL)
97         root->rchild = new_node;
98      else
99         insert(root->rchild, new_node);
100   }
101}
102/*
103 This function is for searching the node from
104 binary Search Tree
105 */
106node *search(node *root, int key, node **parent) {
107   node *temp;
108   temp = root;
109   while (temp != NULL) {
110      if (temp->data == key) {
111         printf("\nThe %d Element is Present", temp->data);
112         return temp;
113      }
114      *parent = temp;
115 
116      if (temp->data > key)
117         temp = temp->lchild;
118      else
119         temp = temp->rchild;
120   }
121   return NULL;
122}
123/*
124 This function displays the tree in inorder fashion
125 */
126void inorder(node *temp) {
127   if (temp != NULL) {
128      inorder(temp->lchild);
129      printf("%d", temp->data);
130      inorder(temp->rchild);
131   }
132}
133/*
134 This function displays the tree in preorder fashion
135 */
136void preorder(node *temp) {
137   if (temp != NULL) {
138      printf("%d", temp->data);
139      preorder(temp->lchild);
140      preorder(temp->rchild);
141   }
142}
143 
144/*
145 This function displays the tree in postorder fashion
146 */
147void postorder(node *temp) {
148   if (temp != NULL) {
149      postorder(temp->lchild);
150      postorder(temp->rchild);
151      printf("%d", temp->data);
152   }
153}
154
similar questions
queries leading to this page
tree traversal in c