Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 8th, 2006, 10:32 PM
Authorized User
Join Date: Nov 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to pandjie
Default tree data structure

I want to create a tree data structure [not binary tree nor JTree] with unlimited branch and I can go in each branch in that tree. Anyone can give me an example? or URL link..


Old January 16th, 2006, 05:15 AM
Friend of Wrox
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts

Maybe something like this?

import java.util.*;

public class NaryTree
private ArrayList children = new ArrayList();
private Object value;

public NaryTree(Object value)
    this.value = value;

public NaryTree getChild(int n)
    return (NaryTree)children.get(n);

public void putChild(int n, NaryTree child)
    children.add(n, child);

public Object getValue()
    return value;

public void setValue(Object value)
    this.value = value;

Of course, this is basically just an ArrayList with a value associated with it. So you could just extend ArrayList, add the value getter and setter, and have your N-ary tree. But the code might be a little harder to understand without some good Javadocs.

Jon Emerson
Adobe Systems, Inc.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing tree structure rjonk XSLT 6 November 16th, 2006 10:11 AM
Need help with tree data structure vidhya_venkat C++ Programming 0 June 14th, 2006 01:13 PM
Creating a Tree Structure pazzuzu C++ Programming 2 February 26th, 2005 12:07 PM
Show data in tree-view structure kiennt Crystal Reports 1 March 1st, 2004 06:21 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.