public class RecoverBinarySearchTree {
static Node temp1;
static Node temp2;
static Node previousNode=null,firstStartNode=null,LastEndNode=null;
public static void main(String[] args) {
int[] arr = {10,5,4,7,15,14,17};
Node root = null;
for(int val:arr) {
root = insertNode(root, val);
}
System.out.println("*****************original***********");
printInorder(root);
temp1.value = 15; //replace from 5
temp2.value = 5; //replace from 15
System.out.println("*****************after replace***********");
printInorder(root);
System.out.println("*****************recover***********");
findSegments(root);
int val = firstStartNode.value;
firstStartNode.value = LastEndNode.value;
LastEndNode.value = val;
printInorder(root);
}
static Node insertNode(Node root, Integer value) {
Node newNode = new Node(value);
if (root == null) {
return newNode;
} else {
if (value <= root.value) {
root.left = insertNode(root.left, value);
if(root.left.value==5) {
temp1 = root.left;
}
if(root.left.value==15) {
temp2 = root.left;
}
} else {
root.right = insertNode(root.right, value);
if(root.right.value==5) {
temp1 = root.right;
}
if(root.right.value==15) {
temp2 = root.right;
}
}
return root;
}
}
static void findSegments(Node root) {
if(null==root) {
return;
}
findSegments(root.left);
//logic
if(previousNode!=null) {
if(previousNode.value>root.value) {
if(null==firstStartNode) {
firstStartNode = previousNode;
}
LastEndNode = root;
}
}
previousNode = root;
findSegments(root.right);
}
static void printInorder(Node root) {
if(null!=root) {
printInorder(root.left);
System.out.println(root.value);
printInorder(root.right);
}else {
return;
}
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
output:
*****************original***********
4
5
7
10
14
15
17
*****************after replace***********
4
15
7
10
14
5
17
*****************recover***********
4
5
7
10
14
15
17
static Node temp1;
static Node temp2;
static Node previousNode=null,firstStartNode=null,LastEndNode=null;
public static void main(String[] args) {
int[] arr = {10,5,4,7,15,14,17};
Node root = null;
for(int val:arr) {
root = insertNode(root, val);
}
System.out.println("*****************original***********");
printInorder(root);
temp1.value = 15; //replace from 5
temp2.value = 5; //replace from 15
System.out.println("*****************after replace***********");
printInorder(root);
System.out.println("*****************recover***********");
findSegments(root);
int val = firstStartNode.value;
firstStartNode.value = LastEndNode.value;
LastEndNode.value = val;
printInorder(root);
}
static Node insertNode(Node root, Integer value) {
Node newNode = new Node(value);
if (root == null) {
return newNode;
} else {
if (value <= root.value) {
root.left = insertNode(root.left, value);
if(root.left.value==5) {
temp1 = root.left;
}
if(root.left.value==15) {
temp2 = root.left;
}
} else {
root.right = insertNode(root.right, value);
if(root.right.value==5) {
temp1 = root.right;
}
if(root.right.value==15) {
temp2 = root.right;
}
}
return root;
}
}
static void findSegments(Node root) {
if(null==root) {
return;
}
findSegments(root.left);
//logic
if(previousNode!=null) {
if(previousNode.value>root.value) {
if(null==firstStartNode) {
firstStartNode = previousNode;
}
LastEndNode = root;
}
}
previousNode = root;
findSegments(root.right);
}
static void printInorder(Node root) {
if(null!=root) {
printInorder(root.left);
System.out.println(root.value);
printInorder(root.right);
}else {
return;
}
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
output:
*****************original***********
4
5
7
10
14
15
17
*****************after replace***********
4
15
7
10
14
5
17
*****************recover***********
4
5
7
10
14
15
17
No comments:
Post a Comment