944,121 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10496
  • C++ RSS
Mar 28th, 2006
0

Binary Search Tree

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
blackdove is offline Offline
46 posts
since Feb 2005
Mar 28th, 2006
2

Re: Binary Search Tree

You meant BSTree::getRoot.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Mar 28th, 2006
0

Re: Binary Search Tree

Thanks for the help. Atleast it will compile. Now I just need to try and figure out how to get this thing running correctly.
Reputation Points: 11
Solved Threads: 0
Light Poster
blackdove is offline Offline
46 posts
since Feb 2005
Mar 29th, 2006
0

Re: Binary Search Tree

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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 0
Light Poster
Nedals is offline Offline
43 posts
since Dec 2005
Mar 29th, 2006
0

Re: Binary Search Tree

>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:
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 30th, 2006
0

Re: Binary Search Tree

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:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 0
Light Poster
blackdove is offline Offline
46 posts
since Feb 2005
Mar 31st, 2006
0

Re: Binary Search Tree

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.
Reputation Points: 11
Solved Threads: 0
Light Poster
Nedals is offline Offline
43 posts
since Dec 2005
Mar 31st, 2006
0

Re: Binary Search Tree

>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
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 31st, 2006
0

Re: Binary Search Tree

Thanks for the link. Bookmarked
Reputation Points: 11
Solved Threads: 0
Light Poster
Nedals is offline Offline
43 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Looking up and displaying values in linked lists
Next Thread in C++ Forum Timeline: is it bad





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC