Skip to content

Instantly share code, notes, and snippets.

@mouseroot
Created September 21, 2012 03:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mouseroot/3759626 to your computer and use it in GitHub Desktop.
Save mouseroot/3759626 to your computer and use it in GitHub Desktop.
TreeNode class
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;
}
}
}
}
}
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