-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedTree.java
More file actions
78 lines (59 loc) · 1.35 KB
/
BalancedTree.java
File metadata and controls
78 lines (59 loc) · 1.35 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
public class BalancedTree
{
static int balanceFactor=0;
static int height = 0;
static Node root=null;
public class Node{
Node left;
Node right;
int data;
Node(int d){
this.data=d;
this.left = null;
this.right=null;
}
}
Node insert(Node node,int d){
if(root==null){
root = new Node(d);
return null;
}
else if(d > node.data){
if(node.right==null){
node.right = new Node(d);
}
insert(node.right,d);
}
else if(d < node.data){
if(node.left==null){
node.left = new Node(d);
}
insert(node.left,d);
}
return null;
}
int getSize(Node node){
if(node==null)
return 0;
return 1 + getSize(node.left) + getSize(node.right);
}
void inOrder(Node node){
if(node==null)
return;
inOrder(node.left);
System.out.print(node.data+" ");
inOrder(node.right);
}
public static void main(String[] args)
{
BalancedTree obj = new BalancedTree();
int i = 0;
int a[]={11,13,10,49,79,15};
while(i<a.length){
obj.insert(root,a[i]);
i++;
}
obj.inOrder(root);
System.out.print("\n Number of Nodes:" + obj.getSize(root));
}
}