SlideShare a Scribd company logo
Binary tree in java
Binary Tree in Java
Thisis a program of a binary tree which is extended from its super
class SimpleTree. It uses all the functionalities from its super
class. A binary tree consists of two leaves mainly left child nodes
and right child nodes.
There are different methods defined performing different
functionalities. Initially it is checked whether the root node is
created for a binary tree or not. Another method is counting for the
leaves of the binary tree, by counting the left and right child nodes.
Some method is checking for the nodes having both the child nodes
available or not. Also methods are modifying and removing the required
nodes.
public class BinaryTree extends SimpleTree {
// counting leaves
public int countLeaves() {
if (root == null) {
return 0;
} else {
return countLeaves(root);
}
}
/*
* determine how many leaves are in the subtree whose root is
node
* Precondition: node is not null
*/
private int countLeaves(Node n) {
BinaryNode node = (BinaryNode) n;
if ((node.getLeft()==null) && (node.getRight()==null)) {
return 1;
} else if ((node.getLeft()==null) &&
(node.getRight()!=null)) {
return countLeaves(node.getRight());
} else if ((node.getLeft()!=null) &&
(node.getRight()==null)) {
return countLeaves(node.getLeft());
} else {
// this is done when
// ((node.getLeft()!=null) && (node.getRight()!=null))
return countLeaves(node.getLeft()) +
countLeaves(node.getRight());
}
}
// count nodes
/**
* return true when every node in the tree has either two children
or none
* (but no node has only one child)
*/
public boolean twoChildrenOrNone() {
if (root == null) {
return true;
} else {
return twoChildrenOrNone(root);
}
}
/*
* determine whether all the node has two children or none
* Precondition: node is not null
*/
private boolean twoChildrenOrNone(Node n) {
BinaryNode node = (BinaryNode) n;
if ((node.getLeft()==null) && (node.getRight()==null)) {
return true;
} else if ((node.getLeft()==null) &&
(node.getRight()!=null)) {
return false;
} else if ((node.getLeft()!=null) &&
(node.getRight()==null)) {
return false;
} else {
// this is done when
// ((node.getLeft()!=null) && (node.getRight()!=null))
return twoChildrenOrNone(node.getLeft()) &&
twoChildrenOrNone(node.getRight());
}
}
// remove leaves
/**
* modify the tree by removing every leaf node
*/
public void trim() {
if (root != null) {
root = trim(root);
}
}
/*
* remove all the leaves.
* Precondition: node is not null
*/
private BinaryNode trim(Node n) {
BinaryNode node = (BinaryNode) n;
if ((node.getLeft()==null) && (node.getRight()==null)) {
// this is a leaf, remove it from the tree.
return null;
} else if ((node.getLeft()==null) &&
(node.getRight()!=null)) {
node.setRight(trim(node.getRight()));
} else if ((node.getLeft()!=null) &&
(node.getRight()==null)) {
node.setLeft(trim(node.getLeft()));
} else {
// this is done when
// ((node.getLeft()!=null) && (node.getRight()!=null))
node.setLeft(trim(node.getLeft()));
node.setRight(trim(node.getRight()));
}
return node;
}
}

More Related Content

PDF
Js objects
PDF
Lazy evaluation drupal camp moscow 2014
PPTX
Drupal 8 database api
PDF
Lodash js
PPT
Lecture6 display data by okello erick
PDF
Functional es6
PDF
JavaScript Fundamentals with Angular and Lodash
KEY
Core dev summit
Js objects
Lazy evaluation drupal camp moscow 2014
Drupal 8 database api
Lodash js
Lecture6 display data by okello erick
Functional es6
JavaScript Fundamentals with Angular and Lodash
Core dev summit

What's hot (20)

PDF
Road to react hooks
PDF
C SQLite usage
DOCX
How to implement joins in mongo db
PDF
13. dynamic allocation
PDF
_Function Builders in Swift #love_swift
PPTX
Constructors
DOCX
Connectivity coding for java and mysql
PDF
ORM is offensive
PDF
Backups with Exported Resources - Zach Leslie, Puppet Labs
PPTX
constructor and destructor
PDF
Listing for TryLambdaExpressions
TXT
Litebox
PDF
AngularJs
PDF
Redis the better NoSQL
PDF
React, Redux, ES2015 by Max Petruck
DOCX
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
PDF
How Much Immutability Is Enough?
PPTX
Constructor ppt
PPTX
MySql:Introduction
PDF
Riak Search 2: Yokozuna
Road to react hooks
C SQLite usage
How to implement joins in mongo db
13. dynamic allocation
_Function Builders in Swift #love_swift
Constructors
Connectivity coding for java and mysql
ORM is offensive
Backups with Exported Resources - Zach Leslie, Puppet Labs
constructor and destructor
Listing for TryLambdaExpressions
Litebox
AngularJs
Redis the better NoSQL
React, Redux, ES2015 by Max Petruck
supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation
How Much Immutability Is Enough?
Constructor ppt
MySql:Introduction
Riak Search 2: Yokozuna
Ad

Viewers also liked (8)

PPTX
Expression tree
PPTX
PDF
Expression trees
PPT
Computer notes - Expression Tree
PPTX
Implementation of trees
PDF
Infix to Prefix (Conversion, Evaluation, Code)
PPT
1.4 expression tree
PPT
flag of india ppt
Expression tree
Expression trees
Computer notes - Expression Tree
Implementation of trees
Infix to Prefix (Conversion, Evaluation, Code)
1.4 expression tree
flag of india ppt
Ad

Similar to Binary tree in java (20)

PDF
A perfect left-sided binary tree is a binary tree where every intern.pdf
DOCX
Once you have all the structures working as intended- it is time to co.docx
PDF
How to do the main method for this programBinaryNode.javapublic.pdf
PDF
Given BinaryNode.javapackage util;import java.util.;T.pdf
PDF
Create an implementation of a binary tree using the recursive appr.pdf
PDF
On the code which has a class in which I implementing a binary tree .pdf
DOCX
For this project, write a program that stores integers in a binary.docx
PDF
I have a .java program that I need to modify so that it1) reads i.pdf
DOCX
Decision tree handson
PDF
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
PDF
For the code below complete the preOrder() method so that it perform.pdf
DOCX
Required to augment the authors Binary Search Tree (BST) code to .docx
PDF
import javautilQueue import javautilLinkedList import .pdf
PDF
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
PDF
To create a node for a binary search tree (BTNode, ) and to create a .pdf
PDF
Given the following codepackage data1;import java.util.;p.pdf
PDF
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
PDF
package DataStructures; public class HelloWorld AnyType extends.pdf
PDF
Need Help with this Java Assignment. Program should be done in JAVA .pdf
PDF
JavaCreate a java application using the code example in the follo.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
Once you have all the structures working as intended- it is time to co.docx
How to do the main method for this programBinaryNode.javapublic.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdf
Create an implementation of a binary tree using the recursive appr.pdf
On the code which has a class in which I implementing a binary tree .pdf
For this project, write a program that stores integers in a binary.docx
I have a .java program that I need to modify so that it1) reads i.pdf
Decision tree handson
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
For the code below complete the preOrder() method so that it perform.pdf
Required to augment the authors Binary Search Tree (BST) code to .docx
import javautilQueue import javautilLinkedList import .pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdf
Given the following codepackage data1;import java.util.;p.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
package DataStructures; public class HelloWorld AnyType extends.pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
JavaCreate a java application using the code example in the follo.pdf

More from Programming Homework Help (8)

PDF
Java Assignment Help
PDF
C# Assignmet Help
PDF
C# Assignmet Help
PDF
Family tree in java
PDF
Bank account in java
PDF
Mouse and Cat Game In C++
PDF
Word games in c
PDF
Card Games in C++
Java Assignment Help
C# Assignmet Help
C# Assignmet Help
Family tree in java
Bank account in java
Mouse and Cat Game In C++
Word games in c
Card Games in C++

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Institutional Correction lecture only . . .
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Institutional Correction lecture only . . .
Final Presentation General Medicine 03-08-2024.pptx
Final Presentation General Medicine 03-08-2024.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Cell Types and Its function , kingdom of life
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O7-L3 Supply Chain Operations - ICLT Program
A systematic review of self-coping strategies used by university students to ...
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Anesthesia in Laparoscopic Surgery in India
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
2.FourierTransform-ShortQuestionswithAnswers.pdf

Binary tree in java

  • 2. Binary Tree in Java Thisis a program of a binary tree which is extended from its super class SimpleTree. It uses all the functionalities from its super class. A binary tree consists of two leaves mainly left child nodes and right child nodes. There are different methods defined performing different functionalities. Initially it is checked whether the root node is created for a binary tree or not. Another method is counting for the leaves of the binary tree, by counting the left and right child nodes. Some method is checking for the nodes having both the child nodes available or not. Also methods are modifying and removing the required nodes. public class BinaryTree extends SimpleTree { // counting leaves public int countLeaves() { if (root == null) { return 0; } else { return countLeaves(root); } } /* * determine how many leaves are in the subtree whose root is node * Precondition: node is not null */ private int countLeaves(Node n) { BinaryNode node = (BinaryNode) n; if ((node.getLeft()==null) && (node.getRight()==null)) { return 1; } else if ((node.getLeft()==null) && (node.getRight()!=null)) { return countLeaves(node.getRight()); } else if ((node.getLeft()!=null) && (node.getRight()==null)) { return countLeaves(node.getLeft()); } else { // this is done when // ((node.getLeft()!=null) && (node.getRight()!=null)) return countLeaves(node.getLeft()) + countLeaves(node.getRight()); } } // count nodes
  • 3. /** * return true when every node in the tree has either two children or none * (but no node has only one child) */ public boolean twoChildrenOrNone() { if (root == null) { return true; } else { return twoChildrenOrNone(root); } } /* * determine whether all the node has two children or none * Precondition: node is not null */ private boolean twoChildrenOrNone(Node n) { BinaryNode node = (BinaryNode) n; if ((node.getLeft()==null) && (node.getRight()==null)) { return true; } else if ((node.getLeft()==null) && (node.getRight()!=null)) { return false; } else if ((node.getLeft()!=null) && (node.getRight()==null)) { return false; } else { // this is done when // ((node.getLeft()!=null) && (node.getRight()!=null)) return twoChildrenOrNone(node.getLeft()) && twoChildrenOrNone(node.getRight()); } } // remove leaves /** * modify the tree by removing every leaf node */ public void trim() { if (root != null) { root = trim(root); } } /* * remove all the leaves. * Precondition: node is not null */ private BinaryNode trim(Node n) { BinaryNode node = (BinaryNode) n; if ((node.getLeft()==null) && (node.getRight()==null)) {
  • 4. // this is a leaf, remove it from the tree. return null; } else if ((node.getLeft()==null) && (node.getRight()!=null)) { node.setRight(trim(node.getRight())); } else if ((node.getLeft()!=null) && (node.getRight()==null)) { node.setLeft(trim(node.getLeft())); } else { // this is done when // ((node.getLeft()!=null) && (node.getRight()!=null)) node.setLeft(trim(node.getLeft())); node.setRight(trim(node.getRight())); } return node; } }