-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_21.java
More file actions
81 lines (65 loc) · 2.53 KB
/
Copy pathBT_Problem_21.java
File metadata and controls
81 lines (65 loc) · 2.53 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
package trees.binaryTree;
import java.util.HashMap;
/* Problem Title :- Construct Binary tree from Inorder and preorder traversal */
public class BT_Problem_21 {
static class Node {
char data;
Node left, right;
Node(char item) {
data = item;
left = right = null;
}
}
public static Node root;
// Store indexes of all items so that we can quickly find later
static HashMap<Character,Integer> mp = new HashMap<>();
static int preIndex = 0;
/* Recursive function to construct binary of size len from Inorder traversal in[]
and Preorder traversal pre[].
Initial values of inStart and inEnd should be0 and len -1.
The function doesn't do any error checking for cases where inorder
and preorder do not form a tree */
public static Node buildTree(char[] in, char[] pre, int inStart, int inEnd) {
if(inStart > inEnd) {
return null;
}
/* Pick current node from Preorder traversal using preIndex and increment preIndex */
char curr = pre[preIndex++];
Node tNode;
tNode = new Node(curr);
/* If this node has no children then return */
if (inStart == inEnd) {
return tNode;
}
/* Else find the index of this node in Inorder traversal */
int inIndex = mp.get(curr);
/* Using index in Inorder traversal, construct left and right subtree's */
tNode.left = buildTree(in, pre, inStart, inIndex - 1);
tNode.right = buildTree(in, pre, inIndex + 1, inEnd);
return tNode;
}
// This function mainly creates an unordered_map, then calls buildTree()
public static Node buldTreeWrap(char[] in, char[] pre, int len) {
for(int i = 0; i < len; i++)
mp.put(in[i], i);
return buildTree(in, pre, 0, len - 1);
}
/* This function is here just to test buildTree() */
static void printInorder(Node node) {
if(node == null)
return;
printInorder(node.left);
System.out.print(node.data + " ");
printInorder(node.right);
}
public static void main(String[] args) {
char[] in = {'D', 'B', 'E', 'A', 'F', 'C'};
char[] pre = {'A', 'B', 'D', 'E', 'C', 'F'};
int len = in.length;
root = buldTreeWrap(in, pre, len);
/* Let us test the built tree by printing Inorder traversal */
System.out.println("Inorder traversal of the constructed tree is");
printInorder(root);
}
}
// TIME COMPLEXITY :- O(N)