SlideShare a Scribd company logo
Given BinaryNode.java
package util;
import java.util.*;
/*
This class implements Binary Node for Binary Trees
Methods:
I) Two constructors
II) Two toString methods
III) height and size methods
IV) preorder, postorder, and inorder traversals
V) BFS and DFS traversal
*/
public class BinaryNode {
public E element;//data
public BinaryNode left;//left child
public BinaryNode right;//right child
//constructor for leaves
public BinaryNode(E element) {
this(element, null, null);
}
//constructor for internal nodes
public BinaryNode(E element, BinaryNode left, BinaryNode right) {
this.left = left;
this.right = right;
this.element = element;
}
public String toString2() {
if (left == null && right == null)//base case
return element + "";
return element + "(" + left + ", " + right + ")";
}
public int height() {
if (left == null && right == null)
return 0;
if (left == null)
return 1 + right.height();
if (right == null)
return 1 + left.height();
return 1 + Math.max(left.height(), right.height());
}
public int size() {
int size = 1;//counting root
if (left != null)//counting left subtree nodes
size += left.size();
if (right != null)//counting right subtree nodes
size += right.size();
return size;
}
public void printPreOrder() {
System.out.print(element + " ");
if (left != null)
left.printPreOrder();
if (right != null)
right.printPreOrder();
}
public void printPostOrder() {
if (left != null)
left.printPostOrder();
if (right != null)
right.printPostOrder();
System.out.print(element + " ");
}
public void printInOrder() {
if (left != null)
left.printInOrder();
System.out.print(element + " ");
if (right != null)
right.printInOrder();
}
public void printBFS() {//breadth first search traversal - non-recursive method
Queue q = new LinkedList<>();
q.add(this);
while (!q.isEmpty()) {
BinaryNode cur = q.remove();
System.out.print(cur.element + " ");
if (cur.left != null)
q.add(cur.left);
if (cur.right != null)
q.add(cur.right);
}
}
public void printDFS() {//depth first search traversal - equivalent to pre-order traversal
Stack stack = new Stack<>();
stack.add(this);
while (!stack.empty()) {
BinaryNode cur = stack.pop();
System.out.print(cur.element + " ");
if (cur.right != null)
stack.push(cur.right);
if (cur.left != null)
stack.push(cur.left);
}
}
public String toString() {
if (left == null && right == null && element == null)
return "";
Queue list = new LinkedList<>();
String result = "";
list.add(this);
list.add(null);
int level = (int) Math.pow(2, height());
BinaryNode dummy = new BinaryNode(null);
while (!list.isEmpty()) {
boolean allDummies = true;
for (BinaryNode b : list)
if (b != dummy && b != null) {
allDummies = false;
break;
}
BinaryNode cur = list.remove();
if (cur == null || allDummies)
break;
for (int i = 0; i < level - 1; i++)
result += 't';
if (cur != dummy)
result += cur.element;
for (int i = 0; i < level + 1; i++)
result += 't';
if (cur.left != null)
list.add(cur.left);
else
list.add(dummy);
if (cur.right != null)
list.add(cur.right);
else
list.add(dummy);
if (list.peek() == null) {
for (int i = 0; i < height(); i++)
result += 'n';
list.remove();
list.add(null);
level /= 2;
}
}
return result + "n";
}
}
i need the main methods to test the following classes, countGreaterThanValue, isSymmetric, and
removeLeaves. i keep getting errors and cannot successfully run the program to test individually
package part;
import util.*
import java.util.*
import util.BinaryNode;
public class Question1 {
public static int countGreaterThanValue(BinaryNode root, int value) {
if (root == null) {
return 0;
}
int count = 0;
if (root.element > value) {
count = 1;
}
count += countGreaterThanValue(root.left, value);
count += countGreaterThanValue(root.right, value);
return count;
}
}
package part;
import util.BinaryNode;
public class Question2 {
public static boolean isSymmetric(BinaryNode root) {
if (root == null) {
return true;
}
return isMirror(root.left, root.right);
}
private static boolean isMirror(BinaryNode left, BinaryNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
return (left.element.equals(right.element)) &&
isMirror(left.left, right.right) &&
isMirror(left.right, right.left);
}
}
package part;
import util.BinaryNode;
public class Question3 {
public static BinaryNode removeLeaves(BinaryNode root) {
if (root == null) {
return null;
}
if (root.left == null && root.right == null) {
return null;
}
root.left = removeLeaves(root.left);
root.right = removeLeaves(root.right);
return root;
}
}

More Related Content

Similar to Given BinaryNode.javapackage util;import java.util.;T.pdf (20)

PDF
Binary tree in java
Programming Homework Help
 
PDF
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
info750646
 
PDF
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Footageetoffe16
 
DOCX
Required to augment the authors Binary Search Tree (BST) code to .docx
debishakespeare
 
DOCX
Please finish everything marked TODO BinaryNode-java public class Bi.docx
JakeT2gGrayp
 
PDF
In the class LinkedBinarySearchTree implement only the following metho.pdf
sidkucheria
 
PDF
I have a .java program that I need to modify so that it1) reads i.pdf
allystraders
 
PDF
package DataStructures; public class HelloWorld AnyType extends.pdf
apleathers
 
PDF
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 
PDF
For the code below complete the preOrder() method so that it perform.pdf
xlynettalampleyxc
 
DOCX
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
PDF
import javautilQueue import javautilLinkedList import .pdf
ADITIEYEWEAR
 
DOCX
hw4.DS_Store__MACOSXhw4._.DS_Storehw4HW4Test.javahw4.docx
wilcockiris
 
DOCX
Mcq 15-20Q15Which of the following trees are binary search tr
AbramMartino96
 
PDF
CS-102 Course_ Binary Tree Lectures .pdf
ssuser034ce1
 
PDF
To create a node for a binary search tree (BTNode, ) and to create a .pdf
bhim1213
 
PDF
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
shyamsunder1211
 
PDF
Works Applications Test - Chinmay Chauhan
Chinmay Chauhan
 
PDF
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
shaktisinhgandhinaga
 
PDF
Need help with this paperThis assignment consists of writing resea.pdf
sktambifortune
 
Binary tree in java
Programming Homework Help
 
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
info750646
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Footageetoffe16
 
Required to augment the authors Binary Search Tree (BST) code to .docx
debishakespeare
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
JakeT2gGrayp
 
In the class LinkedBinarySearchTree implement only the following metho.pdf
sidkucheria
 
I have a .java program that I need to modify so that it1) reads i.pdf
allystraders
 
package DataStructures; public class HelloWorld AnyType extends.pdf
apleathers
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 
For the code below complete the preOrder() method so that it perform.pdf
xlynettalampleyxc
 
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
import javautilQueue import javautilLinkedList import .pdf
ADITIEYEWEAR
 
hw4.DS_Store__MACOSXhw4._.DS_Storehw4HW4Test.javahw4.docx
wilcockiris
 
Mcq 15-20Q15Which of the following trees are binary search tr
AbramMartino96
 
CS-102 Course_ Binary Tree Lectures .pdf
ssuser034ce1
 
To create a node for a binary search tree (BTNode, ) and to create a .pdf
bhim1213
 
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
shyamsunder1211
 
Works Applications Test - Chinmay Chauhan
Chinmay Chauhan
 
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
shaktisinhgandhinaga
 
Need help with this paperThis assignment consists of writing resea.pdf
sktambifortune
 

More from Conint29 (13)

PDF
Fix my codeCode.pdf
Conint29
 
PDF
Domain Description for the Lunar Rover Back in July 30th 1971, the c.pdf
Conint29
 
PDF
Design a PIC based wearable device that keeps a tract of the activit.pdf
Conint29
 
PDF
Describe any five (5) advantages of computer networkingList any (5) ex.pdf
Conint29
 
PDF
I need help with this assignment Ive gotten abit stuck with the cod.pdf
Conint29
 
PDF
I am looking for some assistance with SQLite database. I have tried se.pdf
Conint29
 
PDF
how to Create a PDF holding all of your database data from Module 8..pdf
Conint29
 
PDF
How do I declare the following constructors in my .h file Below.pdf
Conint29
 
PDF
Here I have a function in my code that checks the separation of airc.pdf
Conint29
 
PDF
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
PDF
Exchange Rate Determination Theory OvershootingExchange rate over.pdf
Conint29
 
PDF
Encapsulating method details in a class [ Choose ] instance vari.pdf
Conint29
 
PDF
Hello. I need help with my assignment. Translate the ER Diagram for .pdf
Conint29
 
Fix my codeCode.pdf
Conint29
 
Domain Description for the Lunar Rover Back in July 30th 1971, the c.pdf
Conint29
 
Design a PIC based wearable device that keeps a tract of the activit.pdf
Conint29
 
Describe any five (5) advantages of computer networkingList any (5) ex.pdf
Conint29
 
I need help with this assignment Ive gotten abit stuck with the cod.pdf
Conint29
 
I am looking for some assistance with SQLite database. I have tried se.pdf
Conint29
 
how to Create a PDF holding all of your database data from Module 8..pdf
Conint29
 
How do I declare the following constructors in my .h file Below.pdf
Conint29
 
Here I have a function in my code that checks the separation of airc.pdf
Conint29
 
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
Exchange Rate Determination Theory OvershootingExchange rate over.pdf
Conint29
 
Encapsulating method details in a class [ Choose ] instance vari.pdf
Conint29
 
Hello. I need help with my assignment. Translate the ER Diagram for .pdf
Conint29
 
Ad

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Ad

Given BinaryNode.javapackage util;import java.util.;T.pdf

  • 1. Given BinaryNode.java package util; import java.util.*; /* This class implements Binary Node for Binary Trees Methods: I) Two constructors II) Two toString methods III) height and size methods IV) preorder, postorder, and inorder traversals V) BFS and DFS traversal */ public class BinaryNode { public E element;//data public BinaryNode left;//left child public BinaryNode right;//right child //constructor for leaves public BinaryNode(E element) { this(element, null, null); } //constructor for internal nodes public BinaryNode(E element, BinaryNode left, BinaryNode right) { this.left = left; this.right = right; this.element = element; } public String toString2() {
  • 2. if (left == null && right == null)//base case return element + ""; return element + "(" + left + ", " + right + ")"; } public int height() { if (left == null && right == null) return 0; if (left == null) return 1 + right.height(); if (right == null) return 1 + left.height(); return 1 + Math.max(left.height(), right.height()); } public int size() { int size = 1;//counting root if (left != null)//counting left subtree nodes size += left.size(); if (right != null)//counting right subtree nodes size += right.size(); return size; } public void printPreOrder() { System.out.print(element + " "); if (left != null) left.printPreOrder(); if (right != null) right.printPreOrder(); } public void printPostOrder() { if (left != null) left.printPostOrder(); if (right != null)
  • 3. right.printPostOrder(); System.out.print(element + " "); } public void printInOrder() { if (left != null) left.printInOrder(); System.out.print(element + " "); if (right != null) right.printInOrder(); } public void printBFS() {//breadth first search traversal - non-recursive method Queue q = new LinkedList<>(); q.add(this); while (!q.isEmpty()) { BinaryNode cur = q.remove(); System.out.print(cur.element + " "); if (cur.left != null) q.add(cur.left); if (cur.right != null) q.add(cur.right); } } public void printDFS() {//depth first search traversal - equivalent to pre-order traversal Stack stack = new Stack<>(); stack.add(this); while (!stack.empty()) { BinaryNode cur = stack.pop(); System.out.print(cur.element + " "); if (cur.right != null) stack.push(cur.right); if (cur.left != null) stack.push(cur.left); }
  • 4. } public String toString() { if (left == null && right == null && element == null) return ""; Queue list = new LinkedList<>(); String result = ""; list.add(this); list.add(null); int level = (int) Math.pow(2, height()); BinaryNode dummy = new BinaryNode(null); while (!list.isEmpty()) { boolean allDummies = true; for (BinaryNode b : list) if (b != dummy && b != null) { allDummies = false; break; } BinaryNode cur = list.remove(); if (cur == null || allDummies) break; for (int i = 0; i < level - 1; i++) result += 't'; if (cur != dummy) result += cur.element; for (int i = 0; i < level + 1; i++) result += 't'; if (cur.left != null) list.add(cur.left); else list.add(dummy); if (cur.right != null) list.add(cur.right); else list.add(dummy); if (list.peek() == null) {
  • 5. for (int i = 0; i < height(); i++) result += 'n'; list.remove(); list.add(null); level /= 2; } } return result + "n"; } } i need the main methods to test the following classes, countGreaterThanValue, isSymmetric, and removeLeaves. i keep getting errors and cannot successfully run the program to test individually package part; import util.* import java.util.* import util.BinaryNode; public class Question1 { public static int countGreaterThanValue(BinaryNode root, int value) { if (root == null) { return 0; } int count = 0; if (root.element > value) { count = 1; } count += countGreaterThanValue(root.left, value);
  • 6. count += countGreaterThanValue(root.right, value); return count; } } package part; import util.BinaryNode; public class Question2 { public static boolean isSymmetric(BinaryNode root) { if (root == null) { return true; } return isMirror(root.left, root.right); } private static boolean isMirror(BinaryNode left, BinaryNode right) { if (left == null && right == null) { return true; } if (left == null || right == null) { return false; } return (left.element.equals(right.element)) && isMirror(left.left, right.right) && isMirror(left.right, right.left); } } package part;
  • 7. import util.BinaryNode; public class Question3 { public static BinaryNode removeLeaves(BinaryNode root) { if (root == null) { return null; } if (root.left == null && root.right == null) { return null; } root.left = removeLeaves(root.left); root.right = removeLeaves(root.right); return root; } }