Skip to content

Commit 214ad00

Browse files
committed
Binary Tree Updates
Binary Tree Updates
1 parent 9e4b6cb commit 214ad00

10 files changed

+197
-0
lines changed
Binary file not shown.
Binary file not shown.
845 Bytes
Binary file not shown.
1.62 KB
Binary file not shown.

bin/chapter6trees/VerticalSum.class

2.09 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : FillNextSiblings.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter6trees;
16+
17+
import java.util.LinkedList;
18+
import java.util.Queue;
19+
20+
public class FillNextSiblingsWithLevelOrder {
21+
public static void fillNextSiblings(SiblingBinaryTreeNode root) {
22+
SiblingBinaryTreeNode tmp = null;
23+
if (root == null)
24+
return;
25+
// Initialization
26+
Queue<SiblingBinaryTreeNode> q = new LinkedList<SiblingBinaryTreeNode>();
27+
q.offer(root);
28+
q.offer(null);
29+
while (!q.isEmpty()) {
30+
tmp = q.poll();
31+
if (tmp != null) {
32+
tmp.setNextSibling(q.peek());
33+
if (tmp.getLeft() != null)
34+
q.offer(tmp.getLeft());
35+
if (tmp.right != null)
36+
q.offer(tmp.right);
37+
} else {
38+
// completion of a level;
39+
if (!q.isEmpty())
40+
q.offer(null);
41+
}
42+
}
43+
}
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : FillNextSiblingsWithRecursion.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter6trees;
16+
17+
public class FillNextSiblingsWithRecursion {
18+
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : PrintAllAncestors.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter6trees;
16+
17+
public class PrintAllAncestors {
18+
public static boolean printAllAncestors(BinaryTreeNode root, BinaryTreeNode node){
19+
if(root == null)
20+
return false;
21+
if(root.getLeft() == node || root.getRight() == node ||
22+
printAllAncestors(root.getLeft(), node) || printAllAncestors(root.getRight(), node)) {
23+
System.out.println(root.getData());
24+
return true;
25+
}
26+
return false;
27+
}
28+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : SiblingBinaryTreeNode.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter6trees;
16+
17+
public class SiblingBinaryTreeNode {
18+
public int data;
19+
public SiblingBinaryTreeNode left;
20+
public SiblingBinaryTreeNode right;
21+
public SiblingBinaryTreeNode nextSibling;
22+
23+
public SiblingBinaryTreeNode(int data){
24+
this.data = data;
25+
left = null;
26+
right = null;
27+
nextSibling = null;
28+
}
29+
public SiblingBinaryTreeNode(int data, SiblingBinaryTreeNode left, SiblingBinaryTreeNode right, SiblingBinaryTreeNode nextSibling){
30+
this.data = data;
31+
this.left = left;
32+
this.right = right;
33+
this.nextSibling = nextSibling;
34+
}
35+
public int getData() {
36+
return data;
37+
}
38+
public void setData(int data) {
39+
this.data = data;
40+
}
41+
public SiblingBinaryTreeNode getLeft() {
42+
return left;
43+
}
44+
public void setLeft(SiblingBinaryTreeNode left) {
45+
this.left = left;
46+
}
47+
public SiblingBinaryTreeNode getRight() {
48+
return right;
49+
}
50+
public void setRight(SiblingBinaryTreeNode right) {
51+
this.right = right;
52+
}
53+
public SiblingBinaryTreeNode getNextSibling() {
54+
return nextSibling;
55+
}
56+
public void setNextSibling(SiblingBinaryTreeNode nextSibling) {
57+
this.nextSibling = nextSibling;
58+
}
59+
// Sets the data in this BinaryTreeNode node.
60+
public void setValue(int data) {
61+
this.data = data;
62+
}
63+
// Tests whether this node is a leaf node.
64+
public boolean isLeaf() {
65+
return left == null && right == null;
66+
}
67+
}

src/chapter6trees/VerticalSum.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : VerticalSum.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter6trees;
16+
17+
import java.util.HashMap;
18+
19+
public class VerticalSum {
20+
public static void vSum(HashMap <Integer, Integer> hash,BinaryTreeNode root, int c){
21+
if(root.left!=null)
22+
vSum(hash, root.left, c-1);
23+
if(root.right!=null)
24+
vSum(hash,root.right, c+1);
25+
int data=0;
26+
if(hash.containsKey(c))
27+
data=hash.get(c);
28+
hash.put(c, root.data+data);
29+
}
30+
public static void verticalSum(BinaryTreeNode root){
31+
HashMap <Integer, Integer> hash = new HashMap<Integer, Integer>();
32+
vSum(hash, root, 0);
33+
System.out.println();
34+
35+
for(int k:hash.keySet()){
36+
System.out.println("key(pos): "+k+ " sum: "+ hash.get(k)+" ");
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)