View Single Post
  #6 (permalink)  
Old 08-05-2007, 01:19 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Icon4 Help! BinarySearchTree Insert

Here's the last code that I got for "insert" implementation, please take a look and see if there's anything wrong. For some reason, I don't get ordered number when I did "inorder" to my BinarySearchTrees

PHP Code:
    public void insert(BinaryNode node) {
        
BinaryNode n this.root;
        if (
Double.parseDouble(node.getValue().toString()) < Double.parseDouble(n.getValue().toString())) {
            while (
!= null) {
                if (
Double.parseDouble(node.getValue().toString()) < Double.parseDouble(n.getValue().toString()) && n.getLeft() == null) {
                    
n.setLeft(node);
                } else if (
Double.parseDouble(node.getValue().toString()) > Double.parseDouble(n.getValue().toString()) && n.getRight() == null) {
                    
n.setRight(node);
                }
                
n.getLeft();
            }
        } else if (
Double.parseDouble(node.getValue().toString()) > Double.parseDouble(n.getValue().toString())) {
            while (
!= null) {
                if (
Double.parseDouble(node.getValue().toString()) > Double.parseDouble(n.getValue().toString()) && n.getRight() == null) {
                    
n.setRight(node);
                } else if (
Double.parseDouble(node.getValue().toString()) < Double.parseDouble(n.getValue().toString()) && n.getLeft() == null) {
                    
n.setLeft(node);
                }
                
n.getRight();
            }
        } else {
            
System.out.println("SAME NUMBER EXCEPTION!");
        }
    } 
here's the inorder implementation
PHP Code:
    public void inorder(BinaryNode node) {
        if (
node == null) {
            return;
        }
        
inorder(node.getLeft());
        
System.out.println(node.getValue()); // visit the code
        
inorder(node.getRight());
    } 

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 08-05-2007 at 01:23 PM.
Reply With Quote