SlideShare a Scribd company logo
1 of 5
Download to read offline
using
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
*/
publicclass 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;
}
@Override
public String toString(){
if(left == null && right == null)//base case
return element + "";
return element + "(" + left + ", " + right + ")";
}
publicint 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());
}
publicint 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;
}
publicvoid printPreOrder(){
System.out.print(element + " ");
if(left != null)
left.printPreOrder();
if(right != null)
right.printPreOrder();
}
publicvoid printPostOrder(){
if(left != null)
left.printPostOrder();
if(right != null)
right.printPostOrder();
System.out.print(element + " ");
}
publicvoid printInOrder(){
if(left != null)
left.printInOrder();
System.out.print(element + " ");
if(right != null)
right.printInOrder();
}
publicvoid 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);
}
}
publicvoid 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 toString2() {
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";
}
}
2. Write a method with signature public staticboolean isSymmetric(BinaryNode root) which gets
a Binary Tree (not BST necessarily!) and returns a boolean specifying whether the tree is a
mirror of itself (Symmetric around the center).
3. Write a method with signature public staticBinaryNode removeLeaves(BinaryNode root)
which gets a Binary Tree and removes all its leaves and returns the root.

More Related Content

Similar to usingpackage util;import java.util.;This class implements.pdf

PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
xlynettalampleyxc
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
shanki7
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
archgeetsenterprises
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdfimport java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
shaktisinhgandhinaga
 
tested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdftested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdf
shanki7
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
michardsonkhaicarr37
 
Please refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdfPlease refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdf
sooryasalini
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
fathimalinks
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
amazing2001
 

Similar to usingpackage util;import java.util.;This class implements.pdf (20)

PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdfimport java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
 
tested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdftested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
 
Linked lists
Linked listsLinked lists
Linked lists
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
Please refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdfPlease refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdf
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 

More from info335653

you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
info335653
 
You are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdfYou are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdf
info335653
 
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdfWinds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
info335653
 
With reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdfWith reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdf
info335653
 
Will upvote Please fix the following code and post your inputs and o.pdf
Will upvote Please fix the following code and post your inputs and o.pdfWill upvote Please fix the following code and post your inputs and o.pdf
Will upvote Please fix the following code and post your inputs and o.pdf
info335653
 

More from info335653 (12)

You�ve been hired by the local government to determine which employe.pdf
You�ve been hired by the local government to determine which employe.pdfYou�ve been hired by the local government to determine which employe.pdf
You�ve been hired by the local government to determine which employe.pdf
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 
You are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdfYou are the chief data scientist of the marketing company �VisualZ.�.pdf
You are the chief data scientist of the marketing company �VisualZ.�.pdf
 
Write a program that displays a table of the Celsius temperatures 0 .pdf
Write a program that displays a table of the Celsius temperatures 0 .pdfWrite a program that displays a table of the Celsius temperatures 0 .pdf
Write a program that displays a table of the Celsius temperatures 0 .pdf
 
Why is the release of GDP statistics less interesting to investors t.pdf
Why is the release of GDP statistics less interesting to investors t.pdfWhy is the release of GDP statistics less interesting to investors t.pdf
Why is the release of GDP statistics less interesting to investors t.pdf
 
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdfWinds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
Winds of Change in Klickitat CountyThe Harvest Wind ProjectM..pdf
 
With reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdfWith reference to the case study, define social media marketing and cr.pdf
With reference to the case study, define social media marketing and cr.pdf
 
Will upvote Please fix the following code and post your inputs and o.pdf
Will upvote Please fix the following code and post your inputs and o.pdfWill upvote Please fix the following code and post your inputs and o.pdf
Will upvote Please fix the following code and post your inputs and o.pdf
 
Which of the following statements is CORRECT a. The four most importa.pdf
Which of the following statements is CORRECT a. The four most importa.pdfWhich of the following statements is CORRECT a. The four most importa.pdf
Which of the following statements is CORRECT a. The four most importa.pdf
 
We will discuss 1. IT systems� complexity IT systems have become unman.pdf
We will discuss 1. IT systems� complexity IT systems have become unman.pdfWe will discuss 1. IT systems� complexity IT systems have become unman.pdf
We will discuss 1. IT systems� complexity IT systems have become unman.pdf
 
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdf
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdfUsing MISP VM ova file, import it on VirtualBox and respond to the q.pdf
Using MISP VM ova file, import it on VirtualBox and respond to the q.pdf
 
True or false Public policy is not an important component of organi.pdf
True or false Public policy is not an important component of organi.pdfTrue or false Public policy is not an important component of organi.pdf
True or false Public policy is not an important component of organi.pdf
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 

usingpackage util;import java.util.;This class implements.pdf

  • 1. using 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 */ publicclass 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; } @Override public String toString(){ if(left == null && right == null)//base case return element + ""; return element + "(" + left + ", " + right + ")"; } publicint height() { if(left == null && right == null)
  • 2. return 0; if(left == null) return 1 + right.height(); if(right == null) return 1 + left.height(); return 1 + Math.max(left.height(), right.height()); } publicint 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; } publicvoid printPreOrder(){ System.out.print(element + " "); if(left != null) left.printPreOrder(); if(right != null) right.printPreOrder(); } publicvoid printPostOrder(){ if(left != null) left.printPostOrder(); if(right != null) right.printPostOrder(); System.out.print(element + " "); } publicvoid printInOrder(){ if(left != null) left.printInOrder(); System.out.print(element + " "); if(right != null) right.printInOrder(); }
  • 3. publicvoid 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); } } publicvoid 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 toString2() { 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)
  • 4. 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"; } } 2. Write a method with signature public staticboolean isSymmetric(BinaryNode root) which gets a Binary Tree (not BST necessarily!) and returns a boolean specifying whether the tree is a mirror of itself (Symmetric around the center). 3. Write a method with signature public staticBinaryNode removeLeaves(BinaryNode root)
  • 5. which gets a Binary Tree and removes all its leaves and returns the root.