Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete the following problem using java language. Consider the following declaration of Class BinarySearchTree and TreeNode (an inner class): public class BinarySearchTree { private

Please complete the following problem using java language.

image text in transcribed

Consider the following declaration of Class BinarySearchTree and TreeNode (an inner class):

public class BinarySearchTree { private static class TreeNode { // 3 private instance variables private int data; private TreeNode leftLink; private TreeNode rightLink; // One no-argument constructor for an empty node. Public TreeNode() { data = 0; leftLink = rightLink = null; } // One one-argument constructor public TreeNode (int newData, TreeNode newLeftLink, TreeNode newRightLink) { data = newData; leftLink = newLeftLink; rightLink = newRightLink; } } //End of TreeNode inner class // Declaration of class BinarySearchTree begins here. // Three instance variables. private TreeNode root; private TreeNode parent; private int parentLink; // One no-argument constructor for creating an empty binary tree. public BinarySearchTree ( ) { root = parent = null; parentLink = 0; } // One copy constructor to create one by making a deep copy of an existing BST given // by the root of the tree which is the lone parameter. public BinarySearchTree (TreeNode rootNode) { parent = null; parentLink = 0; root = copyBST (rootNode); } public TreeNode copyBST (TreeNode rootNode) // Recursively copies a BST rooted at the given rootNode and returns // the root node of the copied BST. { if (rootNode == null) return rootNode; // else the current tree or subtree not empty yet. TreeNode leftChild = copyBST (rootNode.leftLink); TreeNode rightChild = copyBST (rootNode.rightLink); return new TreeNode (rootNode.data, leftChild, rightChild); }

public TreeNode predecessor TreeNode theParent, int linknumber) This method will find and return the immediately preceding node of a given node in terms integer values ofthe tree nodes. Note that the node is given by the two parameters of this method such that the first one is its parent node and the second indicates whether the node is the left child (when linknumber 1) or the right child (when it linknumber 2) of its parent node. And, when the node does not have a parent (as is the case when it is the tree root itself its parent is given as null. of course, when there is no such predecessor node (which is the case when the given node is the smallest node of the entire current tree). then null will be returned

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago