Created
September 21, 2012 03:50
-
-
Save mouseroot/3759626 to your computer and use it in GitHub Desktop.
TreeNode class
This file contains 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 static void insertNode(int id,String name) | |
{ | |
if(root == null) | |
{ | |
System.out.println("Craeteing new tree."); | |
root = new TreeNode(id,name); | |
return; | |
} | |
else | |
{ | |
TreeNode walker; | |
walker = root; | |
while(true) | |
{ | |
if(id < walker.id) //is the inserted item greater then the current walked item? | |
{ | |
if(walker.left == null) | |
{ | |
walker.left = new TreeNode(id,name); | |
return; | |
} | |
else | |
{ | |
walker = walker.left; | |
} | |
} | |
else | |
{ | |
if(walker.right == null) | |
{ | |
walker.right = new TreeNode(id,name); | |
return; | |
} | |
else | |
{ | |
walker = walker.right; | |
} | |
} | |
} | |
} | |
} |
This file contains 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
private static class TreeNode | |
{ | |
//Customer information | |
int id; | |
String name; | |
TreeNode left; | |
TreeNode right; | |
//Our constructor takes the id and first and last name | |
public TreeNode(int id,String name) | |
{ | |
this.id = id; | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment