forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inorder_Traversal.kt
62 lines (55 loc) · 1.16 KB
/
Inorder_Traversal.kt
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
//Code for printing inorder traversal in kotlin
class Node
{
//Structure of Binary tree node
int num;
Node left, right;
public node(int num)
{
key = item;
left = right = null;
}
}
class BinaryTree
{
Node root;
BinaryTree()
{
root = null;
}
//function to print inorder traversal
fun printInorder(Node: node):void
{
if (node == null)
return;
printInorder(node.left);
//printing val of node
println(node.num);
printInorder(node.right);
}
fun printInorder():void
{
printInorder(root);
}
}
//Main function
fun main()
{
BinaryTree tree = new BinaryTree();
var read = Scanner(System.`in`)
println("Enter the size of Array:")
val arrSize = read.nextLine().toInt()
var arr = IntArray(arrSize)
println("Enter data of binary tree")
for(i in 0 until arrSize)
{
arr[i] = read.nextLine().toInt()
tree.root = new Node(arr[i]);
}
println("Inorder traversal of binary tree is");
tree.printInorder();//function call
}
/*
Input: 10 8 3 30 5 19 4 5 1
Output:5 3 1 2 8 10 19 30 4
*/