Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help writing a toString() method to a Binary Node class. The method will create a String description of the tree, analogous to the toString()

Need help writing a toString() method to a Binary Node class. The method will create a String description of the tree, analogous to the toString() method used in a previous homework...the BinaryNode class and the method I wrote for the previous homework are below...

ToString() Method:

public String toString( int count){

String field1 = "";

String field2 = "";

if(data == null)

field1 = "dummy";

else

field1 = data.toString();

if(link == null)

field2 = "null in tail";

else

field2 = link.toString();

return field1+", "+field2;

}

--------------------------------------------------------------------------------------------------

BinaryNode Class:

public class BinaryNode { // Invariant of the IntBTNode class: // 1. Each node has one integer, stored in the instance // variable data. // 2. The instance variables left and right are references to the node's // left and right children. private int data; private BinaryNode left, right;

/** * Initialize a IntBTNode with a specified initial data and links * children. Note that a child link may be the null reference, * which indicates that the new node does not have that child. * @param initialData * the initial data of this new node * @param initialLeft * a reference to the left child of this new node--this reference may be null * to indicate that there is no node after this new node. * @param initialRight * a reference to the right child of this new node--this reference may be null * to indicate that there is no node after this new node. * Postcondition: * This node contains the specified data and links to its children. **/ public BinaryNode(int initialData, BinaryNode initialLeft, BinaryNode initialRight) { data = initialData; left = initialLeft; right = initialRight; }

}

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

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions