SlideShare a Scribd company logo
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
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
Anaya Zafar
 
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
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Linked lists
Linked listsLinked lists
Linked lists
George Scott IV
 
#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
ajoy21
 
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
 

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
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
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
 

More from info335653

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
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
 
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
info335653
 
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
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
 
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
info335653
 
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
info335653
 
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
info335653
 
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
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

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 

Recently uploaded (20)

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 

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.