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 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 15 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. public int size0) eze (number of nodes) of the tree. In the depicted example tree, szeO returns 10 . (20 points) public boolean contains(int targetKey) returns true if a target key exists in the tree, otherwise returns false. In the above example tree, contains(8) returns true but contains(21) returns false. (25 points) public void preOrder) prints the tree using pre-order traversal. Note that pre-order in this case prints the root, then visits left, visits middle child, and finally visits the right child. (15 points) public boolean isBinaryTree0) returns true if the tree is a binary tree. Note that, an empty tree is a binary tree. Also, a single node is considered a binary tree. In the above example, isBinaryTree() returns false as some 16 10 2 12 3 6 15 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. public int size0) eze (number of nodes) of the tree. In the depicted example tree, szeO returns 10 . (20 points) public boolean contains(int targetKey) returns true if a target key exists in the tree, otherwise returns false. In the above example tree, contains(8) returns true but contains(21) returns false. (25 points) public void preOrder) prints the tree using pre-order traversal. Note that pre-order in this case prints the root, then visits left, visits middle child, and finally visits the right child. (15 points) public boolean isBinaryTree0) returns true if the tree is a binary tree. Note that, an empty tree is a binary tree. Also, a single node is considered a binary tree. In the above example, isBinaryTree() returns false as some
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
