how can I insert a alement into the Binary tree search in java

Alireza1227
Alireza1227 used Ask the Experts™
on
     public NonEmptyTree<K, V> insert(K key, V value) {

            
            
      }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Looks like classwork - please see the guidelines on that
Commented:
heres a usefull link:
http://www.roseindia.net/java/java-get-example/java-binary-tree-insert.shtml


public class BinarytreeInsert {
 
    public static void main(String[] args) {
        new BinarytreeInsert().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 root value " + rootnode.value);
        System.out.println("=================================");
        insert(rootnode, 11);
        insert(rootnode, 15);
        insert(rootnode, 16);
        insert(rootnode, 23);
        insert(rootnode, 79);
 
    }
    
 
    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);
            }
        }
    }
} 

Open in new window

Commented:
theres this site too that can help, has some good tips on binary search trees:
http://www.java-tips.org/java-se-tips/java.lang/binary-search-tree-implementation-in-java.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial