Question: using java Code skeleton: // node class for a ternary tree class Node { int key; Node left, middle, right; // constructor public Node(int key)

using java Code skeleton: // node class for a ternary tree classNode { int key; Node left, middle, right; // constructor public Node(intusing java

Code skeleton:

// node class for a ternary tree class Node { int key; Node left, middle, right;

// constructor public Node(int key) { this.key = key; left = middle = right = null; } }

// ternary tree class public class TernaryTree { Node root;

// constructor public TernaryTree() { root = null; }

// returns the number of nodes in the ternary tree. public int size() { //write here }

//returns true if a target key exists in the tree public boolean contains(int targetKey) { // write here }

//prints the tree using pre-order traversal. public void preOrder(){ // write here }

//returns true if the tree is a binary tree. Note that, an empty tree is a binary tree. public boolean isBinaryTree() { // write here }

public static void main(String args[]) { TernaryTree myTree = new TernaryTree(); myTree.root = new Node(3); myTree.root.left = new Node(1); myTree.root.middle = new Node(4); myTree.root.middle.left = new Node(7); // myTree.root.middle.middle = new Node(6); myTree.root.middle.right = new Node(2);

myTree.preOrder(); System.out.println();

int searchKey = 5; System.out.println("There are " + myTree.size() + " nodes in the tree."); System.out.println("Tree contains " + searchKey + "? " + myTree.contains(searchKey)); System.out.println("Is it a binary tree? " + myTree.isBinaryTree()); } }

16 10 2 12 3 6 8 Ternary Tree For this homework, we consider a ternary tree class, the code skeleton is provided for you in TernaryTree.java file. A ternary tree is a tree in which every node can have at most 3 children. So, for each node, we have 3 references: left child, right child and the middle child. An example ternary tree is depicted above. There are four methods to be implemented, described in the following

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!