java binary tree
Java
public class BinaryTree {
private int key;
private BinaryTree left, right;
/**
* Simple constructor.
*
* @param key
* to set as key.
*/
public BinaryTree(int key) {
this.key = key;
}
/**
* Extended constructor.
*
* @param key
* to set as key.
* @param left
* to set as left child.
* @param right
* to set as right child.
*/
public BinaryTree(int key, BinaryTree left, BinaryTree right) {
this.key = key;
setLeft(left);
setRight(right);
}
public int getKey() {
return key;
}
/**
* @return the left child.
*/
public BinaryTree getLeft() {
return left;
}
/**
* @return the right child.
*/
public BinaryTree getRight() {
return right;
}
public boolean hasLeft() {
return left != null;
}
public boolean hasRight() {
return right != null;
}
//Return a String representation of the BinaryTree using level order traversal
public String toString(){
int h = height(this);
int i;
String result = "";
for (i=1; i<=h; i++) {
result += printGivenLevel(this, i);
}
return result;
}
//returns the number of nodes in the BinaryTree
public int size(){
return size(this);
}
public static int size(BinaryTree tree){
if(tree == null) return 0;
return 1 + size(tree.getLeft()) + size(tree.getRight());
}
public int height(){ return height(this);}
public static int height(BinaryTree tree){
if(tree == null) return 0;
int left = height(tree.getLeft());
int right = height(tree.getRight());
return Math.max(left, right) + 1;
}
public String printGivenLevel (BinaryTree root ,int level) {
if (root == null) return "";
String result = "";
if (level == 1) {
result += root.getKey() + " ";
return result;
}else if (level > 1) {
String left = printGivenLevel(root.left, level-1);
String right = printGivenLevel(root.right, level-1);
return left + right;
}else{
return "";
}
}
/**
* @param left
* to set
*/
public void setLeft(BinaryTree left) {
this.left = left;
}
/**
* @param right
* to set
*/
public void setRight(BinaryTree right) {
this.right = right;
}
}
Also in Java:
- Title
- convert java list to array
- Category
- Java
- Title
- java read file text
- Category
- Java
- Title
- in java how to convert string to integer
- Category
- Java
- Title
- processing pi
- Category
- Java
- Title
- check if list contains new line in java
- Category
- Java
- Title
- java hashmap syntax
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- remove spaces at beginning and end of string java
- Category
- Java
- Title
- ecommerce app github android
- Category
- Java
- Title
- calculate days between two dates in java
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- how to use spring in java
- Category
- Java
- Title
- kotlin android intent pass data
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- random processing
- Category
- Java
- Title
- java while
- Category
- Java
- Title
- how to remove all components from layeredPane java
- Category
- Java
- Title
- add one character to string java
- Category
- Java
- Title
- java 8 function supplier consumer
- Category
- Java
- Title
- java php object
- Category
- Java
- Title
- height constraint layout guideline
- Category
- Java
- Title
- java get input
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- get sha key android
- Category
- Java
- Title
- processing play sound
- Category
- Java
- Title
- 3-way radix quicksort java
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- properties java 8 maven in pom xml
- Category
- Java
- Title
- how to show the hex detail of a file in java
- Category
- Java
- Title
- random boolean java
- Category
- Java
- Title
- java optional to collection
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- how to write sensor data into file android studio
- Category
- Java
- Title
- java d'intervalle de resultat
- Category
- Java
- Title
- java map iteration
- Category
- Java
- Title
- int to long java
- Category
- Java
- Title
- java map foreach
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- java run project from command line
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- how to use pow function in java
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- JavaFX mouse over effe
- Category
- Java
- Title
- file with line numbers inserted java
- Category
- Java
- Title
- how to check if a list is empty java
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- how to get the dimensions of a 2d array in java
- Category
- Java
- Title
- java self signed certificate truststore
- Category
- Java
- Title
- java hashmap get value
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- arraylist with values
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- create notification android
- Category
- Java
- Title
- immagini java
- Category
- Java
- Title
- on click android studio not working
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- java remove double spaces
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- java 8 collect multiple lists into single list
- Category
- Java
- Title
- iterate map in java
- Category
- Java
- Title
- how to get the max value of an array java
- Category
- Java
- Title
- java observable
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- java loop through array
- Category
- Java
- Title
- java permutation
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- converting char array to string
- Category
- Java
- Title
- javafx detect collision
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- java regex replace all characters before
- Category
- Java
- Title
- reading in lines from a file java
- Category
- Java
- Title
- how to make int array java android
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- javafx datepicker
- Category
- Java
- Title
- how to split a string in java
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- java read from connection even if 404
- Category
- Java
- Title
- convert hashset to int array java
- Category
- Java
- Title
- java convert a char[] to string
- Category
- Java
- Title
- bracket balancing program in java
- Category
- Java
- Title
- removeeventlistener
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- find length of array java
- Category
- Java
- Title
- java define main function
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- String array into LinkedList java
- Category
- Java
- Title
- java pause program until key pressed
- Category
- Java
- Title
- creating file in specific location in java application
- Category
- Java
- Title
- split by new line java
- Category
- Java
- Title
- java get size of array
- Category
- Java