Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,676 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,202 people online right now. Registration is fast and FREE... Join Now!




URGENT HELP, please review my code related to binary tree

 
Reply to this topicStart new topic

URGENT HELP, please review my code related to binary tree

neokelly
post 16 Jul, 2007 - 01:27 PM
Post #1


New D.I.C Head

*
Joined: 16 Jul, 2007
Posts: 2


My Contributions


If you can, please post the answer by tonight!!! Thanks!!!

----------------------
1. Using C++ design two data structures in order to implement a simple
binary tree. Do this as follows: First, implement a node structure that
references a single "FooBar" item. Second, implement a root structure that
references a single node.

CODE

/*
* This is the structure of a node in the Binary Tree
*/
struct TreeNode {
   // An object of  this TreeNode represents one node
   // in a binary tree of strings.

   string FooBar;       // The data in this node.
   TreeNode *left;    // Pointer to left subtree.
   TreeNode *right;   // Pointer to right subtree.
   TreeNode(string str = "") {
          // This is the Constructor
          // Make a node containing the specified string.
      FooBar = str;
      left = NULL;
      right = NULL;
   }
};  // end struct TreeNode


I don’t understand why need a structure for “root” node.

----

2. Implement the insert function for the binary tree you've just designed.
Assume that the function accepts the root of the binary tree, and the
"FooBar" item to be inserted. The resulting binary tree should not contain
duplicate values.

CODE


/*
* A recursive function named treeInsert is used to add the item to the binary sort tree to which the parameter "root" refers.
*/
void treeInsert(TreeNode *root, string newItem) {
  
      // This sub function returns true if item is one of the items in the binary
      // sort tree to which root points.   Return false if not.
bool treeContains( TreeNode *root, string item ) {

    if ( root == NULL ) {
          // Empty tree
       return false;
    }
    else if ( item == root->FooBar ) {
          // Item being found in root node
       return true;
    }
    else if ( item < root->Foobar ) {
          // Search the lef tree
       return treeContains( root->left, item );
    }    
    else {
          // Search  the right tree
       return treeContains( root->right, item );
    }
}  // end treeContains()

// Here begins the body of the insertion function
if ( root == NULL ) {
          // Empty tree.  Set root to point to a new node containing
          // the new item.  This becomes the only node in the tree.
      root = new TreeNode( newItem );
      return;
   }
   else if  (treeContains(root, item)) {
              // Do not insert if the item is already in the tree
          cout << "\n That item is already in the tree. \n";
       }
   else if ( newItem < root->FooBar ) {
      treeInsert( root->left, newItem );
   }
   else {
      treeInsert( root->right, newItem );
   }
}
}  // end treeInsert()



----

3. Implement a non-recursive function that prints out the contents of the
"FooBar" items in order. Hint: You may need to modify the node structure
designed in section i) of this question in order to accomplish this
efficiently.

CODE


/ * Print the items in the tree in postorder, one item
   *  to a line.
   */
void traversalTree(TreeNode *root) {
stack stk;
TreeNode *temp;

if( root != NULL )  {
  temp=root;

      do  {
   while( temp != NULL)  {

      stk.push( temp );
      temp = temp->left;
}/*end while*/

if( !stk.empty() )   {
      temp = stk.pop();
      cout<<”     “ << temp->FooBar <<"     ";
     temp = temp->right;
  }/*end if*/
  else
    break;
  }while(1); /*end do while*/

}/* end if */
else
    cout<<" Empty tree";
} /*end function */

User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 16 Jul, 2007 - 05:36 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Do you have a question?
User is offlineProfile CardPM

Go to the top of the page

neokelly
post 16 Jul, 2007 - 07:15 PM
Post #3


New D.I.C Head

*
Joined: 16 Jul, 2007
Posts: 2


My Contributions


QUOTE(Amadeus @ 16 Jul, 2007 - 06:36 PM) *

Do you have a question?


my question is: is the code correctly written?
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 16 Jul, 2007 - 08:41 PM
Post #4


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


Compile it. See if it works.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:30AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month