trees in java
Java
package MyPackage;
public class Tree {
static class Node {
int value;
Node left, right;
Node(int value){
this.value = value;
left = null;
right = null;
}
}
public void insert(Node node, int value) {
if (value < node.value) { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted " + value + " to left of " + node.value); node.left = new Node(value); } } else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Inserted " + value + " to right of "
+ node.value);
node.right = new Node(value);
}
}
}
public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
System.out.print(" " + node.value);
traverseInOrder(node.right);
}
}
public static void main(String args[])
{
Tree tree = new Tree();
Node root = new Node(5);
System.out.println("Binary Tree Example");
System.out.println("Building tree with root value " + root.value);
tree.insert(root, 2);
tree.insert(root, 4);
tree.insert(root, 8);
tree.insert(root, 6);
tree.insert(root, 7);
tree.insert(root, 3);
tree.insert(root, 9);
System.out.println("Traversing tree in order");
tree.traverseLevelOrder();
}
}
Also in Java:
- Title
- system.arraycopy java
- Category
- Java
- Title
- Display double in decimal places java
- Category
- Java
- Title
- how to sort collection in java
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- adb: command not found
- Category
- Java
- Title
- usaco 2018 january contest
- Category
- Java
- Title
- prime number program in java
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- how to print to the console in java
- Category
- Java
- Title
- override java
- Category
- Java
- Title
- how to check how many anagrams a word has in java
- Category
- Java
- Title
- retrofit
- Category
- Java
- Title
- null pointer exception java
- Category
- Java
- Title
- number to char java
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- how to make an array in java
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- java arraylist in enum
- Category
- Java
- Title
- java concatenate arrays
- Category
- Java
- Title
- java stream find specific element
- Category
- Java
- Title
- protect java
- Category
- Java
- Title
- using class in java
- Category
- Java
- Title
- how to remove all items from alist in java
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- java
- Category
- Java
- Title
- how to create an array with unique values
- Category
- Java
- Title
- print a string java
- Category
- Java
- Title
- gradle require java version
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- java d'intervalle de resultat
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- java hello world
- Category
- Java
- Title
- java mockito print called methods
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- how to init an array with objects in java
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- palindrome function java
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- find the triplet sum in java linked list
- Category
- Java
- Title
- iterate through hashmap in java
- Category
- Java
- Title
- how to return the first character in an array from a method java
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- enum with numbers java
- Category
- Java
- Title
- how to append to a string in java
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- convert list of integer to array in java
- Category
- Java
- Title
- set preference value android
- Category
- Java
- Title
- java console write
- Category
- Java
- Title
- how to import a scanner in java
- Category
- Java
- Title
- java read each lines in file
- Category
- Java
- Title
- discord jda remove @everyone from channel
- Category
- Java
- Title
- java check for string length
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- how to convert string to double in android studio
- Category
- Java
- Title
- Jlabel icon
- Category
- Java
- Title
- How to create a 2d array in java
- Category
- Java
- Title
- run static method java
- Category
- Java
- Title
- java get command line output
- Category
- Java
- Title
- get sha key android
- Category
- Java
- Title
- action on long press of edit edittext
- Category
- Java
- Title
- convert int to string java
- Category
- Java
- Title
- how to send http post create request using curl command
- Category
- Java
- Title
- java copy array
- Category
- Java
- Title
- reverse linked list in java to get both head and tail
- Category
- Java
- Title
- goodbye java
- Category
- Java
- Title
- how to create an abstract method in java
- Category
- Java
- Title
- get world by name bukkit
- Category
- Java
- Title
- how to print the map in java
- Category
- Java
- Title
- java android development get element by id
- Category
- Java
- Title
- hello world java
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- java how to put a string sentence in a map
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- how to compare string in java
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- import java.io.serializable
- Category
- Java
- Title
- log4j with spring boot restful services
- Category
- Java
- Title
- Primitive Type vs. Reference Type
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- java remove first character from string
- Category
- Java
- Title
- type javascirpt
- Category
- Java
- Title
- java script removing first three indexes
- Category
- Java
- Title
- counting the number of characters in a string java
- Category
- Java
- Title
- waht does&& mean in java
- Category
- Java
- Title
- java if statement string equals scanner
- Category
- Java
- Title
- json request body not getting parsed by spring boot controller
- Category
- Java
- Title
- arraylist add method
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- java random numbers in specific range
- Category
- Java
- Title
- ndroid.support.v4.content.FileProvider
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- converter int array para string java
- Category
- Java
- Title
- processing sound library
- Category
- Java
- Title
- javadoc reference general toString
- Category
- Java