Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.77 KB | None | 0 0
  1. // BST_Delete.cpp : Defines the entry point for the console application.
  2. //tree.cpp
  3. //demonstrates binary tree
  4. #include <iostream>
  5. #include <stack>
  6. using namespace std;
  7. /////////////////////////////////////////////////////////////
  8. class Node
  9. {
  10. public:
  11. int iData; //data item (key)
  12. double dData; //data item
  13. Node* pLeftChild; //this node's left child
  14. Node* pRightChild; //this node's right child
  15. //----------------------------------------------------------
  16. //constructor
  17. Node() : iData(0), dData(0.0), pLeftChild(NULL),
  18. pRightChild(NULL)
  19. { }
  20. //----------------------------------------------------------
  21. ~Node() //destructor
  22. { cout << "X-" << iData << " "; }
  23. //----------------------------------------------------------
  24. void displayNode() //display ourself: {75, 7.5}
  25. {
  26. cout << '{' << iData << ", " << dData << "} ";
  27. }
  28. }; //end class Node
  29. /////////////////////////////////////////////////////////////
  30. class Tree
  31. {
  32. private:
  33. Node* pRoot; //first node of tree
  34.  
  35. public:
  36. //----------------------------------------------------------
  37. Tree() : pRoot(NULL) //constructor
  38. { }
  39. //----------------------------------------------------------
  40. Node* find(int key) //find node with given key
  41. { //(assumes non-empty tree)
  42. Node* pKeyPointer = pRoot; //start at root
  43. while(pKeyPointer->iData != key) //while no match,
  44. {
  45. if(key < pKeyPointer->iData) //go left?
  46. pKeyPointer = pKeyPointer->pLeftChild;
  47. else //or go right?
  48. pKeyPointer = pKeyPointer->pRightChild;
  49. if(pKeyPointer == NULL) //if no child,
  50. return NULL; //didn't find it
  51. }
  52. return pKeyPointer; //found it
  53. } //end find()
  54. //-----------------------------------------------------------
  55. void del (int key)
  56. {
  57. Node* KeyPointer;
  58. Node* current;
  59. Node* parent;
  60. Node* temp;
  61.  
  62. KeyPointer = pRoot;
  63. while (KeyPointer->iData != key) //finding the pointer with key data
  64. {
  65. if (KeyPointer->iData > key)
  66. {
  67. KeyPointer = KeyPointer->pLeftChild;
  68. }
  69. else if (KeyPointer->iData < key)
  70. {
  71. KeyPointer = KeyPointer->pRightChild;
  72. }
  73. } //when loop ends, KeyPointer will be pointer with key
  74.  
  75. if (KeyPointer = NULL)
  76. {
  77. cout << "Node to be deleted is NULL" << endl;
  78. }
  79.  
  80. else if (KeyPointer->pRightChild == NULL && KeyPointer->pLeftChild == NULL) //case 1: No child
  81. {
  82. temp = KeyPointer;
  83. KeyPointer = NULL;
  84. delete temp;
  85. }
  86.  
  87. else if (KeyPointer->pRightChild == NULL) //case 2: one child
  88. {
  89. temp = KeyPointer;
  90. KeyPointer = KeyPointer->pLeftChild;
  91. delete temp;
  92. }
  93.  
  94. else if (KeyPointer->pLeftChild == NULL)
  95. {
  96. temp = KeyPointer;
  97. KeyPointer = KeyPointer->pRightChild;
  98. delete temp;
  99. }
  100.  
  101. else //case 3: two child
  102. {
  103. current = KeyPointer->pRightChild; //smallest of right subtree approach
  104. parent = NULL;
  105.  
  106. while (current->pLeftChild != NULL)
  107. {
  108. parent = current;
  109. current = current->pLeftChild;
  110. }
  111.  
  112. KeyPointer->iData = current->iData; //swapping data
  113.  
  114. if (parent == NULL) //did not traverse,
  115. {
  116. KeyPointer->pRightChild = current->pRightChild;
  117. }
  118.  
  119. else //to link any subtrees under deleted node
  120. {
  121. parent->pLeftChild = current->pRightChild;
  122. }
  123.  
  124. delete current;
  125. }
  126. }//end delete
  127. //-----------------------------------------------------------
  128. void insert(int id, double dd) //insert new node
  129. {
  130. Node* pNewNode = new Node; //make new node
  131. pNewNode->iData = id; //insert data
  132. pNewNode->dData = dd;
  133. if(pRoot==NULL) //no node in root
  134. pRoot = pNewNode;
  135. else //root occupied
  136. {
  137. Node* pKeyPointer = pRoot; //start at root
  138. Node* pParent;
  139. while(true) //(exits internally)
  140. {
  141. pParent = pKeyPointer;
  142. if(id < pKeyPointer->iData) //go left?
  143. {
  144. pKeyPointer = pKeyPointer->pLeftChild;
  145. if(pKeyPointer == NULL) //if end of the line,
  146. { //insert on left
  147. pParent->pLeftChild = pNewNode;
  148. return;
  149. }
  150. } //end if go left
  151. //if num to be inserted matches existing numbers
  152. //insert on right
  153. else //or go right?
  154. {
  155. pKeyPointer = pKeyPointer->pRightChild;
  156. if(pKeyPointer == NULL) //if end of the line
  157. { //insert on right
  158. pParent->pRightChild = pNewNode;
  159. return;
  160. }
  161. } //end else go right
  162. } //end while
  163. } //end else not root
  164. } //end insert()
  165. //-----------------------------------------------------------
  166. void traverse(int traverseType)
  167. {
  168. //your code
  169. switch (traverseType)
  170. {
  171. case 1: cout << "\nPreorder traversal: ";
  172. preOrder (pRoot);
  173. break;
  174.  
  175. case 2: cout << "\nInorder traversal: ";
  176. inOrder (pRoot);
  177. break;
  178.  
  179. case 3: cout << "\nPostorder traversal: ";
  180. postOrder (pRoot);
  181. break;
  182. }
  183. cout << endl;
  184. }
  185. //-----------------------------------------------------------
  186. void preOrder(Node* pLocalRoot)
  187. {
  188. //your code
  189. if (pLocalRoot != NULL)
  190. {
  191. cout << pLocalRoot -> iData << " " ;
  192. preOrder (pLocalRoot -> pLeftChild);
  193. preOrder (pLocalRoot -> pRightChild);
  194. }
  195.  
  196. }
  197. //----------------------------------------------------------
  198. void inOrder(Node* pLocalRoot)
  199. {
  200. //your code
  201. if (pLocalRoot != NULL)
  202. {
  203. inOrder (pLocalRoot -> pLeftChild);
  204. cout << pLocalRoot -> iData << " " ;
  205. inOrder (pLocalRoot -> pRightChild);
  206. //when it goes to the right, it doesnt print yet
  207. //but goes to the left of the one on the right
  208.  
  209. //leftmost: 12
  210. //then prints itself: 25
  211. //then goes to right (37) to the leftmost: 30
  212. //then prints itself (30)
  213. //then goes to the right: 33
  214. //done with leftChild of 37
  215. //prints itself: 37
  216. }
  217. }
  218. //---------------------------------------------------------
  219. void postOrder(Node* pLocalRoot)
  220. {
  221. //your code
  222. if (pLocalRoot != NULL)
  223. {
  224. postOrder (pLocalRoot -> pLeftChild);
  225. postOrder (pLocalRoot -> pRightChild);
  226. cout << pLocalRoot -> iData << " " ;
  227. }
  228. }
  229. //-----------------------------------------------------------
  230. void displayTree()
  231. {
  232. stack<Node*> globalStack;
  233. globalStack.push(pRoot);
  234. int nBlanks = 32;
  235. bool isRowEmpty = false;
  236.  
  237. cout <<
  238. "....................................................";
  239. cout << endl;
  240. while(isRowEmpty==false)
  241. {
  242. stack<Node*> localStack;
  243. isRowEmpty = true;
  244.  
  245. for(int j=0; j<nBlanks; j++)
  246. cout << ' ';
  247.  
  248. while(globalStack.empty()==false)
  249. {
  250. Node* temp = globalStack.top();
  251. globalStack.pop();
  252. if(temp != NULL)
  253. {
  254. cout << temp->iData;
  255. localStack.push(temp->pLeftChild);
  256. localStack.push(temp->pRightChild);
  257.  
  258. if(temp->pLeftChild != NULL ||
  259. temp->pRightChild != NULL)
  260. isRowEmpty = false;
  261. }
  262. else
  263. {
  264. cout << "--";
  265. localStack.push(NULL);
  266. localStack.push(NULL);
  267. }
  268. for(int j=0; j<nBlanks*2-2; j++)
  269. cout << ' ';
  270. } //end while globalStack not empty
  271. cout << endl;
  272. nBlanks /= 2;
  273. while(localStack.empty()==false)
  274. {
  275. globalStack.push( localStack.top() );
  276. localStack.pop();
  277. }
  278. } //end while isRowEmpty is false
  279. cout <<
  280. "...................................................";
  281. cout << endl;
  282. } //end displayTree()
  283. //---------------------------------------------------------
  284. void destroy() //deletes all nodes
  285. { destroyRec(pRoot); } //start at root
  286. //---------------------------------------------------------
  287. void destroyRec(Node* pLocalRoot) //delete nodes in
  288. { // this subtree
  289. if(pLocalRoot != NULL)
  290. { //uses postOrder
  291. destroyRec(pLocalRoot->pLeftChild); //left subtree
  292. destroyRec(pLocalRoot->pRightChild); //right subtree
  293. delete pLocalRoot; //delete this node
  294. }
  295. }
  296. //----------------------------------------------------------
  297. }; //end class Tree
  298. ////////////////////////////////////////////////////////////
  299. int main()
  300. {
  301. int value;
  302. char choice;
  303. Node* found;
  304. Tree theTree; //create tree
  305.  
  306. theTree.insert(50, 5.0); //insert nodes
  307. theTree.insert(25, 2.5);
  308. theTree.insert(75, 7.5);
  309. theTree.insert(12, 1.2);
  310. theTree.insert(37, 3.7);
  311. theTree.insert(43, 4.3);
  312. theTree.insert(30, 3.0);
  313. theTree.insert(33, 3.3);
  314. theTree.insert(87, 8.7);
  315. theTree.insert(93, 9.3);
  316. theTree.insert(97, 9.7);
  317.  
  318.  
  319. do //interact with user
  320. { //until user types 'q'
  321. cout << "\nEnter first letter of ";
  322. cout << "show, insert, find, traverse, delete or quit: ";
  323. cin >> choice;
  324. switch(choice)
  325. {
  326. case 's': //show the tree
  327. theTree.displayTree();
  328. break;
  329. case 'i': //insert a node
  330. cout << "Enter value to insert: ";
  331. cin >> value;
  332. theTree.insert(value, value * 0.1);
  333. cout << value << " has been inserted" << endl;
  334. break;
  335. case 'f': //find a node
  336. cout << "Enter value to find: ";
  337. cin >> value;
  338. found = theTree.find(value);
  339. if(found != NULL)
  340. {
  341. cout << "Found: ";
  342. found->displayNode();
  343. cout << endl;
  344. }
  345. else
  346. cout << "Could not find " << value << endl;
  347. break;
  348. case 't': //traverse the tree
  349. cout << "Enter traverse type (1=preorder, "
  350. << "2=inorder, 3=postorder): ";
  351. cin >> value;
  352. theTree.traverse(value);
  353. break;
  354. case 'q': //quit the program
  355. theTree.destroy();
  356. cout << endl;
  357. break;
  358.  
  359. case 'd':
  360. cout << "Enter value to delete: ";
  361. cin >> value;
  362. found = theTree.find(value);
  363.  
  364. if (found != NULL)
  365. {
  366. theTree.del(value);
  367. cout << value << " has been deleted" << endl;
  368. }
  369.  
  370. else
  371. cout << "Could not find " << value << endl;
  372.  
  373. cout << endl;
  374. break;
  375.  
  376. default:
  377. cout << "Invalid entry\n";
  378. } //end switch
  379. } while(choice != 'q'); //end while
  380.  
  381. system("pause");
  382. return 0;
  383. } //end main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement