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 methodIf 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
public Nodes(T d){ this.data=d; } public T getData(){ return this.data; } public Nodes return this.kid1; } public Nodes return this.kid2; } public Nodes return this.kid3; } public void setData(T d){ this.data=d; } public void setKid1(Nodes this.kid1=k1; } public void setKid2(Nodes this.kid2=k2; } public void setKid3(Nodes this.kid3=k3; } public String toString(){ return this.data + " "; } } public class ternarytree private Nodes private int count;
public ternarytree(){
} public void insert(T newData){ if(root==null) root=new Nodes else{ boolean inserted=false; Nodes while(!inserted){ if(ptr.getData().compareTo(newData)>0){ if(ptr.getKid1()==null){ ptr.setKid1(new Nodes 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
Get step-by-step solutions from verified subject matter experts
