-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGfg.java
More file actions
125 lines (113 loc) · 3.35 KB
/
Copy pathGfg.java
File metadata and controls
125 lines (113 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Java program to demonstrate insert operation
// in binary search tree with parent pointer
import java.util.*;
class GfG {
static class Node
{
int key;
Node left, right, parent;
}
// A utility function to create a new BST Node
static Node newNode(int item)
{
Node temp = new Node();
temp.key = item;
temp.left = null;
temp.right = null;
temp.parent = null;
return temp;
}
static Node findNode(Node root, int myNode) {
if(root == null) {
return null;
}
if(myNode == root.key) {return root;}
Node nodeFound = findNode(root.left, myNode);
if(findNode(root.left, myNode) == null) {
nodeFound = findNode(root.right, myNode);
};
return nodeFound;
}
static void findDistanceNodes(Node root, int distance, Node currentNode, ArrayList<Integer> solution, Map<Integer, Boolean> visited) {
if(root == null || distance < 0 || visited.get(root.key)) {return;}
visited.put(root.key, true);
if(distance == 0) {
solution.add(root.key);
return;
}
findDistanceNodes(root.left, distance - 1, currentNode, solution, visited);
findDistanceNodes(root.right, distance - 1, currentNode, solution, visited);
findDistanceNodes(root.parent, distance - 1, currentNode, solution, visited);
}
public static ArrayList<Integer> KDistanceNodes(Node root, int target , int k)
{
Node currentNode = findNode(root, target);
ArrayList<Integer> solution = new ArrayList<Integer>();
Map<Integer, Boolean> visited = new HashMap<Integer, Boolean>();
findDistanceNodes(currentNode, k, currentNode, solution, visited);
return solution;
}
// A utility function to do inorder traversal of BST
static void inorder(Node root)
{
if (root != null)
{
inorder(root.left);
System.out.print("Node : "+ root.key + " , ");
if (root.parent == null)
System.out.println("Parent : NULL");
else
System.out.println("Parent : " + root.parent.key);
inorder(root.right);
}
}
/* A utility function to insert a new Node with
given key in BST */
static Node insert(Node node, int key)
{
/* If the tree is empty, return a new Node */
if (node == null) return newNode(key);
/* Otherwise, recur down the tree */
if (key < node.key)
{
Node lchild = insert(node.left, key);
node.left = lchild;
// Set parent of root of left subtree
lchild.parent = node;
}
else if (key > node.key)
{
Node rchild = insert(node.right, key);
node.right = rchild;
// Set parent of root of right subtree
rchild.parent = node;
}
/* return the (unchanged) Node pointer */
return node;
}
// Driver Program to test above functions
public static void main(String[] args)
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
Node root = null;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
insert(root, 90);
// print iNoder traversal of the BST
// inorder(root);
ArrayList<Integer> nd = KDistanceNodes(root, 70, 2);
for(int i = 0; i < nd.size(); i++) {
System.out.println(nd.get(i));
}
}
}