0% found this document useful (0 votes)
2 views3 pages

2

The document contains a Java program that constructs a binary tree from an array and allows for the removal of a specified node. It defines classes for the tree nodes and pairs, and includes methods for constructing the tree, displaying it, finding the maximum value, and removing a node. The main method reads input to create the tree and perform the removal operation, then displays the updated tree structure.

Uploaded by

rakshit2000.rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

2

The document contains a Java program that constructs a binary tree from an array and allows for the removal of a specified node. It defines classes for the tree nodes and pairs, and includes methods for constructing the tree, displaying it, finding the maximum value, and removing a node. The main method reads input to create the tree and perform the removal operation, then displays the updated tree structure.

Uploaded by

rakshit2000.rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

*;
import java.util.*;

public class Main {


public static class Node {
int data;
Node left;
Node right;

Node(int data, Node left, Node right) {


this.data = data;
this.left = left;
this.right = right;
}
}

public static class Pair {


Node node;
int state;

Pair(Node node, int state) {


this.node = node;
this.state = state;
}
}

public static Node construct(Integer[] arr) {


Node root = new Node(arr[0], null, null);
Pair rtp = new Pair(root, 1);

Stack<Pair> st = new Stack<>();


st.push(rtp);

int idx = 0;
while (st.size() > 0) {
Pair top = st.peek();
if (top.state == 1) {
idx++;
if (arr[idx] != null) {
top.node.left = new Node(arr[idx], null, null);
Pair lp = new Pair(top.node.left, 1);
st.push(lp);
} else {
top.node.left = null;
}

top.state++;
} else if (top.state == 2) {
idx++;
if (arr[idx] != null) {
top.node.right = new Node(arr[idx], null, null);
Pair rp = new Pair(top.node.right, 1);
st.push(rp);
} else {
top.node.right = null;
}

top.state++;
} else {
st.pop();
}
}

return root;
}

public static void display(Node node) {


if (node == null) {
return;
}

String str = "";


str += node.left == null ? "." : node.left.data + "";
str += " <- " + node.data + " -> ";
str += node.right == null ? "." : node.right.data + "";
System.out.println(str);

display(node.left);
display(node.right);
}

public static int max(Node node) {


if(node.right == null){
return node.data;
} else {
return max(node.right);
}
}

public static Node remove(Node node, int data) {


if (node == null) {
return null;
}

if (data > node.data) {


node.right = remove(node.right, data);
return node;
} else if (data < node.data) {
node.left = remove(node.left, data);
return node;
} else {
if(node.left == null && node.right == null){
return null;
} else if(node.left == null){
return node.right;
} else if(node.right == null){
return node.left;
} else {
int max = max(node.left);
node.data = max;
node.left = remove(node.left, max);
return node;
}
}
}

public static void main(String[] args) throws Exception {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Integer[] arr = new Integer[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
if (values[i].equals("n") == false) {
arr[i] = Integer.parseInt(values[i]);
} else {
arr[i] = null;
}
}

int data = Integer.parseInt(br.readLine());

Node root = construct(arr);


root = remove(root, data);

display(root);
}

You might also like