forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Inorder_Traversal.cs
More file actions
173 lines (143 loc) · 3.59 KB
/
Tree_Inorder_Traversal.cs
File metadata and controls
173 lines (143 loc) · 3.59 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
Tree Inorder Traversal In C#
*/
using System;
// Node Class To Declare Nodes Of The Tree
class Node
{
public int data;
public Node left;
public Node right;
public void display()
{
Console.Write(" [");
Console.Write(data);
Console.Write("] ");
}
}
// Tree Class To Declare Binary Search Tree
class Tree
{
public Node root;
// Constructor
public Tree()
{
root = null;
}
public Node ReturnRoot()
{
return root;
}
// Function To Insert Node Into BST
public void Insert(int id)
{
Node newNode = new Node();
newNode.data = id;
if (root == null)
root = newNode;
else
{
Node current = root;
Node parent;
while (true)
{
parent = current;
if (id < current.data)
{
current = current.left;
if (current == null)
{
parent.left = newNode;
return;
}
}
else
{
current = current.right;
if (current == null)
{
parent.right = newNode;
return;
}
}
}
}
}
// Inorder Traversal Function
public void Inorder(Node Root)
{
if (Root != null)
{
Inorder(Root.left);
Console.Write(Root.data + " ");
Inorder(Root.right);
}
}
}
class InorderTreeProgram
{
public static void Main(string[] args)
{
// Declaring the Object Of Tree Class
Tree BST = new Tree();
Console.WriteLine("1. Insert Node");
Console.WriteLine("2. Display InOrder Traversal");
Console.WriteLine("3. Exit");
while(true)
{
Console.WriteLine("\nEnter your choice: ");
String choice = Console.ReadLine();
int Choice = int.Parse(choice);
switch(Choice)
{
case 1:
{
Console.WriteLine("Enter the value to insert node: ");
String val = Console.ReadLine();
int data = int.Parse(val);
BST.Insert(data);
break;
}
case 2:
{
Console.WriteLine("\nTree Inorder Traversal: ");
BST.Inorder(BST.ReturnRoot());
Console.WriteLine(" ");
break;
}
case 3:
{
Console.WriteLine("Exit");
System.Environment.Exit(0);
break;
}
}
}
}
}
/**
1. Insert Node
2. Display InOrder Traversal
3. Exit
Enter your choice: 1
Enter the value to insert node: 30
Enter your choice: 1
Enter the value to insert node: 35
Enter your choice: 1
Enter the value to insert node: 57
Enter your choice: 1
Enter the value to insert node: 15
Enter your choice: 1
Enter the value to insert node: 63
Enter your choice: 1
Enter the value to insert node: 49
Enter your choice: 1
Enter the value to insert node: 77
Enter your choice: 1
Enter the value to insert node: 98
Enter your choice: 2
Tree Inorder Traversal:
15 30 35 49 57 63 77 98
Enter your choice: 3
Exit
*/