Results 1 to 8 of 8

Thread: Data Tree

  1. #1
    Join Date
    Jul 2003
    New York City

    Data Tree

    Okay, now this may be an irritatingly simple issue, but I've had no formal education in programming and am a bit confused on this. I've had some instances where I've found a data tree to be the best method of dealing with information, as the heirarchal data is important. So now, be it from a Database or an XML file or whatever, how could I store this tree within the system? I need to be able to easily transverse the tree and edit, remove, add nodes. So would I store this infromation in a multi-dimensional array? Or as a number of Objects?

    I'm asking here because it's not really specific to a language. Wow could I store this tree so that I can work with it? I don't think storing it as a string would make sense. Am I not making sense? Or am I looking at this the wrong way?

  2. #2
    Join Date
    Jan 2004
    Melbourne, Australia
    In an Object-Oriennted environment, you would normally create one heirarchical object that branches out to its members. It would work in a similar way to a multi-dimensional array but with a plethora of extra functionality and performance benefits. You would write methods to traverse the tree and manage its nodes.

    Make sense? If you have eny experience with Java, you could take a look at the few premade Tree-like objects.


  3. #3
    Join Date
    Jul 2003
    New York City
    See, an Object Oriented approach was making the most sense to me. But still, OOP is something I still have a bit of a problem incorporating into programming I do (since my post "programming" experience is PHP, which doesn't force the Object Oriented approach). Admittedly the most OO I've used is as I learn Java, but I'm still confused as to how I would be able to maintain the heirarchal data. How would I be able to maintain that "This object's parent is such-and-such object" ?

  4. #4
    Join Date
    Jan 2004
    Melbourne, Australia
    I don't really understand your question. We are seeing the same question from such a different perspective.


  5. #5
    Join Date
    Jul 2003
    New York City
    Erm, basically, I'm still confused as to how I would do this, in say an Object Oriented enviroment.

  6. #6
    Join Date
    Jan 2004
    Melbourne, Australia
    Its a very complex subject with no real "best" approach. It's the sort of thing you will find alot of books on, however. I don't really have what it takes to teach OO Data Structures.

    The most appropriate answer would depend on the actual context of the question. Are you referring to a HTTP -- stateless -- environment?


  7. #7
    Join Date
    Jul 2003
    The City of Roses
    Basically, a tree node would consist of a grouped structure with three (optionally four) pointers/references. One for the data, two for the left and right subtrees, and an optional fourth referring to the parent (there are of course other variations, but this is the most basic). Exactly how this structure of pointers is grouped is language dependant. For example, C has its struct keyword. Perl traditionally uses hashes. (Anyone else know how PHP implements objects?)

    (Example tree)
       /     \
      B       F
     / \     / \
    A   C   E   G
    So, to find the smallest value in the tree (which would be the left-most node) would work something like this (psuedo code)
    p = root
    while (p->left != NULL)
    	p = p->left
    p starts off as a pointer to node D. The first time through the loop p (currently node D) is replaced with the pointer value that is node D's left subtree, making p now a pointer to B. And so forthe until p points to a node that has no left subtree.

    Here is another tree algorithm that returns the next inorder node.
    if (p->right != NULL)
    	p = p->right
    	while (p->left != NULL)
    		p = p->left
    	return p
    if (p->parent == NULL)
    	return NULL  // we're done
    if (p->parent->left == p)  // p is a left subtree
    	return p->parent
    while (p->parent != NULL && p->parent->right == p)  // while p is a right subtree
    	p = p->parent
    return p->parent
    With this algorithm we get

    inorder(A) returns B
    inorder(B) returns C
    inorder(c) returns D
    inorder(G) returns NULL

    For emphasis on modular programming, it's good to wrap the whole thing up in an abstract data type (ADT). That means that whatever part of your program that uses the tree should have no knowledge of the inside workings. That means no concept of subtrees, no concept of nodes, not concept of a root. The only thing anyone else should know is that we can insert, retrieve, and delete data from the tree. How it does it: don't know, don't care.

    To get a lot more detailed information I'd recommend The Art of Computer Programming. These books will make up for any lack of formal training. It covers all the fundamental data structures and other not so fundamental data structures that even college graduates may not have seen, as well as all the formal mathematics involved.

    Though this book can be some very heavy reading. If it seems to be too much you could start with Algorithms in (insert language here). This book has editions for several different languages, but this link is the one for Java. Note, however, that the actual code in this book is there to demonstrate a particular algorithm, not teach you good programming skills. The structure and style of the code probably should not be followed. There once was an edition of this book in psuedo code, but unfortunately it's no longer in print.

    Anyways, if you do opt for the second book keep in mind that the former is the bible for the given subject, and I'd recommend you do get it eventually regardless where you start.
    Last edited by Jeff Mott; 06-27-2005 at 07:54 AM.

  8. #8
    Join Date
    Jul 2003
    New York City
    Thank you very much. This is starting to make much more sense to me now!

    And I'll also take a look at those books as well, thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11722 seconds
  • Memory Usage 2,914KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates