Created
September 17, 2013 12:28
-
-
Save bjhaid/6593680 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OrdBok { | |
public static void main (String args[]) { | |
new OrdBok().run(); | |
} | |
public void run() { | |
BinNode testnode = new BinNode("Ken"); | |
System.out.println("Tree root node " + testnode.value); | |
testnode.addChild(new BinNode("Kfen")); | |
// addChild(new BinNode("Kfen")); | |
testnode.addChild(new BinNode("Alice")); | |
testnode.addChild(new BinNode("Betty")); | |
testnode.addChild(new BinNode("Lisa")); | |
testnode.addChild(new BinNode("Sean")); | |
} | |
BinNode root = new BinNode("Susin"); | |
class BinNode { | |
String value; | |
BinNode left; | |
BinNode right; | |
public BinNode(String value) { | |
this.value = value; | |
} | |
public boolean compare(BinNode node) { | |
if (node.value.compareTo(this.value) < 0) { | |
System.out.println(node.value + " < " + this.value); | |
return true; | |
} else return false; | |
} | |
public void addChild(BinNode node) { | |
if (this.compare(node)) { | |
if (node.left != null) { | |
this.addChild(node.left); | |
} else { | |
this.left = new BinNode(node.value); | |
System.out.println(" Inserted " + node.value + " to the left of Node " + this.value); | |
} | |
} else if (!this.compare(node)) { | |
if (node.right != null ) { | |
this.addChild(node.right); | |
System.out.println(" Inserted " + node.value + " to the right of Node " + this.value); | |
} else { | |
System.out.println(" Inserted " + node.value + " to the right of Node " + this.value); | |
this.right = new BinNode(node.value); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment