-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathInorder.c
144 lines (121 loc) · 4 KB
/
Inorder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/******************************************************************************
Author: @Suvraneel Bhuin
Inorder Traversal of a Binary Tree
Definition: Process all nodes of a tree by recursively processing the left subtree, then processing the root, and finally the right subtree
In-order traversal is mainly used to print the values, stored in the nodes of a binary search tree, in ascending order.
Algorithm of Inorder(tree) Traversal
1.Traverse the left subtree, i.e., call Inorder(left-subtree)
2.Visit the root.
3.Traverse the right subtree, i.e., call Inorder(right-subtree)
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node *lt;
struct node *rt;
}*root = NULL, *temp = NULL;
//root & temp are made global so that we dont need to pass those again & again
// Function to search the appropriate position to insert the new node
void search(struct node *t)
{
if ((temp->key > t->key) && (t->rt != NULL)) //key > root node move down more right
search(t->rt);
else if ((temp->key > t->key) && (t->rt == NULL)) //if right node NULL, insert key
t->rt = temp;
else if ((temp->key < t->key) && (t->lt != NULL)) //key < root node move down more left */
search(t->lt);
else if ((temp->key < t->key) && (t->lt == NULL)) //if left node NULL, insert key
t->lt = temp;
}
/* To insert a node in the tree */
void insert()
{
int data;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct node *)malloc(1*sizeof(struct node));
temp->key = data;
temp->lt = temp->rt = NULL; //initialise lt & rt child as null
if (root == NULL)
root = temp;
else
search(root);
}
/* recursive function to perform inorder (L-V-R) traversal of tree */
void inorder(struct node *t)
{
if (root == NULL) //empty tree
{
printf("No elements in a tree to display");
return;
}
if (t->lt != NULL) // L (Left)
inorder(t->lt); // ↓ ↓
printf("%d => ", t->key); // V (Visit)
if (t->rt != NULL) // ↓ ↓
inorder(t->rt); // R (Right)
}
//main driver
void main()
{
int choice;
printf("\n------------------------------------");
printf("\n1 - Insert an element into tree");
printf("\n2 - Inorder Traversal");
printf("\n3 - Exit");
printf("\n------------------------------------");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
printf("Inorder Traversal :\n");
inorder(root);
break;
case 3:
printf("Program terminated\n");
exit(0);
default :
printf("Invalid Choice entered");
break;
}
}
}
/*
Time Complexity : O(n)
Space Complexity: O(h) (where h is the height of the Tree)
Sample Run:
------------------------------------
1 - Insert an element into tree
2 - Inorder Traversal
3 - Exit
------------------------------------
Enter your choice : 1
Enter data of node to be inserted : 12
Enter your choice : 1
Enter data of node to be inserted : 4
Enter your choice : 1
Enter data of node to be inserted : 6
Enter your choice : 1
Enter data of node to be inserted : 9
Enter your choice : 1
Enter data of node to be inserted : 14
Enter your choice : 1
Enter data of node to be inserted : 17
Enter your choice : 1
Enter data of node to be inserted : 3
Enter your choice : 1
Enter data of node to be inserted : 19
Enter your choice : 2
Inorder Traversal :
3 => 4 => 6 => 9 => 12 => 14 => 17 => 19 =>
Enter your choice : 3
Program Terminated
*/