Created
February 19, 2012 23:18
-
-
Save thomashw/1866426 to your computer and use it in GitHub Desktop.
Example of implementing a binary tree. Includes adding nodes, printing, and searching the tree.
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 BinaryTree | |
{ | |
public void insert( TreeNode 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 TreeNode(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 TreeNode( value ); | |
} | |
} | |
} | |
public void run() { | |
TreeNode rootnode = new TreeNode(25); | |
/* Build tree */ | |
System.out.println("Building tree with rootvalue " + rootnode.value); | |
System.out.println("================================="); | |
insert(rootnode, 11); | |
insert(rootnode, 15); | |
insert(rootnode, 16); | |
insert(rootnode, 23); | |
insert(rootnode, 79); | |
/* Print tree in order */ | |
System.out.println("Traversing tree in order"); | |
System.out.println("================================="); | |
printInOrder(rootnode); | |
/* Search tree */ | |
System.out.println("Searching tree"); | |
System.out.println("================================="); | |
search( rootnode, 40 ); | |
search( rootnode, 11 ); | |
} | |
public void printInOrder( TreeNode node ) | |
{ | |
if( node != null ) { | |
printInOrder( node.left ); | |
System.out.println( " Traversed " + node.value ); | |
printInOrder( node.right ); | |
} | |
} | |
public void search( TreeNode node, int value ) | |
{ | |
if( node == null ) | |
System.out.println( " Could not find value " + value + " in tree." ); | |
else if( value < node.value ) | |
search( node.left, value ); | |
else if( value > node.value ) | |
search( node.right, value ); | |
else | |
System.out.println( " Found value " + value + " in tree." ); | |
} | |
public static void main( String[] args ) | |
{ | |
BinaryTree bt = new BinaryTree(); | |
bt.run(); | |
} | |
} |
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 TreeNode | |
{ | |
TreeNode left; | |
TreeNode right; | |
int value; | |
public TreeNode( int value ) | |
{ | |
this.value = value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment