Algorithm for Modified Preorder Tree Traversal in Java - java

In the spirit of How to repair a corrupted MPTT tree (nested set) in the database using SQL?, I am trying to figure out an algorithm to determine the left and right values of a Modified Preorder Tree Traversal in Java given a root Node. Does anyone have any experience converting a regular pre order traversal into the modified traversal?
I currently have this as my preorder traversal.
List<Node> preorderTraversal(Node root) {
List<Node> list = new ArrayList<>();
if(root == null) return list;
Stack<Node> stack = new Stack<>();
stack.push(root);
while(!stack.empty()) {
root = stack.pop();
list.add(root);
if(root.children != null) {
for(Node child : root.children) {
if(child != null) {
stack.push(child);
}
}
}
}
return list;
}

Firstly, your preorder traversal code traverses children in reverse order. When you push the children onto the stack in order, they get popped in reverse order, leading to incorrect behaviour. It should be something like this:
static List<Node> preorderTraversal(Node root) {
List<Node> list = new ArrayList<>();
if (root == null) return list;
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.empty()) {
root = stack.pop();
list.add(root);
if (root.children != null) {
// iterate in reverse
for (int i = root.children.size() - 1; i >= 0; i--) {
Node child = root.children.get(i);
if (child != null) {
stack.push(child);
}
}
}
}
return list;
}
Before we look at modified preorder traversal, it is helpful to look at how to implement preorder traversal recursively:
static List<Node> preorderTraversalRecursive(Node root) {
ArrayList<Node> outList = new ArrayList<>();
preorderTraversalRecursive(root, outList);
return outList;
}
private static void preorderTraversalRecursive(Node root, ArrayList<Node> outList) {
if (root == null) {
return;
}
outList.add(root);
if (root.children != null) {
for (Node child : root.children) {
preorderTraversalRecursive(child, outList);
}
}
}
This code simple outputs a node before traversing its children.
To make this into a modified preorder traversal, you only need to keep track of a counter that is incremented before and after every node is processed, and record it before and after the children are processed in order to get left and right values. Here, the current count is returned by the method so that it can be changed during processing of child nodes and this updated value used for their parents' right value:
static List<MPTTNode> modifiedPreorderTraversalRecursive(Node root) {
ArrayList<MPTTNode> outList = new ArrayList<>();
modifiedPreorderTraversalRecursive(root, 0, outList);
return outList;
}
private static int modifiedPreorderTraversalRecursive(Node root, int counter, ArrayList<MPTTNode> outList) {
if (root == null) {
return counter;
}
counter++;
MPTTNode mpttNode = new MPTTNode(root.data, counter, 0); // right value is unknown, leave as 0 for now
outList.add(mpttNode);
if (root.children != null) {
for (Node child : root.children) {
// modify counter
counter = modifiedPreorderTraversalRecursive(child, counter, outList);
}
}
counter++;
mpttNode.right = counter;
return counter;
}
This can also be implemented iteratively:
static List<MPTTNode> modifiedPreorderTraversal(Node root) {
List<MPTTNode> list = new ArrayList<>();
if (root == null) return list;
Stack<Node> stack = new Stack<>();
Stack<Integer> pending = new Stack<>();
stack.push(root);
int counter = 0;
while (!stack.empty()) {
root = stack.pop();
if (root == null) {
int nodeIndex = pending.pop();
counter++;
list.get(nodeIndex).right = counter;
continue;
}
counter++;
pending.push(list.size());
list.add(new MPTTNode(root.data, counter, 0)); // right value is unknown, leave as 0 for now
stack.push(null);
if (root.children != null) {
// iterate in reverse
for (int i = root.children.size() - 1; i >= 0; i--) {
Node child = root.children.get(i);
if (child != null) {
stack.push(child);
}
}
}
}
return list;
}
This works by using a pending stack to keep track of the indices of parent nodes within the output list (list). It uses null values in the stack stack to signal that all children of a node have been processed and so the right value of their parent is known.
Here's all my code, including the same example tree as is used in the linked question:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
Node tree = new Node("Electronics",
Arrays.asList(
new Node("Televisions",
Arrays.asList(
new Node("Tube"),
new Node("LCD"),
new Node("Plasma")
)
),
new Node("Portable Electronics",
Arrays.asList(
new Node("MP3 Players", Collections.singletonList(
new Node("Flash")
)),
new Node("CD Players"),
new Node("2 Way Radios")
)
)
)
);
List<MPTTNode> list1 = Node.modifiedPreorderTraversal(tree);
List<MPTTNode> list2 = Node.modifiedPreorderTraversalRecursive(tree);
if (!list1.equals(list2)) {
throw new RuntimeException("Traversals not equal");
}
for (var node : list1) {
System.out.printf("%-30s left:%5d, right:%5d\n", node.data, node.left, node.right);
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Node {
String data;
List<Node> children;
public Node(String data, List<Node> children) {
this.data = data;
this.children = children;
}
public Node(String data) {
this.data = data;
}
static List<Node> preorderTraversal(Node root) {
List<Node> list = new ArrayList<>();
if (root == null) return list;
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.empty()) {
root = stack.pop();
list.add(root);
if (root.children != null) {
// iterate in reverse
for (int i = root.children.size() - 1; i >= 0; i--) {
Node child = root.children.get(i);
if (child != null) {
stack.push(child);
}
}
}
}
return list;
}
static List<MPTTNode> modifiedPreorderTraversal(Node root) {
List<MPTTNode> list = new ArrayList<>();
if (root == null) return list;
Stack<Node> stack = new Stack<>();
Stack<Integer> pending = new Stack<>();
stack.push(root);
int counter = 0;
while (!stack.empty()) {
root = stack.pop();
if (root == null) {
int nodeIndex = pending.pop();
counter++;
list.get(nodeIndex).right = counter;
continue;
}
counter++;
pending.push(list.size());
list.add(new MPTTNode(root.data, counter, 0)); // right value is unknown, leave as 0 for now
stack.push(null);
if (root.children != null) {
// iterate in reverse
for (int i = root.children.size() - 1; i >= 0; i--) {
Node child = root.children.get(i);
if (child != null) {
stack.push(child);
}
}
}
}
return list;
}
static List<Node> preorderTraversalRecursive(Node root) {
ArrayList<Node> outList = new ArrayList<>();
preorderTraversalRecursive(root, outList);
return outList;
}
private static void preorderTraversalRecursive(Node root, ArrayList<Node> outList) {
if (root == null) {
return;
}
outList.add(root);
if (root.children != null) {
for (Node child : root.children) {
preorderTraversalRecursive(child, outList);
}
}
}
static List<MPTTNode> modifiedPreorderTraversalRecursive(Node root) {
ArrayList<MPTTNode> outList = new ArrayList<>();
modifiedPreorderTraversalRecursive(root, 0, outList);
return outList;
}
private static int modifiedPreorderTraversalRecursive(Node root, int counter, ArrayList<MPTTNode> outList) {
if (root == null) {
return counter;
}
counter++;
MPTTNode mpttNode = new MPTTNode(root.data, counter, 0);
outList.add(mpttNode);
if (root.children != null) {
for (Node child : root.children) {
counter = modifiedPreorderTraversalRecursive(child, counter, outList);
}
}
counter++;
mpttNode.right = counter;
return counter;
}
}
import java.util.Objects;
public class MPTTNode {
String data;
int left;
int right;
public MPTTNode(String data, int left, int right) {
this.data = data;
this.left = left;
this.right = right;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MPTTNode mpttNode = (MPTTNode) o;
return left == mpttNode.left && right == mpttNode.right && Objects.equals(data, mpttNode.data);
}
}
Output:
Electronics left: 1, right: 20
Televisions left: 2, right: 9
Tube left: 3, right: 4
LCD left: 5, right: 6
Plasma left: 7, right: 8
Portable Electronics left: 10, right: 19
MP3 Players left: 11, right: 14
Flash left: 12, right: 13
CD Players left: 15, right: 16
2 Way Radios left: 17, right: 18

Related

how to create binary search tree 2d model from these codes?

hi im new to codings and i have to print my binary search tree in a 2d model but this codes only print the orders of number in order(left-root-right) such as when i insert 10, 9, 11, 8, it will print inorder (left root right) = 8,9,10,11. what method or codes should i add to create a 2d tree here. sorry idk how to properly put the codes here just look at it like it is only a one code only.
class binarySearchTree {
class Node {
int key;
Node left, right;
int data;
public Node(int data){
key = data;
left = right = null;
}
}
// BST root node
Node root;
// Constructor for BST =>initial empty tree
binarySearchTree(){
root = null;
}
//delete a node from BST
void deleteKey(int key) {
root = delete_Recursive(root, key);
}
//recursive delete function
Node delete_Recursive(Node root, int key) {
//tree is empty
if (root == null) return root;
//traverse the tree
if (key < root.key) //traverse left subtree
root.left = delete_Recursive(root.left, key);
else if (key > root.key) //traverse right subtree
root.right = delete_Recursive(root.right, key);
else {
// node contains only one child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// node has two children;
//get inorder successor (min value in the right subtree)
root.key = minValue(root.right);
// Delete the inorder successor
root.right = delete_Recursive(root.right, root.key);
}
return root;
}
int minValue(Node root) {
//initially minval = root
int minval = root.key;
//find minval
while (root.left != null) {
minval = root.left.key;
root = root.left;
}
return minval;
}
// insert a node in BST
void insert(int key) {
root = insert_Recursive(root, key);
}
//recursive insert function
Node insert_Recursive(Node root, int key) {
//tree is empty
if (root == null) {
root = new Node(key);
return root;
}
//traverse the tree
if (key < root.key) //insert in the left subtree
root.left = insert_Recursive(root.left, key);
else if (key > root.key) //insert in the right subtree
root.right = insert_Recursive(root.right, key);
// return pointer
return root;
}
void inorder() {
inorder_Recursive(root);
}
// recursively traverse the BST
void inorder_Recursive(Node root) {
if (root != null) {
inorder_Recursive(root.left);
System.out.print(root.key + " x ");
inorder_Recursive(root.right);
}
}
//PostOrder Traversal - Left:Right:rootNode (LRn)
void postOrder(Node node) {
if (node == null)
return;
// first traverse left subtree recursively
postOrder(node.left);
// then traverse right subtree recursively
postOrder(node.right);
// now process root node
System.out.print(node.key + " ");
}
// InOrder Traversal - Left:rootNode:Right (LnR)
void inOrder(Node node) {
if (node == null)
return;
//first traverse left subtree recursively
inOrder(node.left);
//then go for root node
System.out.print(node.key + " ");
//next traverse right subtree recursively
inOrder(node.right);
}
//PreOrder Traversal - rootNode:Left:Right (nLR)
void preOrder(Node node) {
if (node == null)
return;
//first print root node first
System.out.print(node.key + " ");
// then traverse left subtree recursively
preOrder(node.left);
// next traverse right subtree recursively
preOrder(node.right);
}
// Wrappers for recursive functions
void postOrder_traversal() {
postOrder(root); }
void inOrder_traversal() {
inOrder(root); }
void preOrder_traversal() {
preOrder(root); }
}
here i found this codes in stackoverflow, i want te output like this, i can use this but i dont know how can i make this as user input for the data and make it insert the integer into a tree not this manually inserted of the integer. thankyou very much to whoever put effort to understand my question and my situation as newbie.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BTreePrinterTest {
private static Node<Integer> test2() {
Node<Integer> root = new Node<Integer>(2);
Node<Integer> n11 = new Node<Integer>(3);
Node<Integer> n12 = new Node<Integer>(5);
Node<Integer> n21 = new Node<Integer>(2);
Node<Integer> n22 = new Node<Integer>(6);
Node<Integer> n23 = new Node<Integer>(9);
Node<Integer> n31 = new Node<Integer>(5);
root.left = n11;
root.right = n12;
n11.left = n21;
n11.right = n22;
n12.left = n23;
n12.right = n31;
return root;
}
public static void main(String[] args) {
BTreePrinter.printNode(test2());
}
}
class Node<T extends Comparable<?>> {
Node<T> left, right;
T data;
public Node(T data) {
this.data = data;
}
}
class BTreePrinter {
public static <T extends Comparable<?>> void printNode(Node<T> root) {
int maxLevel = BTreePrinter.maxLevel(root);
printNodeInternal(Collections.singletonList(root), 1, maxLevel);
}
private static <T extends Comparable<?>> void printNodeInternal(List<Node<T>> nodes, int level, int maxLevel) {
if (nodes.isEmpty() || BTreePrinter.isAllElementsNull(nodes))
return;
int floor = maxLevel - level;
int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
int firstSpaces = (int) Math.pow(2, (floor)) - 1;
int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
BTreePrinter.printWhitespaces(firstSpaces);
List<Node<T>> newNodes = new ArrayList<Node<T>>();
for (Node<T> node : nodes) {
if (node != null) {
System.out.print(node.data);
newNodes.add(node.left);
newNodes.add(node.right);
} else {
newNodes.add(null);
newNodes.add(null);
System.out.print(" ");
}
BTreePrinter.printWhitespaces(betweenSpaces);
}
System.out.println("");
for (int i = 1; i <= endgeLines; i++) {
for (int j = 0; j < nodes.size(); j++) {
BTreePrinter.printWhitespaces(firstSpaces - i);
if (nodes.get(j) == null) {
BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1);
continue;
}
if (nodes.get(j).left != null)
System.out.print("/");
else
BTreePrinter.printWhitespaces(1);
BTreePrinter.printWhitespaces(i + i - 1);
if (nodes.get(j).right != null)
System.out.print("\\");
else
BTreePrinter.printWhitespaces(1);
BTreePrinter.printWhitespaces(endgeLines + endgeLines - i);
}
System.out.println("");
}
printNodeInternal(newNodes, level + 1, maxLevel);
}
private static void printWhitespaces(int count) {
for (int i = 0; i < count; i++)
System.out.print(" ");
}
private static <T extends Comparable<?>> int maxLevel(Node<T> node) {
if (node == null)
return 0;
return Math.max(BTreePrinter.maxLevel(node.left), BTreePrinter.maxLevel(node.right)) + 1;
}
private static <T> boolean isAllElementsNull(List<T> list) {
for (Object object : list) {
if (object != null)
return false;
}
return true;
}
}
btw im learning this by my own, i tried merging the two codes but it gives me error i cant fix it.
I should have not made the whole exercise for you, so please try to understand the code. Tell me if something is not clear for you.
public static void main(String[] args) throws IOException {
System.out.println("Write your input");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String lines = br.readLine();
binarySearchTree b = new binarySearchTree();
b.input(lines);
b.print();
}
These functions go to binarySearchTree.
protected void printRecursive(Node node, int depth) {
System.out.println("");
for(int i = 0; i<depth; i++) {
System.out.print(" ");
}
System.out.print(node.key);
if(node.left != null) {
printRecursive(node.left, depth + 1);
}
if(node.right != null) {
printRecursive(node.right, depth + 1);
}
}
public void input(String s) throws IOException {
String[] strs = s.trim().split("\\s+");
for (int i = 0; i < strs.length; i++) {
insert(Integer.parseInt(strs[i]));
}
}
Also i used this answer in my code.

How to extract the maximum value from a N-ary tree using recursion in Java

I am trying to find a way to keep track of the maximum value in my code. Right now I know that my for-loop looks at every node in my tree and compares it to the last max. But my issue is as the function calls are popped off the stack I end up only evaluating the first set of children and my max is chosen from those rather than the whole tree. The nodes can have any number of children and a payload.
public static Integer max(TreeNode<Integer> root) {
if (root == null) {
return null;
}
int maximum = root.payload;
if (root != null) {
for (int i = 0; i < root.children.size(); i++) {
if (root.children.get(i).payload > maximum) {
maximum = root.children.get(i).payload;
}
max(root.children.get(i));
}
}
return maximum;
}
tree example:
1
4 6 7
9 10 11 12
Max:12
My Max:7
public class TreeNode<T> {
public ArrayList<TreeNode<T>> children;
public T payload;
}
You could try this
public static Integer max(TreeNode<Integer> root) {
if (root == null) {
return null;
}
if (root.children == null) {
return null;
}
int maximum = root.payload;
for (int i = 0; i < root.children.size(); i++) {
if (root.children.get(i).payload > maximum) {
maximum = root.children.get(i).payload;
}
Integer childsMaximun = max(root.children.get(i));
if ((childsMaximun != null) && (childsMaximun > maximum)){
maximum = childsMaximun;
}
}
return maximum;
}
You need to add contructor to initialize an ArrayList, try my TreeNode class:
public class TreeNode<T> {
public T payload;
public List<TreeNode<T>> children;
public TreeNode(T payload) {
this.payload = payload;
children = new ArrayList<>();
}
public void addChildren(T... childs){
for (T child: childs){
TreeNode<T> node = new TreeNode<>(child);
children.add(node);
}
}
public void addChildren(TreeNode<T>... childs){
for (TreeNode<T> child: childs){
children.add(child);
}
}
public static TreeNode<Integer> DEFAULT(){
TreeNode<Integer> root7 = new TreeNode<>(7);
root7.addChildren(10,11,12);
TreeNode<Integer> root6 = new TreeNode<>(6);
TreeNode<Integer> root4 = new TreeNode<>(4);
root4.addChildren(9);
TreeNode<Integer> root1 = new TreeNode<>(1);
root1.addChildren(root7, root6, root4);
return root1;
}
}
public static int maximum(TreeNode<Integer> t) {
if (t == null) {
return 0;
}
int max = t.payload;
List<TreeNode<Integer>> children = t.children;
if (children != null && children.size() > 0) {
for (TreeNode<Integer> e: children) {
// recursive call here
int maxNext = maximum(e);
if (maxNext > max) max = maxNext;
}
}
return max;
}
Your solution is not recursive. You are only checking the max in the children of root, not deeper than that.
You should traverse through all the children and keep on updating max. I have done it using recursion, there can be many other ways to traverse a tree.

How do i Modify the append and insert operations in a linked list?

Hello i have an Assignment to modify a java program :
1- Makeing the Append operation be always in the front of the List :
LinkedList myList = new LinkedList(5);
myList.append(7);
myList.print();
it should be like this :
7 -> 5 -> null
instead of :
5 -> 7 -> null
2- Insert operation will be always as follows: any new added element using insert operation
should be after the current maximum element in the List
LinkedList myList = new LinkedList(2);
myList.append(5);
myList.insert(1);
myList.insert(4);
myList.print();
it should be :
5 -> 4 -> 1 -> 2 -> null
instead of :
5 -> 4 -> 2 -> 1 -> null
here is the code
class Node {
Node next;
int num ;
public Node(int val) {
num = val;
next = null;
}
}
public class LinkedList {
Node head;
public LinkedList(int val) {
head = new Node(val);
}
public void append(int val) {
Node tmpNode = new Node(val);
tmpNode.num = val;
tmpNode.next = head;
head = tmpNode;
}
public void insert(int val) {
Node currentNode = head;
Node nextNode = head.next;
if (currentNode.num < val) {
Node tmpNode = head;
head = new Node(val);
head.next = tmpNode;
return;
}
if (nextNode != null && nextNode.num < val) {
currentNode.next = new Node(val);
currentNode.next.next = nextNode;
return;
}
while (nextNode != null && nextNode.num < val) {
currentNode = nextNode;
nextNode = nextNode.next;
}
currentNode.next = new Node(val);
currentNode.next.next = nextNode;
}
public void delete(int val) {
Node prevNode = null;
Node currNode = head;
if (head.num == val) {
head = head.next;
return;
}
while (currNode != null && currNode.num != val) {
prevNode = currNode;
currNode = currNode.next;
}
if (currNode == null) {
System.out.println("A node with that value does not exist.");
} else {
prevNode.next = currNode.next;
}
}
public void print() {
Node tmpNode = head;
while (tmpNode != null) {
System.out.print(tmpNode.num + " -> ");
tmpNode = tmpNode.next;
}
System.out.print("null" + "\n");
}
public static void main(String[] args) {
LinkedList myList = new LinkedList(2);
myList.append(5);
myList.insert(10);
myList.insert(4);
myList.print();
}
}
I have tried to do it but i am not sure i did it right
Thanks for your time !

Print all nodes that are N level above all Leaf Nodes

I need to print all the nodes that are N level above all Leaf Nodes. I tried below approach, but now I am stuck and unable to proceed. Please help. I need to code only using Java 7 and no other versions.
For example, I have this path 1 --> 2 --> 3 --> 4, so in this case assuming 4 is my leaf node, node 3 is 1 level above 4 and node 2 is 2 levels above leaf node 4 and node 1 is 3 levels above leaf node 4.
Note: Please use only Java 7.
public class NNodeBeforeLeaf {
static Node root;
static class Node {
int data;
Node left, right;
Node(int data){
this.data = data;
left=right=null;
}
}
public static boolean isLeaf(Node n){
if(n.right == null && n.left == null)
return true;
return false;
}
public static void main(String[] args) {
int level = 2; // level N
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(8);
print(root, 0, level);
}
public static void print(Node n, int currLevel, int level){
if(n == null){
return;
}
if(!isLeaf(n)){
print(n.left, currLevel + 1, level);
print(n.right, currLevel + 1, level);
}
printNode(n, currLevel, level);
}
public static void printNode(Node n, int currLevel, int level){}
}
You have a miss in your structure to do this a Node know its child but not is parent so you need to build a structure that will give you this link : here is my proposition : i build a map that give me the parent associate to a node with method buildParentMap this function already list all the leaf in one pass to avoid a double iteration on your tree then i use this map to go up as many time as asked on each leaf i list just before here is a snippet
be carefull this code work but there is no security if your are trying to upper that root or if the same node is present in too child (but 2 Node with the same data wont be a problem)
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
public class NNodeBeforeLeaf {
static Node root;
static class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}
#Override
public String toString() {
return "Node : " + data;
}
}
public static boolean isLeaf(Node n) {
if (n.right == null && n.left == null)
return true;
return false;
}
public static void main(String[] args) {
int level = 2; // level N
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(8);
print(root, 0, level);
int levelToUp = 1;
HashSet<Node> result = getUpper(levelToUp, root);
System.out.println(Arrays.toString(result.toArray()));
}
private static HashSet<Node> getUpper(int levelToUp, Node node) {
HashMap<Node, Node> parenttMap = new HashMap<Node, Node>();
LinkedList<Node> leafs = new LinkedList<Node>();
buildParentMap(node, parenttMap, leafs);
HashSet<Node> result = new HashSet<>();
for (Node leaf : leafs) {
result.add(getUpperLevel(leaf, levelToUp, parenttMap));
}
return result;
}
private static Node getUpperLevel(Node leaf, int i, HashMap<Node, Node> parenttMap) {
Node tmp = leaf;
while (i > 0) {
i--;
tmp = parenttMap.get(tmp);
}
return tmp;
}
private static void buildParentMap(Node root2, HashMap<Node, Node> hashMap, LinkedList<Node> leaf) {
if (root2 == null) {
return;
} else if (isLeaf(root2)) {
leaf.add(root2);
} else {
hashMap.put(root2.left, root2);
buildParentMap(root2.left, hashMap, leaf);
hashMap.put(root2.right, root2);
buildParentMap(root2.right, hashMap, leaf);
}
}
public static void print(Node n, int currLevel, int level) {
if (n == null) {
return;
}
printNode(n, currLevel, level);
if (!isLeaf(n)) {
print(n.left, currLevel + 1, level);
print(n.right, currLevel + 1, level);
}
}
public static void printNode(Node n, int currLevel, int level) {
String output = "";
for (int i = 0; i < currLevel; i++) {
output += "\t";
}
System.out.println(output + n);
}
}
PLEASE READ MY COMMENT FIRST
Since the nodes in your program store data only for the nodes below them, I couldn't really find a way of actually going up the tree ':), but I could think of this work around, basically what you can do is, each time you need to go up by n levels you can traverse down from the root to (curLevel - n) here is a sample program that does this (it prints all the nodes at a level which is n above the current level, i hope this is what you meant):
class tree{
static class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left = null;
right = null;
}
}
static Node root;
public static boolean isLeaf(Node n){
if(n.left == null && n.right == null)
return true;
return false;
}
public static void goDownTillLevel(Node n, int level){
int l = level;
if(n != null){
if(level == 0) {
System.out.println(n.data);
}
else{
if(!isLeaf(n)){
goDownTillLevel(n.left, --level);
level = l; //since by the time the above function calls finished, level had been reduced to 0
goDownTillLevel(n.right, --level);
}
}
}
}
public static void nLevelsAbove(Node n, int curLevel, int level){
goDownTillLevel(root, (curLevel - level - 1));
}
public static void main(String args[]){
int curLevel = 0;
root = new Node(1);
curLevel++;
root.left = new Node(2);
root.right = new Node(2);
curLevel++;
root.left.left = new Node(3);
root.left.right = new Node(3);
root.right.left = new Node(3);
Node n = new Node(3);
root.right.right = n;
curLevel++;
nLevelsAbove(n, curLevel, 1);
}
}
Though I'd like to add that if going up is one of your concerns, don't use this node structure, instead add another variable to the node, a reference to the node right above it, that way this could be made much easier and shorter.
The output of the above code is:
2
2
I think that the implementation of public static boolean isLeaf(Node n) is wrong, it should check only if right is null otherwise it is not a node, either a leaf
To get the current level of node, you can try with this code
int level = 0;
while(node.right != null) {
level++;
node = node.right;
}
System.out.println("current level node: " + level);
Your structure is not able to determine the height of the current node, except when traversing from bottom to top. In order to achieve this, you have to traverse to the leafs first.
Each recursion (bottom up now) should then return it's heights. As youre not stating if your tree is a full binary tree, a node can have multiple heights depending on his children. If the heights match the desired height, the node can be printed.
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class NNodeBeforeLeaf {
static Node root;
static class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}
}
public static boolean isLeaf(Node n) {
return n.right == null && n.left == null;
}
public static void main(String[] args) {
int level = 2; // level N
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(8);
print(root, level);
}
public static void print(Node n, int level) {
traversAndPrint(n, level);
}
private static Set<Integer> traversAndPrint(Node n, int levelToPrint) {
if (isLeaf(n)) return Collections.singleton(0); // We are a leaf, so we have height 0
final Set<Integer> childrenHeights = new HashSet<>();
// are no leaf, so we have to get the heights of our children
if (n.right != null) childrenHeights.addAll(traversAndPrint(n.right, levelToPrint));
if (n.left != null) childrenHeights.addAll(traversAndPrint(n.left, levelToPrint));
assert !childrenHeights.isEmpty();
// And increase these heights
final Set<Integer> selfHeights = new HashSet<>();
for (Integer childrenHeigth : childrenHeights) {
final int selfHeight = childrenHeigth + 1;
selfHeights.add(selfHeight);
}
// If we have the desired height, print
if (selfHeights.contains(levelToPrint)) printNode(n);
return selfHeights; // return our heights
}
public static void printNode(Node n) {
// Do whatever you want
System.out.println(n.data);
}
}
I found another approach. I put all nodes in a list. For each level up I remove the leaf nodes in that list. A leaf node in the list is defined as a node with left=null and right=null or if they are not null left and right should not be in the list. After the level ups I print the now leaf nodes in the list.
public class NNodeBeforeLeaf {
static Node root;
static class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}
}
public static boolean isLeaf(Node n) {
if ((n.right == null) && (n.left == null)) {
return true;
}
return false;
}
public static void main(String[] args) {
int level = 2; // level N
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(8);
printNodes(getNodesNLevelAboveLeafs(root, level));
}
public static void printNodes(List<Node> nodes) {
for (Node n : nodes) {
System.out.println(n.data);
}
}
public static List<Node> getNodesNLevelAboveLeafs(Node root, int level) {
List<Node> allNodes = listAllNodes(root);
for (int i = 0; i < level; i++) {
allNodes.removeAll(getLeafNodes(allNodes));
}
return getLeafNodes(allNodes);
}
private static List<Node> getLeafNodes(List<Node> allNodes) {
List<Node> leafs = new ArrayList<>();
for (Node n : allNodes) {
if (((n.left == null) || !allNodes.contains(n.left))
&& ((n.right == null) || !allNodes.contains(n.right))) {
leafs.add(n);
}
}
return leafs;
}
private static List<Node> listAllNodes(Node node) {
List<Node> nodes = new ArrayList<>();
nodes.add(node);
if (node.left != null) {
nodes.addAll(listAllNodes(node.left));
}
if (node.right != null) {
nodes.addAll(listAllNodes(node.right));
}
return nodes;
}
}

Binary Tree printing all paths

Can the below design can be further optimized? I have used a hashmap and a Queue. SO space complexity will be O(n) and runtime will be O(n)
public class PrintAllRootToLeaves {
public static void print(BinaryTreeNode root) {
Queue nodesQ = new Queue();
HashMap hMap = new HashMap();
BinaryTreeNode head = root;
String tempVal ;
hMap.put(head,String.valueOf(head.getData()));
while (head != null) {
BinaryTreeNode left = head.getLeft();
BinaryTreeNode right = head.getRight();
if (left != null) {
if ((tempVal = (String) hMap.get(head)) != null) {
hMap.put(left,tempVal + left.getData());
}
nodesQ.enqueue(left);
}
if (right != null) {
if ((tempVal = (String) hMap.get(head)) != null) {
hMap.put(right,tempVal + right.getData());
}
nodesQ.enqueue(right);
}
if (right != null && left != null) {
hMap.remove(head);
}
head = (BinaryTreeNode) nodesQ.dequeue();
}
System.out.println("-----------Printing all routes ---------->" + hMap.values());
}
}
public class BinaryTree {
private class Node {
final int key;
final int value;
Node left;
Node Right;
public Node (Node node, int pKey, int qValue) {
key = pKey;
value = qValue;
if (node != null && node.key < pKey) {
left = node;
}
else if (node != null) {
Right = node;
}
}
}
public void preOrderTraversal(Node pNode,String path){
if (path == null) {
path = "";
}
if (pNode != null) {
path = path+" "+String.valueOf(pNode.key);
//If you remove the modulo check it will print all the paths.
if (pNode.key%5 == 0) {
System.out.println(path);
}
preOrderTraversal(pNode.left,path);
preOrderTraversal(pNode.Right,path);
}
}
/**
* #param args
*/
public static void main(String[] args) {
Node node1 = new BinaryTree().new Node(null, 5, 2);
Node node2 = new BinaryTree().new Node(null, 10, 25);
Node node3 = new BinaryTree().new Node(node1, 7, 50);
node3.Right = node2;
Node root = new BinaryTree().new Node(node3, 15, 6);
Node node4 = new BinaryTree().new Node(null, 30, 8);
Node node5 = new BinaryTree().new Node(node4, 20, 7);
root.Right = node5;
//This will print paths only divisable by 5
new BinaryTree().preOrderTraversal(root,null);
}
}

Categories