Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-16-2008, 11:35 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,467
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Binary Tree Implementation in Java
Code:
public class BinaryTreeTest { public static void main(String[] args) { new BinaryTreeTest().run(); } static class Node { Node left; Node right; int value; public Node(int value) { this.value = value; } } public void run() { // build the simple tree from chapter 11. Node root = new Node(5); System.out.println("Binary Tree Example"); System.out.println("Building tree with root value " + root.value); insert(root, 1); insert(root, 8); insert(root, 6); insert(root, 3); insert(root, 9); System.out.println("Traversing tree in order"); printInOrder(root); System.out.println("Traversing tree front-to-back from location 7"); printFrontToBack(root, 7); } public void insert(Node 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.value); node.left = new Node(value); } } else if (value > node.value) { if (node.right != null) { insert(node.right, value); } else { System.out.println(" Inserted " + value + " to right of " + node.value); node.right = new Node(value); } } } public void printInOrder(Node node) { if (node != null) { printInOrder(node.left); System.out.println(" Traversed " + node.value); printInOrder(node.right); } } /** * uses in-order traversal when the origin is less than the node's value * * uses reverse-order traversal when the origin is greater than the node's * order */ public void printFrontToBack(Node node, int camera) { if (node == null) return; if (node.value > camera) { // print in order printFrontToBack(node.left, camera); System.out.println(" Traversed " + node.value); printFrontToBack(node.right, camera); } else if (node.value < camera) { // print reverse order printFrontToBack(node.right, camera); System.out.println(" Traversed " + node.value); printFrontToBack(node.left, camera); } else { // order doesn't matter printFrontToBack(node.left, camera); printFrontToBack(node.right, camera); } } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can anybody help with cuncurrent binary search tree guys) danylo Threads and Synchronization 1 04-23-2008 07:22 PM
Soundex Algorithm Implementation in Java Java Tip java.lang 0 04-12-2008 09:40 PM
Java Telnet App Implementation mgdesmond13 New To Java 0 12-26-2007 07:08 PM
Java Telnet App Implementation mgdesmond13 Java Applets 0 12-26-2007 04:15 PM
Tree structure using JAVA trill Advanced Java 1 08-02-2007 03:50 PM


All times are GMT +3. The time now is 01:34 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org