Question: Write a class to implement a ternary tree node and another to implement a ternary search tree. Each node has up to three children and

Write a class to implement a ternary tree node and another to implement a ternary search tree.

Each node has up to three children and two Comparable generic type data fields

Each left data field is either the only data field or less than the second data field.

Each member of the left subtree is less than the first data field value

Each member of the middle subtree is between the two data field values (greater than the first and less than the second)

Each member of the right subtree is greater than the second data field value

Just write methods to insert elements with no duplicates allowed, find a value in, and print out the tree using inorder (you do not need to use an Iterator).

Some cases for the insert method

If the root is null, add the new element as the first data in a new node and set the root equal to that.

If the new element is less than the first data, insert it in the left subtree

If the new element is greater than the first data,

If there is no second data, add the new element as the second data.

If there is second data,

If the new element is less than the second data, add it to the middle subtree

If the new element is greater than the second data, add it to the right subtree

Write a driver program to insert random Double values less than 100 and print them out, then test several calls to the find method.

this what i have so far

public class Nodes> {

private T data;

private Nodes kid1,kid2,kid3;

public Nodes(T d){

this.data=d;

}

public T getData(){

return this.data;

}

public Nodes getKid1(){

return this.kid1;

}

public Nodes getKid2(){

return this.kid2;

}

public Nodes getKid3(){

return this.kid3;

}

public void setData(T d){

this.data=d;

}

public void setKid1(Nodes k1){

this.kid1=k1;

}

public void setKid2(Nodes k2){

this.kid2=k2;

}

public void setKid3(Nodes k3){

this.kid3=k3;

}

public String toString(){

return this.data + " ";

}

}

public class ternarytree> {

private Nodes root;

private int count;

public ternarytree(){

}

public void insert(T newData){

if(root==null)

root=new Nodes(newData);

else{

boolean inserted=false;

Nodesptr=root;

while(!inserted){

if(ptr.getData().compareTo(newData)>0){

if(ptr.getKid1()==null){

ptr.setKid1(new Nodes(newData));

count++;

inserted=true;

}

else{

ptr=ptr.getKid1();

}

}

else if (ptr.getData().compareTo(newData)<0){

if()

}

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!