answersLogoWhite

0

public class BinaryTreeExample {

public static void main(String[] args)

{

new BinaryTreeExample().run();

}

static class Node

{

Node left;

Node right;

int value;

public Node(int value) {

this.value = value;

}

}

public void run() {

Node rootnode = new Node(25);

System.out.println("Building tree with rootvalue

" + rootnode.value);

System.out.println("==========================");

printInOrder(rootnode);

}

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 " + 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 " + node.value);

node.right = new Node(value);

}

}

}

public void printInOrder(Node node) {

if (node != null) {

printInOrder(node.left);

System.out.println(" Traversed " + node.value);

printInOrder(node.right);

}

}

}

Output of the program

Building tree with root value 25

=================================

Inserted 11 to left of node 25

Inserted 15 to right of node 11

Inserted 16 to right of node 15

Inserted 23 to right of node 16

Inserted 79 to right of node 25

Traversing tree in order

=================================

Traversed 11

Traversed 15

Traversed 16

Traversed 23

Traversed 25

Traversed 79

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Why it is required to set path and classpath?

to compile and run java program you need to set path and classpath path refers to binary used to compile and run java program i.e. "javac" for compilation and "java" for execution


Is it possible to run java program without JVM?

No. Java programs run in the Java Virtual Machine (JVM) - without it your computer won't know how to handle Java bytecode.


What java plug-in do you need to play minecraft?

Any Java program just search Java on Google then download the latest version and then you can play :)! HAVE FUN


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.


How do you use java technology?

it depends on what you mean... if you want to program it id then suggest you started with a program called robocode it teaches to learn java. other wise if your trying to access a program then do a system search.


What is the method to find the height of a binary search tree in Java?

To find the height of a binary search tree in Java, you can use a recursive method that calculates the height of the left and right subtrees and returns the maximum height. This can be implemented by defining a method that takes the root node of the tree as input and recursively calculates the height of the tree.


In what career would one use Java collections?

Careers that use Java Collections include programmers who build data capture and storage programs, as a Java Collection is a framework that allows a programmer to implement reusable data into a program.


Are binary search and linear search already defined method in some class in java and what class is this?

You can check out the Arrays.binarySearch group of methods for searching sorted arrays. There is no predefined linear search for arrays, probably because it is trivially easy to implement. If you have some other data structure to search, the Collections.binarySearch methods should work for you. Most collections can also be converted to a List representation, which has a predefined indexOf method for linear searching.


What in java is not serializable?

Objects that do not implement Serializable.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


How to write a java program that determines the number of prime numbers less than N which is given by the user?

where to start? do you have an algorithm and just want to implement it in java? depends on how big N is, as that will determine which method is most efficient


How do you script a RuneScape bot?

RuneScape bots use Java. Use a Java Binary Code.