Binary Search Tree

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Binary Search Tree

 
0
  #1
Mar 28th, 2006
I've been working on a binary search tree and I've written out most of what I think the code should look like but I can't test it because of an error I'm getting. I have no idea if what I have is even correct. My professor gave us the class, the structure, and the Main to use. My problem is in the getRoot(void) function. When I try compiling my code, it tells me that ptr is undeclared. I can't figure out what to change. I would appreciate it if someone could help me.

Here's my code:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. ///////////////////////////////////////////////
  5. // tnode structure
  6. typedef struct tnode
  7. {
  8. int data;
  9. struct tnode *leftChild;
  10. struct tnode *rightChild;
  11. }TNODE;
  12.  
  13. ///////////////////////////////////////////////
  14. //Binary search tree class
  15.  
  16. class BSTree
  17. {
  18. public:
  19. BSTree(void);
  20.  
  21. void addNode(int data);
  22. void findNode(TNODE *ptr, int data);
  23. void printTree(TNODE *ptr, int level);
  24. TNODE *getRoot(void);
  25. private:
  26. TNODE* ptr;
  27. };
  28.  
  29. ///////////////////////////////////////////////
  30. //main
  31.  
  32. int main()
  33. {
  34. BSTree mytree;
  35. int level = 1;
  36.  
  37. TNODE* tree;
  38. int i, numbers[] = {9, 7, 15, 5, 8, 14, 20, 4};
  39.  
  40. for(i = 0; i < 8; i++)
  41. mytree.addNode(numbers[i]);
  42. mytree.printTree(mytree.getRoot(), level);
  43. return 0;
  44. }
  45.  
  46. ////////////////////////////////////////////////
  47. //methods
  48.  
  49. BSTree::BSTree()
  50. {//constructor
  51. ptr = NULL; // indicates an empty list
  52. }
  53.  
  54. void BSTree::addNode(int data)
  55. {//adds root to the tree.
  56. if(ptr == NULL)
  57. {
  58. ptr->data = data;
  59. ptr->leftChild = NULL;
  60. ptr->rightChild = NULL;
  61. }
  62. else
  63. findNode(ptr, data);
  64. }
  65.  
  66. void BSTree::findNode(TNODE *ptr, int data)
  67. {//finds where to add the node. Traverse through tree then add the node.
  68. if ( data < ptr->data )
  69. {
  70. findNode(ptr->leftChild, data);
  71. }
  72. else if (data > ptr->data)
  73. {
  74. findNode(ptr->rightChild, data);
  75. }
  76. else if(ptr->data == NULL)
  77. {
  78. ptr->data = data;
  79. ptr->leftChild = NULL;
  80. ptr->rightChild = NULL;
  81. }
  82. }
  83.  
  84. void BSTree::printTree(TNODE *ptr, int level)
  85. {//prints out the tree on its side
  86. if(ptr != NULL)
  87. {
  88. printTree(ptr->rightChild, level + 1);
  89. for(int i = 0; i < 3 * level; i++)
  90. cout << " ";
  91. cout << ptr -> data << endl;
  92. printTree(ptr->leftChild, level + 1);
  93. }
  94. }
  95.  
  96. TNODE *getRoot(void)
  97. {//returns the root pointer
  98. return ptr;
  99. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Binary Search Tree

 
0
  #2
Mar 28th, 2006
You meant BSTree::getRoot.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Re: Binary Search Tree

 
0
  #3
Mar 28th, 2006
Thanks for the help. Atleast it will compile. Now I just need to try and figure out how to get this thing running correctly.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: Binary Search Tree

 
0
  #4
Mar 29th, 2006
I don't know exactly what your professor gave you, but the code you have is probably not
going to get you where you want to go. You don't really need the 'findNode' or 'getRoot'
functions (methods).

Here's some commented code that should get you started on the right track.
  1. BSTree::BSTree()
  2. {
  3. ptr = NULL; // indicates an empty list
  4. }
  5. BSTree::~BSTree()
  6. {
  7. // Add a destructor to delete the list on exit
  8. // otherwise you will end up with a memory leak
  9. }
  10. void BSTree::addNode(int data)
  11. {
  12. // create a new record (struct)
  13. // use a *curr for traversing
  14. if(curr == NULL)
  15. {
  16. // List is empty so add the first record (struct).
  17. }
  18. else
  19. {
  20. // There are records in list, so traverse according to rules until curr=NULL
  21. // and insert new record. - exit the loop using a break.
  22. while (curr != NULL)
  23. {
  24. }
  25. }
  26. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary Search Tree

 
0
  #5
Mar 29th, 2006
>I don't know exactly what your professor gave you
Probably something akin to bad Java via C++. Professors have a tendency to do that. :rolleyes:
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: blackdove is an unknown quantity at this point 
Solved Threads: 0
blackdove blackdove is offline Offline
Light Poster

Re: Binary Search Tree

 
0
  #6
Mar 30th, 2006
I finally got it to build a tree using what he gave us. I did, however, end up changing one of his method declarations a bit. This project was just to build the tree. The next one we have to do actually adds on to what we had to do here; adding a destructor, deletion, and traversals. Well this is what I ended up with:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. ///////////////////////////////////////////////
  5. // tnode structure
  6. typedef struct tnode
  7. {
  8. int data;
  9. struct tnode *leftChild;
  10. struct tnode *rightChild;
  11. }TNODE;
  12.  
  13. ///////////////////////////////////////////////
  14. //Binary search tree class
  15.  
  16. class BSTree
  17. {
  18. public:
  19. BSTree(void);
  20.  
  21. void addNode(int data);
  22. TNODE *findNode(TNODE *ptr, int data);
  23. void printTree(TNODE *ptr, int level);
  24. TNODE *getRoot(void);
  25. private:
  26. TNODE* ptr;
  27. };
  28.  
  29. ///////////////////////////////////////////////
  30. //main
  31.  
  32. int main()
  33. {
  34. BSTree mytree;
  35. int level = 1;
  36.  
  37. TNODE* tree;
  38. int i, numbers[] = {9, 7, 15, 5, 8, 14, 20, 4};
  39.  
  40. for(i = 0; i < 8; i++)
  41. mytree.addNode(numbers[i]);
  42. mytree.printTree(mytree.getRoot(), level);
  43. return 0;
  44. }
  45.  
  46. ////////////////////////////////////////////////
  47. //methods
  48.  
  49. BSTree::BSTree()
  50. {//constructor
  51. ptr = NULL; // indicates an empty list
  52. }
  53.  
  54. void BSTree::addNode(int data)
  55. {//adds root to the tree.
  56. if(ptr == NULL)
  57. {
  58. TNODE* newnode1 = new TNODE;
  59. newnode1->data = data;
  60. newnode1->leftChild = NULL;
  61. newnode1->rightChild = NULL;
  62. ptr = newnode1;
  63. }
  64. else
  65. findNode(ptr, data);
  66. }
  67.  
  68. TNODE *BSTree::findNode(TNODE *ptr, int data)
  69. {//finds where to add the node. Traverse through tree then add the node.
  70. if(ptr == NULL)
  71. {
  72. TNODE* newnode1 = new TNODE;
  73. newnode1->data = data;
  74. newnode1->leftChild = NULL;
  75. newnode1->rightChild = NULL;
  76. ptr = newnode1;
  77. return ptr;
  78. }
  79. else if(data < ptr->data)
  80. {
  81. ptr->leftChild = findNode(ptr->leftChild, data);
  82. }
  83. else
  84. ptr->rightChild = findNode(ptr->rightChild, data);
  85. return ptr;
  86. }
  87.  
  88. void BSTree::printTree(TNODE *ptr, int level)
  89. {//prints out the tree on its side
  90. if(ptr != NULL)
  91. {
  92. printTree(ptr->rightChild, level + 1);
  93. for(int i = 0; i < 3 * level; i++)
  94. cout << " ";
  95. cout << ptr -> data << endl;
  96. printTree(ptr->leftChild, level + 1);
  97. }
  98. }
  99.  
  100. TNODE *BSTree::getRoot(void)
  101. {//returns the root pointer
  102. return (ptr);
  103. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: Binary Search Tree

 
0
  #7
Mar 31st, 2006
Just for my own education in C/C++.

In the above thread, the OP has used recursion to insert an item in the list. (I assume that his 'professor' gave the OP this structure).

Is this an approved method? I generally use a while loop for that type of insertion. It seems to me that the recursion method would be incredibably resource intensive.

Your comments, please.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary Search Tree

 
0
  #8
Mar 31st, 2006
>Is this an approved method?
It depends on the programmer and the application.

>It seems to me that the recursion method would be incredibably resource intensive.
For a basic binary search tree, it can easily be. If the tree is suitably random then it should be appropriately balanced and recursion won't cause too much trouble. If the tree gets near any of the degenerate cases then the recursion could get too deep for the system to handle, and the application will break.

>Just for my own education in C/C++.
http://www.eternallyconfuzzled.com/tuts/bst1.html
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 43
Reputation: Nedals is an unknown quantity at this point 
Solved Threads: 0
Nedals Nedals is offline Offline
Light Poster

Re: Binary Search Tree

 
0
  #9
Mar 31st, 2006
Thanks for the link. Bookmarked
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC