Guest User

Untitled

a guest
Jul 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1.  
  2. /*
  3. * File: BinarySearchTree.cpp
  4. * Prj: Dictonary
  5. *
  6. * Created by Will Mernagh on April 21.
  7. * Copyright 2007. All rights reserved.
  8. *
  9. */
  10. #include <iostream>
  11. #include "nodes.h"
  12. #include "BinarySearchTree.h"
  13. using namespace std;
  14.  
  15. /*
  16. * Constructor
  17. */
  18. BST::BST()
  19. {
  20. setRoot(NULL);
  21. }
  22.  
  23. /*
  24. * Returns the root
  25. */
  26. TreeNode * BST::getRoot(){
  27. return root;
  28. }
  29.  
  30. /*
  31. * Set the root
  32. */
  33. void BST::setRoot(TreeNode *rootin){
  34. root = rootin;
  35. }
  36.  
  37. /*
  38. * Inserts the node into the tree
  39. *
  40. * @param TreeNode The Node to add to the tree
  41. * @return False if already in the tree
  42. */
  43. bool BST::insert(TreeNode *tnode)
  44. {
  45. TreeNode *search, *position;
  46. search = getRoot();
  47.  
  48. /* If empty tree */
  49. if(search == NULL){
  50.  
  51. setRoot(tnode);
  52. getRoot()->setLeft(NULL);
  53. getRoot()->setRight(NULL);
  54. getRoot()->setParent(NULL);
  55. return true;
  56.  
  57. }
  58.  
  59. /* if word already in the tree */
  60. if(find(tnode->word)){
  61. return false;
  62. }
  63.  
  64. position = search;
  65.  
  66. while(position != NULL){
  67.  
  68. search = position;
  69.  
  70. if(tnode->word > search->word){
  71. position = search->getRight();
  72. }else if (tnode->word < search->word){
  73. position = search->getLeft();
  74. }else{
  75. return false; //match
  76. }
  77. }
  78.  
  79. if(tnode->word > search->word){
  80.  
  81. search->setRight(tnode);
  82. tnode->setParent(search);
  83. tnode->setLeft(NULL);
  84. tnode->setRight(NULL);
  85.  
  86.  
  87. }else if (tnode->word < search->word){
  88.  
  89. search->setLeft(tnode);
  90. tnode->setParent(search);
  91. tnode->setLeft(NULL);
  92. tnode->setRight(NULL);
  93.  
  94. }
  95. return true;
  96.  
  97. }
  98.  
  99. /*
  100. * Inserts the string into the tree.
  101. *
  102. * @param String The string to add to the tree
  103. * @return False if already in the tree
  104. */
  105. bool BST::insert(string word)
  106. {
  107. TreeNode *tnode = new TreeNode;
  108. tnode->word = word;
  109. if(BST::insert(tnode)){
  110. return true;
  111. }else{
  112. delete tnode;
  113. return false;
  114. }
  115. }
  116.  
  117. /*
  118. * Search for a string and return true if in tree
  119. *
  120. * @param String The string to search for
  121. * @return True if string in the tree
  122. */
  123. bool BST::find(string word)
  124. {
  125. TreeNode *search;
  126.  
  127. search = getRoot();
  128.  
  129. while(search != NULL){
  130.  
  131. if(word > search->word){
  132.  
  133. search = search->getRight();
  134.  
  135. }else if (word < search->word){
  136.  
  137. search = search->getLeft();
  138.  
  139. }else{
  140.  
  141. return true; //match
  142.  
  143. }
  144. }
  145.  
  146. return false;
  147. }
  148.  
  149. /*
  150. * Search for a node and return true if in tree
  151. *
  152. * @param String The string encapsulated in node to search for
  153. * @return True if string in the tree
  154. */
  155. TreeNode * BST::findNode(string word)
  156. {
  157. TreeNode *search;
  158.  
  159. search = getRoot();
  160.  
  161. while(search != NULL){
  162.  
  163. if(word > search->word){
  164.  
  165. search = search->getRight();
  166.  
  167. }else if (word < search->word){
  168.  
  169. search = search->getLeft();
  170.  
  171. }else{
  172.  
  173. return search; //match
  174.  
  175. }
  176. }
  177.  
  178. return NULL;
  179. }
  180.  
  181. /*
  182. * Search for the successor of a string and return it if in tree
  183. *
  184. * @param String The string whose successor to search for
  185. * @return TreeNode if string in the tree else return null
  186. */
  187. TreeNode * BST::successor(string word)
  188. {
  189. TreeNode *wordNode;
  190. TreeNode *search;
  191.  
  192. wordNode = findNode(word);
  193.  
  194. if(wordNode == NULL){
  195. return NULL;
  196. }
  197.  
  198. if((wordNode->getRight()==NULL)&&(wordNode->getParent()==NULL)){
  199.  
  200. return NULL; //no successor
  201.  
  202. }else if(wordNode->getRight() != NULL) {//search down
  203.  
  204. search = wordNode->getRight();
  205.  
  206. while(search->getLeft() != NULL){
  207.  
  208. search = search->getLeft();
  209.  
  210. }
  211.  
  212. return search;
  213.  
  214. }else if(wordNode->getParent()->word > wordNode->word){
  215. return wordNode->getParent();
  216. }else{
  217.  
  218. //Search up
  219.  
  220. search = wordNode;
  221.  
  222. while((search->getParent() != NULL) &&
  223. (search->getParent()->word < wordNode->word)){
  224.  
  225. search = search->getParent();
  226.  
  227. }
  228.  
  229. return search->getParent();
  230.  
  231.  
  232. }
  233. return NULL;
  234. }
  235.  
  236. /*
  237. * Display the items in a tree in order
  238. *
  239. * @param TreeNode The next node to recurse on
  240. */
  241. void BST::recurseDisplay(TreeNode *node)
  242. {
  243. if(node != NULL){
  244.  
  245. recurseDisplay(node->getLeft());
  246. cout<<node->word<<"";
  247.  
  248. if(node->getParent() != NULL){
  249.  
  250. cout << "(" << node->getParent()->word<<")\n";
  251.  
  252. }else{
  253.  
  254. cout<<"\n";
  255.  
  256. }
  257.  
  258. recurseDisplay(node->getRight());
  259.  
  260. }
  261. }
  262.  
  263. /*
  264. * Display the items in a tree in order
  265. *
  266. */
  267. void BST::display()
  268. {
  269. recurseDisplay(getRoot());
  270. }
  271.  
  272. /* Delete a word from the tree
  273. *
  274. * @param string The string to delete
  275. * @return bool False if it does not exist in tree
  276. */
  277. bool BST::del(string word)
  278. {
  279. TreeNode *todel, *scsr;
  280. string scsrWord;
  281. todel = findNode(word);
  282.  
  283.  
  284. /* does not exist */
  285. if(todel == NULL){
  286. return false;
  287. }
  288.  
  289. if(todel == getRoot()){
  290.  
  291. scsr = successor(word);
  292.  
  293. if((scsr == NULL)&&(todel->getLeft() == NULL)){
  294.  
  295. delete(todel);
  296. setRoot(NULL);
  297. return true;
  298.  
  299. }else if(scsr == NULL){
  300.  
  301. setRoot(todel->getLeft());
  302. todel->getLeft()->setParent(NULL);
  303. delete(todel);
  304. return true;
  305.  
  306. }else{
  307.  
  308. scsrWord = scsr->word;
  309. del(scsrWord);
  310. todel->word = scsrWord;
  311. return true;
  312.  
  313. }
  314. }else if((todel->getLeft() == NULL)&&(todel->getRight() == NULL)){
  315.  
  316. if((todel->getParent()->getLeft() != NULL) &&
  317. (todel->getParent()->getLeft()->word == todel->word)){
  318.  
  319. todel->getParent()->setLeft(NULL);
  320.  
  321. }else{
  322.  
  323. todel->getParent()->setRight(NULL);
  324.  
  325. }
  326.  
  327. delete todel;
  328. return true;
  329.  
  330. }else if(todel->getLeft() == NULL){
  331.  
  332. if((todel->getParent()->getLeft() != NULL) &&
  333. (todel->getParent()->getLeft()->word == todel->word)){
  334.  
  335. todel->getParent()->setLeft(todel->getRight());
  336. todel->getRight()->setParent(todel->getParent());
  337.  
  338. }else{
  339.  
  340. todel->getParent()->setRight(todel->getRight());
  341. todel->getRight()->setParent(todel->getParent());
  342.  
  343. }
  344.  
  345. delete todel;
  346. return true;
  347.  
  348. }else if(todel->getRight() == NULL){
  349.  
  350. if((todel->getParent()->getLeft() != NULL) &&
  351. (todel->getParent()->getLeft()->word == todel->word)){
  352.  
  353. todel->getParent()->setLeft(todel->getLeft());
  354. todel->getLeft()->setParent(todel->getParent());
  355.  
  356. }else{
  357.  
  358. todel->getParent()->setRight(todel->getLeft());
  359. todel->getLeft()->setParent(todel->getParent());
  360. }
  361.  
  362. delete todel;
  363. return true;
  364.  
  365. }else{
  366.  
  367. scsr = successor(word);
  368. scsrWord = scsr->word;
  369. del(scsr->word);
  370. todel->word = scsrWord;
  371.  
  372. return true;
  373. }
  374.  
  375. return false;
  376. }
  377.  
  378. /* Destructor
  379. */
  380. BST::~BST(){
  381. while(getRoot() != NULL){
  382. del(getRoot()->word);
  383. }
  384. }
Add Comment
Please, Sign In to add comment