Question

In: Computer Science

write a method in java for a binary search tree that receives a node as input...

write a method in java for a binary search tree that receives a node as input and returns the successor node.

Solutions

Expert Solution

In a Binary search tree (BST) the successor node is the one which comes next to the given node in the Inorder traversal of the nodes of BST.

For eg, For an inorder traversal of aBST: 4-2-5-1-3-6, the successor of node 2 will be node 5.

Inorder works as: Travesing the leftmost nodes then the parent node of the left most node and then he right most node:

So, Left-Parent-Right

CODE:


public class Main
{
static class Node
{
int data;
Node left, right;
}
  

static Node temp = new Node();
  
//creating new node when called
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}

// recursive function to find the Scuccessor node
static Node recursiveSuccessor(Node root, Node x )
{
if (root==null)
return null;
  
if (root==x || (temp = recursiveSuccessor(root.left,x))!=null ||
(temp = recursiveSuccessor(root.right,x))!=null)
{
if (temp!=null)
{
if (root.left == temp)
{
System.out.print( "Inorder Successor of "+x.data);
System.out.print( " is "+ root.data + "\n");
return null;
}
}
  
return root;
}
  
return null;
}

static void findSuccessor(Node root, Node x)
{
// Case1: If right child is not null
if (x.right != null)
{ //finding left-most node
Node node = x.right;
while (node != null && node.left != null)
node = node.left;
Node inorderSucc = node;
System.out.print("Inorder Successor of "+x.data+" is ");
System.out.print(inorderSucc.data+"\n");
}
  
// Case2: If right child is null
if (x.right == null)
{
int f = 0;
Node node = root;
//finding right-most node
while (node != null && node.right != null)
node = node.right;
  
Node rightMost = node;
  
// case3: If x is the right most node
if (rightMost == x)
System.out.print("Right most node reached. No successor!\n");
else
recursiveSuccessor(root, x); // when the right child of node x is null
}
}
  
// Driver program to test above functions
public static void main(String args[])
{

Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.right = newNode(6);
  
// Case 1
findSuccessor(root, root.right);
  
// case 2
findSuccessor(root, root.left.left);
  
// case 3
findSuccessor(root, root.right.right);
  
}
}

OUTPUT:


Related Solutions

Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
Question 3: Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node...
PYTHON CODING Create a method for the Binary Search Tree (deleteNode) that deletes a specified node identified by its value, and rearranges the descendants of the deleted node to ensure the resulting Tree meets the requirements of a Binary Search Tree. a) Discuss and justify your approach to address each possible case. b) Is the new tree (with the deleted node removed) unique? Discuss your answer. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
In java, create a binary search tree (without using additional libraries) of stacks where each node...
In java, create a binary search tree (without using additional libraries) of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key,...
Write a function that takes a binary search tree as input and produces a linked list...
Write a function that takes a binary search tree as input and produces a linked list of the entries, with the entries sorted (smallest entries at the front of the list and largest entries at the back). *Hint: use in-order traversal.* C++ there is no any structure
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT