Skip to content

Commit

Permalink
trees-revise
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustubhdeokar authored and ImgBotApp committed Aug 30, 2024
1 parent 1a4d475 commit 4b80f13
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 2-ij/src/src/tree/StringToTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package tree;public class StringToTree {
}
2 changes: 2 additions & 0 deletions 2-ij/src/src/tree/Traversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package tree;public class Traversal {
}
2 changes: 2 additions & 0 deletions 2-ij/src/src/tree/TreeToString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package tree;public class TreeToString {
}
101 changes: 101 additions & 0 deletions 2-ij/src/src/util/BurnATree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package tree;

import java.util.PriorityQueue;

public class BurnATree {

class MapDistToNode {
int distance;
int node;

public MapDistToNode(int distance, int node) {
this.distance = distance;
this.node = node;
}

@Override
public String toString() {
return "distance=" + distance +
", node=" + node;
}
}

public static void main(String[] args) {

BurnATree nodesAtDistK = new BurnATree();
TreeNode tree = nodesAtDistK.createTree();
PriorityQueue<MapDistToNode> queue = new PriorityQueue<>((c1, c2) -> Integer.compare(c1.distance, c2.distance));
nodesAtDistK.pathToTheNode(tree, tree.left.right.left, queue);
while (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}


private int pathToTheNode(TreeNode tree, TreeNode target, PriorityQueue<MapDistToNode> queue) {

if (tree == null) return -1;

if (tree == target) {
System.out.println("Found:node");
findMaxNodeFromNode(target, 0, queue);
return 1; // start counting from target node.
}

int leftDistance = pathToTheNode(tree.left, target, queue);
// that means the target node is found, and we are calculating the distance of the ancestors.
System.out.println("curr:" + tree.val + "left:" + leftDistance);
if (leftDistance >= 0) {
leftDistance += 1;
findMaxNodeFromNode(tree.right, leftDistance, queue); // to find nodes at distance k, for ancestors different siblings.
}
int rightDistance = -1;
if (leftDistance < 0) {
rightDistance = pathToTheNode(tree.right, target, queue);
System.out.println("curr:" + tree.val + "right:" + rightDistance);
if (rightDistance >= 0) {
rightDistance += 1;
findMaxNodeFromNode(tree.left, rightDistance, queue);
}
}

return Integer.max(leftDistance, rightDistance);

}

private void findMaxNodeFromNode(TreeNode target, int dist, PriorityQueue<MapDistToNode> queue) {
if (target == null) {
return;
}
queue.add(new MapDistToNode(dist, target.val));
if (queue.size() > 1) {
MapDistToNode remove = queue.remove();
System.out.println("removed:" + remove.node);
}
findMaxNodeFromNode(target.left, dist + 1, queue);
findMaxNodeFromNode(target.right, dist + 1, queue);
}

public TreeNode createTree() {
TreeNode node = new TreeNode(1);
node.left = new TreeNode(2);
node.right = new TreeNode(3);

node.left.left = new TreeNode(4);
node.left.right = new TreeNode(5);

node.left.right.left = new TreeNode(6);
node.left.right.right = new TreeNode(7);

node.left.right.left.left = new TreeNode(9);

node.left.right.right.left = new TreeNode(8);
node.left.right.right.right = new TreeNode(10);

node.left.right.left.left.left = new TreeNode(11);
node.left.right.left.left.right = new TreeNode(12);

return node;
}

}
91 changes: 91 additions & 0 deletions 2-ij/src/src/util/NodesAtDistK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package tree;

import java.util.ArrayList;
import java.util.List;

public class NodesAtDistK {

public static void main(String[] args) {

NodesAtDistK nodesAtDistK = new NodesAtDistK();
TreeNode tree = nodesAtDistK.createTree();
List<Integer> list = new ArrayList<>();

nodesAtDistK.findNodesAtDistK(tree, tree.left.right.left, 4, list);


System.out.println("list:" + list);
}


private int findNodesAtDistK(TreeNode tree, TreeNode target, int k, List<Integer> list) {

if (tree == null) return -1;

if (tree == target) {
findChildrenAtDistK(target, k, list);
return 0; // start counting from target node.
}

int leftDistance = findNodesAtDistK(tree.left, target, k, list);
// that means the target node is found, and we are calculating the distance of the ancestors.
System.out.println("curr:" + tree.val + "left:" + leftDistance);
if (leftDistance >= 0) {
leftDistance += 1;
if (leftDistance < k) {
findChildrenAtDistK(tree.right, k - leftDistance - 1, list); // to find nodes at distance k, for ancestors different siblings.
}
}

int rightDistance = findNodesAtDistK(tree.right, target, k, list);
System.out.println("curr:" + tree.val + "right:" + rightDistance);
if (rightDistance >= 0) {
rightDistance += 1;
if (rightDistance < k) {
findChildrenAtDistK(tree.left, k - leftDistance - 1, list);
}
}

int distance = Integer.max(leftDistance, rightDistance);
if (distance == k) {
list.add(tree.val);
}
return distance;

}

private void findChildrenAtDistK(TreeNode target, int k, List<Integer> list) {
if (target == null) {
return;
}
if (k == 0) {
list.add(target.val);
return;
}
findChildrenAtDistK(target.left, k - 1, list);
findChildrenAtDistK(target.right, k - 1, list);
}

public TreeNode createTree() {
TreeNode node = new TreeNode(1);
node.left = new TreeNode(2);
node.right = new TreeNode(3);

node.left.left = new TreeNode(4);
node.left.right = new TreeNode(5);

node.left.right.left = new TreeNode(6);
node.left.right.right = new TreeNode(7);

node.left.right.left.left = new TreeNode(9);

node.left.right.right.left = new TreeNode(8);
node.left.right.right.right = new TreeNode(10);

node.left.right.left.left.left = new TreeNode(11);
node.left.right.left.left.right = new TreeNode(12);

return node;
}

}
59 changes: 59 additions & 0 deletions 2-ij/src/src/util/PathToNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tree;

import java.util.ArrayList;
import java.util.List;

public class PathToNode {

public static void main(String[] args) {

PathToNode pathToNode = new PathToNode();
TreeNode tree = pathToNode.createTree();
List<Integer> result = new ArrayList<>();
boolean[] found = {false};
pathToNode.findPathToNode(tree, tree.left.right.right.left, result, found);
System.out.println(result);
}

private TreeNode findPathToNode(TreeNode tree, TreeNode target, List<Integer> result, boolean[] found) {
if (tree == null || found[0]) return null;
System.out.println("adding:" + tree.val);
result.add(tree.val);
if (tree == target) {
found[0] = true;
return target;
}
TreeNode left = findPathToNode(tree.left, target, result, found);
TreeNode right = findPathToNode(tree.right, target, result, found);

if (!found[0]) {
System.out.println("removing:" + result.get(result.size() - 1));
result.remove(result.size() - 1);
}
if (left != null) return left;
return right;
}


public TreeNode createTree() {
TreeNode node = new TreeNode(1);
node.left = new TreeNode(2);
node.right = new TreeNode(3);

node.left.left = new TreeNode(4);
node.left.right = new TreeNode(5);

node.left.right.left = new TreeNode(6);
node.left.right.right = new TreeNode(7);

node.left.right.left.left = new TreeNode(9);

node.left.right.right.left = new TreeNode(8);
node.left.right.right.right = new TreeNode(10);

node.left.right.left.left.left = new TreeNode(11);
node.left.right.left.left.right = new TreeNode(12);

return node;
}
}
75 changes: 75 additions & 0 deletions 2-ij/src/src/util/Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tree;

import java.util.Stack;

public class Tree {


public void printInorder(TreeNode root, TreeNode[] prev, TreeNode[] wrongNodes) {

if (root == null)
return;
printInorder(root.left, prev, wrongNodes);
if (prev[0].val >= root.val) {
if (wrongNodes[0] == null) wrongNodes[0] = prev[0];
if (wrongNodes[0]!=null) wrongNodes[1] = root;
}
System.out.println(prev[0].val + " " + root.val);
prev[0] = root;
printInorder(root.right, prev, wrongNodes);
}

public void inorderIterative(TreeNode root) {

Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
while (!stack.isEmpty() || curr != null) {
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
while (!stack.isEmpty() && stack.peek().right == null) {
System.out.print(stack.pop().val + " ");

}
if (!stack.isEmpty()) {
TreeNode pop = stack.pop();
System.out.print(pop.val + " ");
curr = pop.right;
}
}

}


public TreeNode getTreeDataSet1() {
TreeNode treeNode = new TreeNode(2);
treeNode.left = new TreeNode(3);
treeNode.right = new TreeNode(1);
// treeNode.left.left = new TreeNode(3);
// treeNode.left.right = new TreeNode(4);
// treeNode.right.left = new TreeNode(6);
// treeNode.right.right = new TreeNode(8);

/*
10
/ \
50 60
/ \ \
70 20 100
*/

return treeNode;
}

public static void main(String[] args) {

Tree tree = new Tree();
TreeNode head = tree.getTreeDataSet1();
TreeNode[] prev = {new TreeNode(Integer.MIN_VALUE)};
TreeNode[] wrongNodes = {null, null};
tree.printInorder(head, prev, wrongNodes);
System.out.println(wrongNodes[0].val + " " + wrongNodes[1].val);
}

}
Loading

0 comments on commit 4b80f13

Please sign in to comment.