Skip to content

Commit e47218d

Browse files
authored
Merge pull request #2 from manojchawan/treeSort-patch
Tree Sort
2 parents 75a861b + ce587d9 commit e47218d

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/java/main/TreeSort.Java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class GFG
2+
{
3+
class Node
4+
{
5+
int key;
6+
Node left, right;
7+
8+
public Node(int item)
9+
{
10+
key = item;
11+
left = right = null;
12+
}
13+
}
14+
Node root;
15+
GFG()
16+
{
17+
root = null;
18+
}
19+
void insert(int key)
20+
{
21+
root = insertRec(root, key);
22+
}
23+
24+
/* A recursive function to
25+
insert a new key in BST */
26+
Node insertRec(Node root, int key)
27+
{
28+
if (root == null)
29+
{
30+
root = new Node(key);
31+
return root;
32+
}
33+
if (key < root.key)
34+
root.left = insertRec(root.left, key);
35+
else if (key > root.key)
36+
root.right = insertRec(root.right, key);
37+
return root;
38+
}
39+
void inorderRec(Node root)
40+
{
41+
if (root != null)
42+
{
43+
inorderRec(root.left);
44+
System.out.print(root.key + " ");
45+
inorderRec(root.right);
46+
}
47+
}
48+
void treeins(int arr[])
49+
{
50+
for(int i = 0; i < arr.length; i++)
51+
{
52+
insert(arr[i]);
53+
}
54+
55+
}
56+
public static void main(String[] args)
57+
{
58+
GFG tree = new GFG();
59+
int arr[] = {5, 4, 7, 2, 11};
60+
tree.treeins(arr);
61+
tree.inorderRec(tree.root);
62+
}
63+
}
64+

0 commit comments

Comments
 (0)