iven codes: ------------------------------------------------------------------------------------ //Node.java public class Node{ protected String data; protected Node left; protected Node right; public Node(String data){ this(data, null, null); } public Node(String data, Node left, Node right){ this.data = data; this.left = left; this.right = right; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Given codes:

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

//Node.java

public class Node{

protected String data;

protected Node left;

protected Node right;

public Node(String data){

this(data, null, null);

}

public Node(String data, Node left, Node right){

this.data = data;

this.left = left;

this.right = right;

}

public String getData(){return this.data;}

public Node getLeft(){ return this.left;}

public Node getRight(){ return this.right;}

public void setData(String s){ this.data = s;}

public void setLeft(Node left){ this.left = left;}

public void setRight(Node right){ this.right = right;}

public void setLeftRight(Node left, Node right){

this.left = left;

this.right = right;

}

public void setAll(String data, Node left, Node right){

this.data = data;

this.left = left;

this.right = right;

}

}

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

//PrintBinaryTree.java

public class PrintBinaryTree{

public static boolean alt_display = !false;

public static String toString(BinaryTree bt){

return toString(bt.getRoot(), new StringBuilder(), true, new StringBuilder()).toString();

}

public static void print(BinaryTree bt){

System.out.println( toString(bt) );

}

public static StringBuilder toString(Node root, StringBuilder prefix, boolean isTail, StringBuilder sb) {

if(root == null){return sb;}

if(root.getRight() != null) {

toString(root.getRight(), new StringBuilder().append(prefix).append(isTail ? "| " : " "), false, sb);

}

if(alt_display){

sb.append(prefix).append(isTail ? "\\-- " : "/-- ").append(root.getData().toString()).append("\n");

}else{

sb.append(prefix).append(isTail ? "└── " : "┌── ").append(root.getData().toString()).append("\n");

}

if(root.getLeft() != null) {

toString(root.getLeft(), new StringBuilder().append(prefix).append(isTail ? " " : "| "), true, sb);

}

return sb;

}

}

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

looking to make changes to these codes:

//BinaryTree.java

import java.io.File;

import java.util.Scanner;

public class BinaryTree {

protected Node root = null;

protected int size = 0;

public BinaryTree(){

size = 0;

}

public BinaryTree(String s){

root = new Node(s);

size = 1;

}

public int getSize(){ return this.size; }

public Node getRoot(){ return this.root; }

public boolean contains(String target){

if( root == null ){ return false; }

if( root.getData().equals(target) ){

return true;

}

return false;

}

public void loadFromFile(String fname){

BinaryTree bt = new BinaryTree();

try{

Scanner file = new Scanner( new File(fname) );

while( file.hasNextLine()){

bt.add(file.nextLine().strip());

}

}catch(Exception e){

System.out.println("Something went wrong!!");

}

this.root = bt.root;

this.size = bt.size;

}

public void add(String s){

addRandom(s);

}

/* add a node in a random place in the tree. */

private void addRandom(String s){

if(root == null && size == 0){

root = new Node(s);

}else{

Node tmp = root;

boolean left = Math.random() < 0.5;

Node child = left ? tmp.getLeft() : tmp.getRight();

while(child != null){

tmp = child;

left = Math.random() < 0.5;

child = left ? tmp.getLeft() : tmp.getRight();

}

// assert: child == null

// yea! we have a place to add s

if(left){

tmp.setLeft(new Node(s));

}else{

tmp.setRight(new Node(s));

}

}

size += 1;

}



/** Computes the height of the binary tree

*

* The height is the length of the longest path from

* the root of the tree to any other node.

*

* @return the height of the tree

*/

public final int height(){

if( root == null ){ return -1; }

if( size == 1){ return 0; }

return heightRecursive(root);

}

protected final static int heightRecursive(Node root){

if( root == null ){

return -1;

}

int leftHeight = heightRecursive(root.getLeft());

int rightHeight = heightRecursive(root.getRight());

if( leftHeight < rightHeight){

return 1 + rightHeight;

}else{

return 1 + leftHeight;

}

}

public static void main(String[] args){

BinaryTree t = new BinaryTree("cat");

System.out.println("height = " + t.height() + ", size = " + t.getSize());

t.add("dog");

t.add("eel");

t.add("cow");

t.add("rat");

System.out.println("height = " + t.height() + ", size = " + t.getSize());

System.out.println(t);

System.out.println("height = " + t.height() + ", size = " + t.getSize());

t.loadFromFile("sample.txt");

System.out.println(t);

}

@Override

public String toString() {

return PrintBinaryTree.toString(this);

}

}

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

//BST

public class BST extends BinaryTree{

// You MUST have a zero argument constructor that

// creates an empty binary search tree

// You can can add code to this if you want (or leave it alone).

// We will create all BSTs for testing using this constructor

public BST(){}

 

@Override

public boolean contains(String s){

return false;

}

@Override

public void add(String s){

}

 

public BST makeBalanced(){

return null;

}

 

public boolean saveToFile(String fname){

return false;

}

}

 
 
In the BinaryTree class
public boolean contains (String s)
// precondition: s is string
// postcondition: returns true if s in the this tree, false otherwise
// constraint:
this method MUST use recursion
public boolean isBST()
// note: an empty tree is
// postcondition: returns true if this tree is a valid binary search tree, false otherwise
// notes: A BST object might NOT satisfy the binary search tree property.
valid BST
This method will not use instanceof. It should determine if the
values and structure of this tree satisfy the binary search tree property.
In the BST class
e0verride
public boolean contains (String s)
// precondition: s is string
// postcondition: returns true if s in the this tree, false otherwise
// efficiency: (i) this method must be efficient
//
(ii) do NOT use recursion for this method
Q0verride
public boolean add (String s)
// precondition: (i) s is a string
// postcondition: (i) s is added to this tree, if it is not already
in the tree, and tree remains a valid BST
(ii) s is added as a leaf in the tree (if added)
(iii) returns true if s is added to the tree, false otherwise
public BST makeBalanced ()
// postcondition: returns a new binary search tree that
(i) has the same data (strings) as this tree
(ii) has minimal height
(iii) is a valid binary search tree
public boolean saveToFile (String fname)
// purpose: saves the current tree in text format in the current directory in a file fname
// precondition: fname is a string (name of file to save tree)
// postcondition: (i) returns true if successful (saved to file), false otherwise
//
(ii) loading the saved file with loadFromFile will exactly reconstruct this tree
The makeBalanced () method must create a new balanced binary search tree with minimal
height. For a given tree there may be many different valid binary search trees with the same
minimal height. It does not matter which optimal tree you return.
Note: The height of tree is the longest distance from the root of the tree to any of its leaf nodes.
A binary tree with a single node has height 0. A binary tree with two nodes has height 1. The
BinaryTree class has a height method that computes the height of a binary tree.
There is a PrintBinaryTree class that is also provided. This will allow you to visualize your
trees when developing, testing and debugging your code.
Transcribed Image Text:In the BinaryTree class public boolean contains (String s) // precondition: s is string // postcondition: returns true if s in the this tree, false otherwise // constraint: this method MUST use recursion public boolean isBST() // note: an empty tree is // postcondition: returns true if this tree is a valid binary search tree, false otherwise // notes: A BST object might NOT satisfy the binary search tree property. valid BST This method will not use instanceof. It should determine if the values and structure of this tree satisfy the binary search tree property. In the BST class e0verride public boolean contains (String s) // precondition: s is string // postcondition: returns true if s in the this tree, false otherwise // efficiency: (i) this method must be efficient // (ii) do NOT use recursion for this method Q0verride public boolean add (String s) // precondition: (i) s is a string // postcondition: (i) s is added to this tree, if it is not already in the tree, and tree remains a valid BST (ii) s is added as a leaf in the tree (if added) (iii) returns true if s is added to the tree, false otherwise public BST makeBalanced () // postcondition: returns a new binary search tree that (i) has the same data (strings) as this tree (ii) has minimal height (iii) is a valid binary search tree public boolean saveToFile (String fname) // purpose: saves the current tree in text format in the current directory in a file fname // precondition: fname is a string (name of file to save tree) // postcondition: (i) returns true if successful (saved to file), false otherwise // (ii) loading the saved file with loadFromFile will exactly reconstruct this tree The makeBalanced () method must create a new balanced binary search tree with minimal height. For a given tree there may be many different valid binary search trees with the same minimal height. It does not matter which optimal tree you return. Note: The height of tree is the longest distance from the root of the tree to any of its leaf nodes. A binary tree with a single node has height 0. A binary tree with two nodes has height 1. The BinaryTree class has a height method that computes the height of a binary tree. There is a PrintBinaryTree class that is also provided. This will allow you to visualize your trees when developing, testing and debugging your code.
A Binary Search Tree (BST) can be used to efficiently implement a sorted set. It stores unique
values and offers an efficient membership method (contains).
A binary search tree (BST) is a binary tree with the additional binary search tree property.
The binary search tree property states that for every node n in the tree
1. n.data is greater than the data value of all nodes in the sub-tree rooted with n.left.
2. n.data is less than the data value of all nodes in the sub-tree rooted with n.right.
As we are implementing a set, all data (strings) in the tree will be unique. Every node in the tree
will have its own distinct string. Strings will be compared using the canonical way (as defined by
the compareTo () method in the String class).
For example, the binary tree on the left (below) IS a binary search tree and the one on the right
is NOT.
B
Ex
Ex
Bat) (Et)(G)
A
Bat) (Ox
You will implement several methods in the BinaryTree and BST classes. The BST class must
extend the BinaryTree class. Methods that you must complete (implement or override) are as
follows:
BinaryTree: contains (String), isBST()
BST: contains (String), add(String), makeBalanced (), and saveToFile(String)
For this assignment, you will need to save a binary search tree in a text file. For a tree
with n values (nodes) the corresponding file will have n lines. Each line contains a single value.
The ordering of the values (lines in the file) must ensure that the loadFromFile method in the
BinaryTree class will recreate your binary search tree.
Transcribed Image Text:A Binary Search Tree (BST) can be used to efficiently implement a sorted set. It stores unique values and offers an efficient membership method (contains). A binary search tree (BST) is a binary tree with the additional binary search tree property. The binary search tree property states that for every node n in the tree 1. n.data is greater than the data value of all nodes in the sub-tree rooted with n.left. 2. n.data is less than the data value of all nodes in the sub-tree rooted with n.right. As we are implementing a set, all data (strings) in the tree will be unique. Every node in the tree will have its own distinct string. Strings will be compared using the canonical way (as defined by the compareTo () method in the String class). For example, the binary tree on the left (below) IS a binary search tree and the one on the right is NOT. B Ex Ex Bat) (Et)(G) A Bat) (Ox You will implement several methods in the BinaryTree and BST classes. The BST class must extend the BinaryTree class. Methods that you must complete (implement or override) are as follows: BinaryTree: contains (String), isBST() BST: contains (String), add(String), makeBalanced (), and saveToFile(String) For this assignment, you will need to save a binary search tree in a text file. For a tree with n values (nodes) the corresponding file will have n lines. Each line contains a single value. The ordering of the values (lines in the file) must ensure that the loadFromFile method in the BinaryTree class will recreate your binary search tree.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY