Binary Tree

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

Join Date: Nov 2007
Posts: 10
Reputation: omeralper is an unknown quantity at this point 
Solved Threads: 1
omeralper omeralper is offline Offline
Newbie Poster

Binary Tree

 
0
  #1
Jan 25th, 2008
i want to implement a binary tree code but i encountered a problem. after i insert some numbers to tree, i couldn't print my numbers except first one or i couldn't insert the numbers except first one. As a result my binary tree doesn't work, it only shows the first(root) value. where is the problem? inserting or printing numbers?
here is my header file

  1. struct node{
  2. int index;
  3. node *rnode;
  4. node *lnode;
  5. };
  6. class MyTree
  7. {
  8. public:
  9. MyTree(void);
  10. void traverseInOrder();
  11. bool isEmpty()const;
  12. bool search(int item);
  13. bool insert(int newItem);
  14. private:
  15. bool searchByNode(int item, node *ptr);
  16. void inOrder(node *ptr);
  17. node *root;
  18. };
  19.  
  20. //here is implementation
  21. #include "MyTree.h"
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. MyTree::MyTree(void)
  26. {
  27. root=0;
  28. }
  29. bool MyTree::isEmpty()const{
  30. if(root==0)
  31. return true;
  32. else
  33. return false;
  34. }
  35. void MyTree::traverseInOrder(){
  36. inOrder(root);
  37. }
  38.  
  39. void MyTree::inOrder(node *ptr){
  40. if(ptr!=0)
  41. {
  42. inOrder(ptr->lnode);
  43. cout<<ptr->index<<endl;
  44. inOrder(ptr->rnode);
  45. }
  46. }
  47.  
  48. bool MyTree::insert(int newItem){
  49. node *newNode= new node();
  50. newNode->index=newItem;
  51. newNode->lnode=0;
  52. newNode->rnode=0;
  53.  
  54. node *current, *trailCurrent;
  55. if(isEmpty()){
  56. root=newNode;
  57. return true;
  58. }
  59. else{
  60. current=root;
  61. while(current!=0)
  62. {
  63. trailCurrent=current;
  64. if(current->index==newItem)
  65. cout<<"there is already such item"<<endl;
  66. else{
  67. if( newItem > current->index )
  68. current=current->rnode;
  69. else
  70. current=current->lnode;
  71. }
  72. }
  73. if(trailCurrent->index < newItem)
  74. newNode=trailCurrent->rnode;
  75. else
  76. newNode=trailCurrent->lnode;
  77. return true;
  78. }
  79. return false;
  80. }
  81.  
  82. bool MyTree::search(int item){
  83. return searchByNode(item,root);
  84. }
  85.  
  86. bool MyTree::searchByNode(int item, node *ptr){
  87. if(item==ptr->index)
  88. return true;
  89. else{
  90. if(item>ptr->index)
  91. searchByNode(item,ptr->rnode);
  92. else
  93. searchByNode(item,ptr->lnode);
  94. }
  95. return false;
  96. }
  97. //main
  98. MyTree test;
  99. test.insert(9);
  100. test.insert(13);
  101. test.insert(10);
  102. cout<<"traverse inorder:"<<endl;
  103. test.traverseInOrder();


Thank you...
Last edited by WolfPack; Jan 25th, 2008 at 9:12 am. Reason: To get Indentation and syntax highlighting use [CODE=CPP][/CODE] Tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 47
Reputation: sweety0 is an unknown quantity at this point 
Solved Threads: 0
sweety0 sweety0 is offline Offline
Light Poster

Re: Binary Tree

 
0
  #2
Jan 25th, 2008
  1. class MyTree
  2. {
  3. private://initialize things on top so that it will be easy for the compiler to initialize them
  4. // dear we dont make functions in private,they all r in public
  5. bool searchByNode(int item, node *ptr);
  6. void inOrder(node *ptr);
  7. node *root;
  8.  
  9. public:
  10. MyTree(void);// if it is a default constructor den no argument
  11. void traverseInOrder();
  12. bool isEmpty()const;
  13. bool search(int item); // it will only be int not int item because we dont pass any variable in da prototype
  14. bool insert(int newItem); //same here only int in paranthesis likewise (int)
  15. };


ur code will look like:::
  1. class MyTree
  2. {
  3. private:
  4. node *root;
  5.  
  6. public:
  7. MyTree();
  8. void traverseInOrder();
  9. bool isEmpty()const;
  10. bool search(int );
  11. bool insert(int );
  12. bool searchByNode(int , node *ptr);
  13. void inOrder(node *ptr);
  14. };
Last edited by WolfPack; Jan 25th, 2008 at 9:17 am. Reason: use [CODE=CPP][/CODE] Tags when you post code
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