Skip to content

Commit 137b6fe

Browse files
committed
make Nary tree from its pre and post order
1 parent 44ccb8b commit 137b6fe

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

src/BinaryTree/CheckIfHeightBalancedBt.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
public class CheckIfHeightBalancedBt {
88

9-
static class Data{
9+
static class Data {
1010
private int height;
1111
private boolean balanced;
1212

@@ -16,20 +16,17 @@ public Data(int height, boolean balanced) {
1616
}
1717
}
1818

19-
private static Data checkIfHeightBalanced(Node node){
20-
if(node == null){
21-
return new Data(0,true);
22-
}else if(node.left == null && node.right == null){
23-
return new Data(1,true);
24-
}
25-
19+
private static Data checkIfHeightBalanced(Node node) {
20+
if (node == null) {
21+
return new Data(0, true);
22+
}
2623
Data leftData = checkIfHeightBalanced(node.left);
2724
Data rightData = checkIfHeightBalanced(node.right);
2825

29-
if(leftData.balanced && rightData.balanced && Math.abs(leftData.height - rightData.height) <=1){
30-
return new Data(Math.max(leftData.height,rightData.height)+1,true);
26+
if (leftData.balanced && rightData.balanced && Math.abs(leftData.height - rightData.height) <= 1) {
27+
return new Data(Math.max(leftData.height, rightData.height) + 1, true);
3128
}
32-
return new Data(0,false);
29+
return new Data(0, false);
3330
}
3431

3532
public static void main(String[] strings) {
@@ -39,13 +36,13 @@ public static void main(String[] strings) {
3936
root.right = new Node(3);
4037

4138
root.left.left = new Node(7);
42-
4339
root.left.right = new Node(6);
40+
4441
root.right.left = new Node(5);
4542
root.right.right = new Node(4);
4643

4744
root.left.left.left = new Node(9);
48-
root.left.left.left.left = new Node(999);
45+
/*root.left.left.left.left = new Node(999);*/
4946

5047

5148
System.out.println(checkIfHeightBalanced(root).balanced);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package BinaryTree;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
6+
/**
7+
* Created by dheeraj on 7/10/2016.
8+
*/
9+
public class MakeNaryTreeFromPrePost {
10+
11+
static class Node {
12+
int data;
13+
ArrayList<Node> children;
14+
15+
public Node(int data) {
16+
this.data = data;
17+
children = new ArrayList<Node>();
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return data + " ";
23+
}
24+
}
25+
26+
static int[] pre = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
27+
static int[] post = new int[]{3, 4, 2, 6, 5, 9, 8, 7, 1};
28+
29+
static HashSet<Integer> integerHashSet = new HashSet<>();
30+
31+
private static Node makeTree(int postStart, int postEnd, int preIndex) {
32+
if (postEnd < postStart) {
33+
return null;
34+
}
35+
Node node = new Node(pre[preIndex]);
36+
integerHashSet.add(pre[preIndex]);
37+
for (int x1 = 0; x1 < pre.length; x1++) {
38+
if (!integerHashSet.contains(pre[x1])) {
39+
40+
int postIndex = -1;
41+
for(int k =postStart;k<=postEnd;k++){
42+
if(post[k] == pre[x1]){
43+
postIndex = k;
44+
break;
45+
}
46+
}
47+
int ps = -1, pe = -1;
48+
for (int x = postStart; x <= postIndex; x++) {
49+
if (!integerHashSet.contains(post[x])) {
50+
if (ps == -1) {
51+
ps = x;
52+
pe = x;
53+
} else {
54+
pe = x;
55+
}
56+
}
57+
}
58+
if (pe != -1 && ps != -1) {
59+
node.children.add(makeTree(ps, pe, x1));
60+
}
61+
}
62+
}
63+
return node;
64+
}
65+
66+
67+
public static void main(String[] strings) {
68+
Node node = makeTree(0, pre.length - 1, 0);
69+
System.out.println();
70+
}
71+
72+
}

0 commit comments

Comments
 (0)