// TO DO: add your implementation and JavaDocs.
public class ThreeTenKTree {
//K-ary tree with an array as internal storage.
//All nodes are stored in the array following level-order top-down
//and left-to-right within one level.
//Root at index 0.
//underlying array for k-ary tree storage
// -- you MUST use this for credit! Do NOT change the name or type
private E[] storage;
//hash table to help remember the index of each stored value
private ThreeTenHashTable indexMap;
//branching factor
private int branchK;
// ADD MORE PRIVATE MEMBERS HERE IF NEEDED!
@SuppressWarnings("unchecked")
public ThreeTenKTree(int length, int k) {
//initialize tree storage as an array of given length
// and branching factor as k.
//May assyme the given length ensures the storage for a perfect tree.
//For example, if k=2, length will be of 1, 3, 7, 15, 29, etc.
//If k=3, length will be of length 1, 4, 13, etc.
//May also assume k>=2.
//Also initialize the hash table with ThreeTenHashTable.defaultTableLength.
}
public int getBranch(){
//report branching factor
//O(1)
return -1; //default return, change or remove as needed
}
public int size() {
//report number of non-null nodes in tree
//O(1)
return -1; //default return, change or remove as needed
}
public int capacity(){
//report the length of storage
//this should be the no. of nodes of a perfect tree of the current height
//O(1)
return -1; //default return, change or remove as needed
}
public int height() {
//report the tree height
//O(1)
return -1; //default return, change or remove as needed
}
@SuppressWarnings("unchecked")
public boolean set(int index, E value) {
// Set value at index in tree storage.
// If value is null, this method attempts to remove a (leaf) node.
// - If index is not valid or the given index does not have a node,
// no change to tree and return false;
// - If node at given index has any child, do not remove and return false;
// - Otherwise, remove the node and return true.
// If value is not null, this method attempts to add/replace a node.
// - If value is already in tree (any index), no change and return false;
// - If value is new and index denotes a valid node of current tree, set value
// at this node and return true;
// - You may need to grow the tree storage (i.e. add a level) for this node
// - If adding this node would make the tree invalid, no change and return false.
// See examples in main() below for different cases.
// Remember to update the hash table if tree is updated.
return false; //default return, change or remove as needed
}
public E get(int index) {
//Return value at node index.
// - return null for invalid index or index with no node in tree
// O(1)
return null; //default return, change or remove as needed
}
public String toStringLevelOrder() {
//Return a string of all nodes (excluding null nodes) in tree storage:
// - include all levels from top down
// - all nodes are printed with a single space separated
// - return an empty string if tree is null
// Note: use Strin.
TO DO add your implementation and JavaDocs.public class ThreeT.pdf
1. // TO DO: add your implementation and JavaDocs.
public class ThreeTenKTree {
//K-ary tree with an array as internal storage.
//All nodes are stored in the array following level-order top-down
//and left-to-right within one level.
//Root at index 0.
//underlying array for k-ary tree storage
// -- you MUST use this for credit! Do NOT change the name or type
private E[] storage;
//hash table to help remember the index of each stored value
private ThreeTenHashTable indexMap;
//branching factor
private int branchK;
// ADD MORE PRIVATE MEMBERS HERE IF NEEDED!
@SuppressWarnings("unchecked")
public ThreeTenKTree(int length, int k) {
//initialize tree storage as an array of given length
// and branching factor as k.
//May assyme the given length ensures the storage for a perfect tree.
//For example, if k=2, length will be of 1, 3, 7, 15, 29, etc.
//If k=3, length will be of length 1, 4, 13, etc.
//May also assume k>=2.
//Also initialize the hash table with ThreeTenHashTable.defaultTableLength.
}
public int getBranch(){
//report branching factor
//O(1)
return -1; //default return, change or remove as needed
}
2. public int size() {
//report number of non-null nodes in tree
//O(1)
return -1; //default return, change or remove as needed
}
public int capacity(){
//report the length of storage
//this should be the no. of nodes of a perfect tree of the current height
//O(1)
return -1; //default return, change or remove as needed
}
public int height() {
//report the tree height
//O(1)
return -1; //default return, change or remove as needed
}
@SuppressWarnings("unchecked")
public boolean set(int index, E value) {
// Set value at index in tree storage.
// If value is null, this method attempts to remove a (leaf) node.
// - If index is not valid or the given index does not have a node,
// no change to tree and return false;
// - If node at given index has any child, do not remove and return false;
// - Otherwise, remove the node and return true.
// If value is not null, this method attempts to add/replace a node.
// - If value is already in tree (any index), no change and return false;
// - If value is new and index denotes a valid node of current tree, set value
// at this node and return true;
3. // - You may need to grow the tree storage (i.e. add a level) for this node
// - If adding this node would make the tree invalid, no change and return false.
// See examples in main() below for different cases.
// Remember to update the hash table if tree is updated.
return false; //default return, change or remove as needed
}
public E get(int index) {
//Return value at node index.
// - return null for invalid index or index with no node in tree
// O(1)
return null; //default return, change or remove as needed
}
public String toStringLevelOrder() {
//Return a string of all nodes (excluding null nodes) in tree storage:
// - include all levels from top down
// - all nodes are printed with a single space separated
// - return an empty string if tree is null
// Note: use StringBuilder instead of String concatenation
// Example:
// binary tree: A
// /
// B C
// / /
// D E
//
// toStringLevelOrder() should return "A B C D E"
return ""; //default return, change or remove as needed
}
4. @Override
public String toString() {
//Return a string of all nodes (including null nodes) in tree storage:
// - include all levels from top down
// - each level is printed on its own line
// - each node in the level is printed with a single space separated
// - null nodes included in string
// - return an empty string if tree is null
// Note: use StringBuilder instead of String concatenation
// Example:
// binary tree: A
// /
// B C
// / /
// D E
//
// toString() should return "AnB CnD null E null"
return ""; //default return, change or remove as needed
}
public String getAncestors(E value){
//Find the node of the given value and return the ancestors of the node in a string:
// - if value not present, return null
// - return string should include all ancestors including the node itself
// - ancestors should start from root and separated by "-->"
// O(height) assuming hash table search is O(1)
return ""; //default return, change or remove as needed
}
5. public String getChildren(E value){
//Find the node of the given value and return the children of the node in a string:
// - if value not present, return null
// - if the node is a leaf, return an empty string
// - return string should include all children, from left to right,
// and separated by a single space
// O(K) where K is the branch factor assuming hash table search is O(1)
return ""; //default return, change or remove as needed
}
public boolean has(E value){
//Determine if value is in tree or not
// - return true if a tree node has value; false otherwise
// - null is not a valid value in tree
// O(1) assuming hash table search is O(1)
return false; //default return, change or remove as needed
}
public boolean isLeaf(E value){
//Determine if value is in a leaf node of tree or not
// - return true if a leaf node has value; false otherwise
// O(K) where K is the branching factor assuming hash table search is O(1)
return false; //default return, change or remove as needed
}
public boolean remove(E value){
//Remove value from tree if value is in a leaf node
// - if value not present, return false
6. // - if value present but not in a leaf node, do not remove and return false
// - if value is in a leaf node, remove node from tree and return true
return false; //default return, change or remove as needed
}
public static FcnsTreeNode createFcnsTree(ThreeTenKTree ktree){
//construct the corresponding first-child-next-sibling tree
//for this k-ary tree and return the root node of the FCNS tree.
// Consider helper methods; consider a recursive approach.
// O(N) where N is the size of the current K-ary tree.
return null; //default return, change or remove as needed
}