Thread: binary search trees

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    Question binary search trees

    I want to create the root pointer in my program not in the class
    bst.h
    --BSTtree class (no pointers)?
    bst.cpp
    bstprog.cpp

    &&&&&&&

    what does this line do/mean

    typedef treeNode *treeNodePtr;






  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    trees generally use pointers to nodes rather than nodes as such. the root node is therefore better defined as a pointer to a given node, not a node as such. This enables you to pass the entry point for the data stored in the tree from function to function much easier. if your bst is a templated class then root would be set to null in the header file by the constructor. If the bst class is not templated then you would set root to null in constructor definition in the cpp file for the bst class. to insert data you create a new node using dynamic memory, ie create a pointer to a new node declared in dynamic memory. If the bst is empty you assign the new node to the root node, and if not you find the spot to insert the new node by searching the tree.


    typedef treeNode *treeNodePtr;

    this means that whenever you write treeNodePtr you mean treeNode *;

    like this:

    treeNodePtr currentNode;

    instead of like this:

    treeNode * currentNode;

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to create the root pointer in my program not in the class
    Why? If you're using a class then theoretically you should be hiding the internal mechanisms of the tree from client code. Therefore, the tree class would control the root pointer.

    >--BSTtree class (no pointers)?
    While you can implement a binary search tree without pointers, you really don't want to. Take my word for it.

    >typedef treeNode *treeNodePtr;
    It creates a synonym for treeNode*. Every variable you declare as treeNodePtr will really be treeNode*.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with binary search.
    By StateofMind in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 02:14 PM
  2. Binary Search Trees
    By lorannex in forum C Programming
    Replies: 3
    Last Post: 04-25-2009, 06:24 AM
  3. Performance issue!
    By maven in forum C Programming
    Replies: 42
    Last Post: 03-23-2009, 11:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM