-
-
Save anonymous/aa119b2829b15ead4cb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BinNode { | |
public int value; | |
public int depth; | |
public BinNode left; | |
public BinNode right; | |
public BinNode(int value, BinNode left, BinNode right){ | |
this.value = value; | |
this.left = left; | |
this.right = right; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Deque; | |
import java.util.LinkedList; | |
public class LevelOrderTreePrint { | |
public static void main(String[] args) { | |
// binary test | |
BinNode bn4 = new BinNode(7, null, null); | |
BinNode bn3 = new BinNode(15, null, null); | |
BinNode bn2 = new BinNode(20, bn3, bn4); | |
BinNode bn1 = new BinNode(9, null, null); | |
BinNode root = new BinNode(3, bn1, bn2); | |
printBinaryTree(root); | |
System.out.println(); | |
// K-ary test | |
Node n4 = new Node(7); | |
Node n3 = new Node(15); | |
Node n2 = new Node(20); | |
n2.children.add(n3); | |
n2.children.add(n4); | |
Node n1 = new Node(9); | |
Node root2 = new Node(3); | |
root2.children.add(n1); | |
root2.children.add(n2); | |
printKaryTree(root2); | |
} | |
private static void printBinaryTree(BinNode root) { | |
root.depth = 0; | |
Deque<BinNode> queue = new LinkedList<BinNode>(); | |
queue.add(root); | |
int depth = 0; | |
while (!queue.isEmpty()) { | |
BinNode curr = queue.poll(); | |
if (curr.depth > depth) { | |
System.out.println(); | |
depth = curr.depth; | |
} | |
System.out.print(curr.value + " "); | |
if (curr.left != null) { | |
curr.left.depth = depth + 1; | |
queue.add(curr.left); | |
} | |
if (curr.right != null) { | |
curr.right.depth = depth + 1; | |
queue.add(curr.right); | |
} | |
} | |
} | |
private static void printKaryTree(Node root) { | |
root.depth = 0; | |
Deque<Node> queue = new LinkedList<Node>(); | |
queue.add(root); | |
int depth = 0; | |
while (!queue.isEmpty()) { | |
Node curr = queue.poll(); | |
if (curr.depth > depth) { | |
System.out.println(); | |
depth = curr.depth; | |
} | |
System.out.print(curr.value + " "); | |
for (Node child : curr.children) { | |
child.depth = depth + 1; | |
queue.add(child); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Node { | |
public int value; | |
public int depth; | |
public List<Node> children; | |
public Node(int value){ | |
this.value = value; | |
this.children = new ArrayList<Node>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment