Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include<iostream>
  2. #include<stack>
  3. using namespace std;
  4. class node
  5. {
  6. public:
  7. string item; //data item
  8. double cost; //data item
  9. node *pleft;
  10. node *pright;
  11. node *pparent;
  12. //ptr to next node in list
  13.  
  14. node(string s, double c) //constructor
  15. { //to insert data
  16. item=s;
  17. cost=c;
  18. pleft=NULL;
  19. pright=NULL;
  20. }
  21. void displaynode()
  22. {
  23. cout << "{" << item << ", " << cost << "} ";
  24. }
  25. };
  26. class Tree
  27. {
  28. private:
  29. node* proot; //ptr to first node on list
  30. public:
  31. Tree() //constructor
  32. {
  33. proot = NULL;
  34. } //(no nodes on list yet)
  35. //insert at start of list
  36.  
  37. int insertFirst(string i, double c)
  38. {
  39. node* pNewnode = new node(i, c); //creates new node
  40.  
  41. if(proot==NULL)
  42. proot = pNewnode;
  43. else
  44. {
  45. node *pcurrent=proot;
  46. node *pparent;
  47. while(1)//loop through tree
  48. {
  49. pparent=pcurrent;//remember where you have come from
  50. if(i<pcurrent->item)//which direction
  51. {
  52. pcurrent=pcurrent->pleft;
  53. if(pcurrent==NULL)
  54. {
  55.  
  56. pparent->pleft=pNewnode;
  57. return 0;
  58. }
  59. }
  60. else
  61. {
  62. pcurrent=pcurrent->pright;
  63. if(pcurrent==NULL)
  64. {
  65.  
  66. pparent->pright=pNewnode;
  67. return 0;
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. void display()
  75. {
  76. stack<node*> globalStack;
  77. globalStack.push(proot);
  78. int nBlanks = 32;
  79. bool isRowEmpty = false;
  80. cout <<
  81. "......................................................";
  82. cout << endl;
  83. while(isRowEmpty==false)
  84. {
  85. stack<node*> localStack;
  86. isRowEmpty = true;
  87. for(int j=0; j<nBlanks; j++)
  88. cout << ' ';
  89. while(globalStack.empty()==false)
  90. {
  91. node* temp = globalStack.top();
  92. globalStack.pop();
  93. if(temp != NULL)
  94. {
  95. cout << temp->item;
  96. localStack.push(temp->pleft);
  97. localStack.push(temp->pright);
  98. if(temp->pleft != NULL ||
  99. temp->pright != NULL)
  100. isRowEmpty = false;
  101. }
  102. else
  103. {
  104. cout << "--";
  105. localStack.push(NULL);
  106. localStack.push(NULL);
  107. }
  108. for(int j=0; j<nBlanks*2-2; j++)
  109. cout << ' ';
  110. } //end while globalStack not empty
  111. cout << endl;
  112. nBlanks /= 2;
  113. while(localStack.empty()==false)
  114. {
  115. globalStack.push( localStack.top() );
  116. localStack.pop();
  117. }
  118. } //end while isRowEmpty is false
  119. cout <<
  120. "......................................................";
  121. cout << endl;
  122.  
  123. }
  124.  
  125. int find(string key) //find link with given key
  126. { //(assumes non-empty list)
  127. node* pcurrent = proot; //start at ‘first’
  128. while(pcurrent->item != key) //while no match,
  129. {if(pcurrent == NULL) //if end of list,
  130. {cout<<"Not Found"<<endl;
  131. return 0;
  132. }
  133. else if (pcurrent->item <key)
  134. {
  135. pcurrent = pcurrent->pright;
  136. }
  137. else
  138. {
  139. pcurrent=pcurrent->pleft;
  140. }
  141. }
  142. cout<<"Found "<<key<<endl;
  143. return 0; //found it
  144. }
  145. void inorder(node*pLocalRoot)
  146. {
  147. if(pLocalRoot !=NULL)
  148. {
  149. inorder(pLocalRoot->pleft);
  150. pLocalRoot->displaynode();
  151. inorder(pLocalRoot->pright);
  152. }
  153. }
  154.  
  155. void traverse()
  156. {
  157. Tree x;
  158. x.inorder(proot);
  159. }
  160.  
  161. void findmin()
  162. {
  163. node* pcurrent= proot;
  164. while(pcurrent->pleft != NULL)
  165.  
  166. {
  167. pcurrent=pcurrent->pleft;
  168. }
  169.  
  170. pcurrent->displaynode();
  171. }
  172. void findmax()
  173. {
  174. node*pcurrent=proot;
  175. while(pcurrent->pright !=NULL)
  176. {
  177. pcurrent=pcurrent->pright;
  178. }
  179. pcurrent->displaynode();
  180. }
  181. }; //end class node
  182. int main()
  183. {
  184. Tree thetree; //make new list
  185. thetree.insertFirst("Milk", 50.00);
  186. thetree.insertFirst("Bread", 70.00);
  187. thetree.insertFirst("Sugar", 150.00);
  188. thetree.insertFirst("Salt", 10.00);
  189. thetree.insertFirst("Yam", 50.00);
  190. thetree.insertFirst("Crisps", 70.00);
  191. thetree.insertFirst("Apples", 70.00);
  192.  
  193.  
  194.  
  195. thetree.display();
  196.  
  197. thetree.find("Crisps");
  198. thetree.findmin();
  199. thetree.traverse();
  200. thetree.findmax();
  201. return 0;
  202. } //end main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement