Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedusing 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 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. public int size) returns the size (number of nodes) of the tree. In the depicted example tree, size0 returns 10. (20 points)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

Where is the fanciest place you've ever been?

Answered: 1 week ago