public class BinarySearchTree { protected int data; protected BinarySearchTree n
ID: 3859583 • Letter: P
Question
public class BinarySearchTree {
protected int data;
protected BinarySearchTree node;
protected BinarySearchTree left;
protected BinarySearchTree right;
protected BinarySearchTree parent;
protected int value;
public BinarySearchTree(int data) {
this(data, null, null, null);
}
public BinarySearchTree(int data, BinarySearchTree left,
BinarySearchTree right, BinarySearchTree parent) {
this.data = data;
this.left = left;
this.right = right;
this.parent = parent;
}
public BinarySearchTree add(int element) {
return add(node, element);
}
private BinarySearchTree add(BinarySearchTree node, int element) {
if (node.data < element) {
if (node.right == null) {
node.right = new BinarySearchTree(element);
} else {
add(node.right, element);
}
} else if (node.data > element) {
if (node.left == null) {
node.left = new BinarySearchTree(element);
} else {
add(node.left, element);
}
}
return node;
}
public int leaves() {
return leaves(node);
}
private int leaves(BinarySearchTree node) {
if (node == null) {
return 0;
} else if (node.left == null && node.right == null) {
return 1;
} else {
return leaves(node.left) + leaves(node.right);
}
}
}
Add the following methods to your BST:
-List<Integer> getPath(int value): this method returns the path from the specified value to the root. Your method should take time proportional to the height of the tree
-int rangeCount(int leftValue, int rightValue): this method returns the number of elements in the BST that are between leftValue and rightValue. You may assume that both leftValue and rightValue exist in the tree
-void toReverse(): this method modifies the BST into a reverse BST. A reverse BST has the property that for every node in the tree, the value of any node in its left sub-tree is greater than the value of the node, and the value of any node in its right sub-tree is less than the value of the node. Note, in all the other methods you can assume that the tree is a BST.
-Write a main method that tests your implementation. Note, you can add any other helper methods to simply your implementation.
Explanation / Answer
Answer:
Answer:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void insert(int,int );
void remove(int);
void print(int);
int find(int);
int find1(int,int);
int input[40],t=1,s,x,i;
main()
{
int ch,y;
for(i=1;i<40;i++)
input[i]=-1;
while(1)
{
cout <<"1.INSERT 2.DELETE 3.print 4.find 5.EXIT Enter your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"enter the element to delete";
cin >>x;
y=find(1);
if(y!=-1) remove(y);
else cout<<"no such element in input";
break;
case 3:
print(1);
cout<<" ";
for(int i=0;i<=32;i++)
cout <<i;
cout <<" ";
break;
case 4:
cout <<"enter the element to find:";
cin >> x;
y=find(1);
if(y == -1) cout <<"no such element in input";
else cout <<x << "is in" <<y <<"position";
break;
case 5:
exit(0);
}
}
}
void insert(int s,int ch )
{
int x;
if(t==1)
{
input[t++]=ch;
return;
}
x=find1(s,ch);
if(input[x]>ch)
input[2*x]=ch;
else
input[2*x+1]=ch;
t++;
}
void remove(int x)
{
if( input[2*x]==-1 && input[2*x+1]==-1)
input[x]=-1;
else if(input[2*x]==-1)
{ input[x]=input[2*x+1];
input[2*x+1]=-1;
}
else if(input[2*x+1]==-1)
{ input[x]=input[2*x];
input[2*x]=-1;
}
else
{
input[x]=input[2*x];
remove(2*x);
}
t--;
}
int find(int s)
{
if(t==1)
{
cout <<"no element in input";
return -1;
}
if(input[s]==-1)
return input[s];
if(input[s]>x)
find(2*s);
else if(input[s]<x)
find(2*s+1);
else
return s;
}
void print(int s)
{
if(t==1)
{cout <<"no element in input:";
return;}
for(int i=1;i<40;i++)
if(input[i]==-1)
cout <<" ";
else cout <<input[i];
return ;
}
int find1(int s,int ch)
{
if(t==1)
{
cout <<"no element in input";
return -1;
}
if(input[s]==-1)
return s/2;
if(input[s] > ch)
find1(2*s,ch);
else find1(2*s+1,ch);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.