Skip to content

Commit 4c1f51e

Browse files
committed
08/10/2019
1 parent be937f8 commit 4c1f51e

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package DSA.Trees;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
import java.util.Stack;
6+
7+
public class PerfectTreeSpecificLevelOrder {
8+
9+
static class Node {
10+
int data;
11+
Node left, right;
12+
13+
public Node(int data) {
14+
this.data = data;
15+
left = right = null;
16+
}
17+
}
18+
19+
static class BinaryTree {
20+
Node root;
21+
22+
void specificLevelTraversal() {
23+
Queue<Node> queue = new LinkedList<>();
24+
System.out.print(root.data + " ");
25+
queue.add(root.left);
26+
queue.add(root.right);
27+
System.out.print(root.left.data + " ");
28+
System.out.print(root.right.data + " ");
29+
while (!queue.isEmpty()) {
30+
Node t = queue.poll();
31+
Node t2 = queue.poll();
32+
System.out.print(t.left.data + " " + t2.right.data + " ");
33+
System.out.print(t.right.data + " " + t2.left.data + " ");
34+
if (t.left.left != null) {
35+
queue.add(t.left);
36+
queue.add(t2.right);
37+
queue.add(t.right);
38+
queue.add(t2.left);
39+
}
40+
41+
}
42+
}
43+
44+
void specificReverseLevelTraversalUtil(Stack<Node> stack) {
45+
Queue<Node> queue = new LinkedList<>();
46+
//System.out.print(root.data + " ");
47+
queue.add(root.left);
48+
queue.add(root.right);
49+
//System.out.print(root.left.data + " ");
50+
//System.out.print(root.right.data + " ");
51+
while (!queue.isEmpty()) {
52+
Node t = queue.poll();
53+
Node t2 = queue.poll();
54+
stack.push(t2.left);
55+
stack.push(t.right);
56+
stack.push(t2.right);
57+
stack.push(t.left);
58+
//System.out.print(t.left.data + " " + t2.right.data + " ");
59+
//System.out.print(t.right.data + " " + t2.left.data + " ");
60+
if (t.left.left != null) {
61+
queue.add(t.right);
62+
queue.add(t2.left);
63+
queue.add(t.left);
64+
queue.add(t2.right);
65+
}
66+
67+
}
68+
}
69+
70+
public void specificReverseLevelTraversal() {
71+
72+
Stack<Node> stack = new Stack<>();
73+
stack.push(root);
74+
stack.push(root.left);
75+
stack.push(root.right);
76+
specificReverseLevelTraversalUtil(stack);
77+
78+
while(!stack.isEmpty()){
79+
System.out.print(stack.pop().data+" ");
80+
}
81+
82+
}
83+
}
84+
85+
public static void main(String[] args) {
86+
BinaryTree tree = new BinaryTree();
87+
tree.root = new Node(1);
88+
tree.root.left = new Node(2);
89+
tree.root.right = new Node(3);
90+
91+
tree.root.left.left = new Node(4);
92+
tree.root.left.right = new Node(5);
93+
tree.root.right.left = new Node(6);
94+
tree.root.right.right = new Node(7);
95+
96+
tree.root.left.left.left = new Node(8);
97+
tree.root.left.left.right = new Node(9);
98+
tree.root.left.right.left = new Node(10);
99+
tree.root.left.right.right = new Node(11);
100+
tree.root.right.left.left = new Node(12);
101+
tree.root.right.left.right = new Node(13);
102+
tree.root.right.right.left = new Node(14);
103+
tree.root.right.right.right = new Node(15);
104+
105+
tree.root.left.left.left.left = new Node(16);
106+
tree.root.left.left.left.right = new Node(17);
107+
tree.root.left.left.right.left = new Node(18);
108+
tree.root.left.left.right.right = new Node(19);
109+
tree.root.left.right.left.left = new Node(20);
110+
tree.root.left.right.left.right = new Node(21);
111+
tree.root.left.right.right.left = new Node(22);
112+
tree.root.left.right.right.right = new Node(23);
113+
tree.root.right.left.left.left = new Node(24);
114+
tree.root.right.left.left.right = new Node(25);
115+
tree.root.right.left.right.left = new Node(26);
116+
tree.root.right.left.right.right = new Node(27);
117+
tree.root.right.right.left.left = new Node(28);
118+
tree.root.right.right.left.right = new Node(29);
119+
tree.root.right.right.right.left = new Node(30);
120+
tree.root.right.right.right.right = new Node(31);
121+
122+
tree.specificReverseLevelTraversal();
123+
}
124+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package DSA.Trees;
2+
3+
import java.util.Vector;
4+
5+
public class ReplaceNodeSumInorderSuccessorPredecessor {
6+
static class Node {
7+
int data;
8+
Node left, right;
9+
10+
public Node(int data) {
11+
this.data = data;
12+
left = right = null;
13+
}
14+
}
15+
static Vector<Integer> arr = new Vector<>();
16+
17+
static void Replace(Node root) {
18+
arr.add(0);
19+
inorderStore(root);
20+
arr.add(0);
21+
inorderReplace(root);
22+
23+
//2 9 3 11 4 13 3
24+
}
25+
static int i=1;
26+
27+
static void inorderReplace(Node root){
28+
if(root==null)
29+
return;
30+
inorderReplace(root.left);
31+
int a = arr.elementAt(i-1);
32+
int b = arr.elementAt(i+1);
33+
root.data = a+b;
34+
i++;
35+
inorderReplace(root.right);
36+
37+
}
38+
static void inorder(Node root){
39+
if(root==null)
40+
return;
41+
inorder(root.left);
42+
System.out.print(root.data+" ");
43+
inorder(root.right);
44+
}
45+
46+
static void inorderStore(Node root){
47+
if(root==null)
48+
return;
49+
inorderStore(root.left);
50+
arr.add(root.data);
51+
inorderStore(root.right);
52+
}
53+
54+
55+
public static void main(String[] args) {
56+
Node root = new Node(1); // 1
57+
root.left = new Node(2); // / \
58+
root.right = new Node(3); // 2 3
59+
root.left.left = new Node(4); // / \ / \
60+
root.left.right = new Node(5); // 4 5 6 7
61+
root.right.left = new Node(6);
62+
root.right.right = new Node(7);
63+
System.out.println("Inorder before:");
64+
inorder(root);
65+
Replace(root);
66+
System.out.println("");
67+
System.out.println("Inorder after:");
68+
inorder(root);
69+
}
70+
}

0 commit comments

Comments
 (0)