Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

br_tree.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #include "br_tree.h"
  2. br_tree::br_tree():root(NULL), h(false){}
  3. vector<string> parse_expression(string expression) { //parses expression with " " being the delimeter
  4. vector<string> results;
  5. string s;
  6. for(unsigned int i = 0; i < expression.length(); i++) {
  7. char c = expression[i];
  8. if(c != ' ') {
  9. s += c;
  10. }
  11. else {
  12. if(s != "") {
  13. results.push_back(s);
  14. s.clear();
  15. }
  16. }
  17. }
  18. if(s != "") {
  19. results.push_back(s);
  20. }
  21. return results;
  22. }
  23. bool br_tree::in_tree(string data) {
  24. bool found = false;
  25. br_node* current;
  26. br_node* parent;
  27. current = root;
  28. while(current) {
  29. if(current->data == data) {
  30. found = true;
  31. break;
  32. }
  33. else {
  34. parent = current;
  35. if(data > current->data) {
  36. current = current->right;
  37. }
  38. else if(data < current->data) {
  39. current = current->left;
  40. }
  41. }
  42. }
  43. return found;
  44. }
  45. RedBlackNodeInterface* br_tree::getRootNode() {
  46. return root;
  47. }
  48. void br_tree::add(string word) {
  49. if(in_tree(word)) {
  50. return;
  51. }
  52. if(root == NULL) {
  53. root = new br_node(word);
  54. root->color = 1;
  55. return;
  56. }
  57. br_node* current;
  58. br_node* cur_parent;
  59. current = root;
  60. while(current) {
  61. cur_parent = current;
  62. if(word > current->data) {
  63. current = current->right;
  64. }
  65. else {
  66. current = current->left;
  67. }
  68. }
  69. if(word < cur_parent->data) {
  70. cur_parent->left = new br_node(word);
  71. cur_parent->left->parent = cur_parent;
  72. br_node* child_added = cur_parent->left;
  73. if(cur_parent->color == 0 && child_added->color == 0) {
  74. balance(cur_parent->left);
  75. }
  76. return;
  77. }
  78. else {
  79. cur_parent->right = new br_node(word);
  80. cur_parent->right->parent = cur_parent;
  81. br_node* child_added = cur_parent->right;
  82. if(cur_parent->color == 0 && child_added->color == 0) {
  83. balance(cur_parent->right);
  84. }
  85. return;
  86. }
  87. }
  88. void br_tree::balance(br_node* cur) {
  89. if((cur->data == root->data) || (cur->parent->data == root->data)) {
  90. return;
  91. }
  92. if((cur->parent->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
  93. if((cur->parent->parent->left->color == 0) && (cur->parent->parent->right->color == 0)) {
  94. cur->parent->parent->left->color = 1;
  95. cur->parent->parent->right->color = 1;
  96. cur->parent->parent->color = 0;
  97. balance(cur->parent->parent);
  98. return;
  99. }
  100. }
  101. if((cur->parent->color == 0) && (cur->color == 0)) {
  102. if((cur->parent->left != NULL) && (cur->parent->parent->left != NULL)) {
  103. if((cur->parent->left->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)) {
  104. //case III left
  105. br_node* temp = cur->parent->parent;
  106. if(cur->parent->right == NULL) {
  107. temp->left = cur->parent->right;
  108. }
  109. else {
  110. temp->left = cur->parent->right;
  111. cur->parent->right->parent = temp;
  112. }
  113. cur->parent->right = temp;
  114. if(temp->parent != NULL) {
  115. if(temp->parent->right->data == temp->data) {
  116. temp->parent->right = cur->parent;
  117. }
  118. else {
  119. temp->parent->left = cur->parent;
  120. }
  121. }
  122. else {
  123. root = cur->parent;
  124. }
  125. cur->parent->parent = temp->parent;
  126. temp->parent = cur->parent;
  127. cur->parent->color = 1;
  128. temp->color = 0;
  129. balance(cur->parent);
  130. return;
  131. }
  132. }
  133. if((cur->parent->left != NULL) && (cur->parent->parent->right != NULL)) {
  134. if((cur->parent->left->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)){
  135. //case II right
  136. br_node* temp1 = cur->parent->parent;
  137. br_node* temp2 = cur->parent;
  138. if(cur->right == NULL) {
  139. temp2->left = cur->right;
  140. }
  141. else {
  142. temp2->left = cur->right;
  143. cur->right->parent = temp2;
  144. }
  145. cur->right = temp2;
  146. temp2->parent = cur;
  147. cur->parent = temp1;
  148. temp1->right = cur;
  149. balance(temp2);
  150. return;
  151. }
  152. }
  153. if((cur->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
  154. if((cur->parent->right->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)){
  155. //case II left
  156. br_node* temp1 = cur->parent->parent;
  157. br_node* temp2 = cur->parent;
  158. if(cur->left == NULL) {
  159. temp2->right = cur->left;
  160. }
  161. else {
  162. temp2->right = cur->left;
  163. cur->left->parent = temp2;
  164. }
  165. cur->left = temp2;
  166. temp2->parent = cur;
  167. cur->parent = temp1;
  168. temp1->left = cur;
  169. balance(temp2);
  170. return;
  171. }
  172. }
  173. if((cur->parent->right != NULL) && (cur->parent->parent->right != NULL)) {
  174. if((cur->parent->right->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)) {
  175. //case III right
  176. br_node* temp = cur->parent->parent;
  177. if(cur->parent->left == NULL) {
  178. temp->right = cur->parent->left;
  179. }
  180. else {
  181. temp->right = cur->parent->left;
  182. cur->parent->left->parent = temp;
  183. }
  184. cur->parent->left = temp;
  185. if(temp->parent != NULL) {
  186. if(temp->parent->right->data == temp->data) {
  187. temp->parent->right = cur->parent;
  188. }
  189. else {
  190. temp->parent->left = cur->parent;
  191. }
  192. }
  193. else {
  194. root = cur->parent;
  195. }
  196. cur->parent->parent = temp->parent;
  197. temp->parent = cur->parent;
  198. cur->parent->color = 1;
  199. temp->color = 0;
  200. balance(cur->parent);
  201. return;
  202. }
  203. }
  204. }
  205. if(root->color == 0) {
  206. root->color = 1;
  207. return;
  208. }
  209. balance(cur->parent);
  210. }
  211. void br_tree::addPhrase(string words) {
  212. vector<string> parsed_string = parse_expression(words);
  213. for(unsigned int i = 0; i < parsed_string.size(); i++) {
  214. add(parsed_string[i]);
  215. }
  216. }
  217. void br_tree::remove(string word) {
  218. if(!(in_tree(word))) {
  219. return;
  220. }
  221. if(root == NULL) {
  222. return;
  223. }
  224. br_node* current;
  225. br_node* parent;
  226. current = root;
  227. while(current) {
  228. if(current->data == word) {
  229. break;
  230. }
  231. else {
  232. parent = current;
  233. if(word > current->data) {
  234. current = current->right;
  235. }
  236. else if(word < current->data) {
  237. current = current->left;
  238. }
  239. }
  240. }
  241. if(current->color == 1) {
  242. h = true;
  243. }
  244. else {
  245. h = false;
  246. }
  247. root = del_leaf(root, word);
  248. }
  249. br_node* br_tree::del_leaf(br_node* root, string data) {
  250. br_node* temp;
  251. if(root == NULL) {
  252. return root;
  253. }
  254. else {
  255. if(data < root->data) {
  256. root->left = del_leaf(root->left, data);
  257. if(h) {
  258. cout << "2" << endl;
  259. bal_del(root->left);
  260. }
  261. }
  262. else {
  263. if(data > root->data) {
  264. root->right = del_leaf(root->right, data);
  265. if(h) {
  266. cout << "3" << endl;
  267. cout << root->data << endl;
  268. bal_del(root->right);
  269. }
  270. }
  271. else {
  272. temp = root;
  273. if(temp->right == NULL) {
  274. root = temp->left;
  275. delete temp;
  276. }
  277. else {
  278. if(temp->left == NULL) {
  279. root = temp->right;
  280. delete temp;
  281. }
  282. else {
  283. temp->left = del(temp->left, temp);
  284. if(h) {
  285. cout << "4" << endl;
  286. if(h) {
  287. bal_del(temp);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. return root;
  296. }
  297. void br_tree::bal_del(br_node* x) {
  298. //cout << "bal_del(" << x->data << ")" << endl;
  299. if(x == NULL) {
  300. return;
  301. }
  302. while(x != root && x->color == 1) {
  303. if(x == x->parent->left) {
  304. br_node* w = x->parent->right;
  305. if(w == NULL) {
  306. return;
  307. }
  308. if(w->right == NULL || w->left == NULL) {
  309. return;
  310. }
  311. if(w != NULL && w->right != NULL && w->left != NULL) {
  312. if(w->color == 0) {
  313. cout << "case 1 first" << endl;
  314. w->color = 1; //Case 1
  315. x->parent->color = 0; //Case 1
  316. left_rot(x->parent); //Case 1
  317. w = x->parent->right; //Case 1
  318. }
  319. else if(w->left->color == 1 && w->right->color == 1) {
  320. cout << "case 2 first" << endl;
  321. w->color = 1; //Case 2
  322. x = x->parent; //Case 2
  323. }
  324. else {
  325. if(w->right->color == 1) {
  326. cout << "case 3 first" << endl;
  327. w->left->color = 1; //Case 3
  328. w->color = 0; //Case 3
  329. right_rot(w); //Case 3
  330. w = x->parent->right; //Case 3
  331. }
  332. cout << "case 4 first" << endl;
  333. w->color = x->parent->color; //Case 4
  334. x->parent->color = 1; //Case 4
  335. w->right->color = 1; //Case 4
  336. left_rot(x->parent);
  337. x = root;
  338. }
  339. }
  340. }
  341. else {
  342. br_node* w = x->parent->left;
  343. if(w == NULL) {
  344. return;
  345. }
  346. if(w->right == NULL || w->left == NULL) {
  347. return;
  348. }
  349. if(w->color == 0) {
  350. cout << "case 1 second" << endl;
  351. w->color = 1; //Case 1
  352. x->parent->color = 0; //Case 1
  353. right_rot(x->parent); //Case 1
  354. w = x->parent->left; //Case 1
  355. }
  356. else if(w->right->color == 1 && w->left->color == 1) {
  357. cout << "case 2 second" << endl;
  358. w->color = 0; //Case 2
  359. x = x->parent; //Case 2
  360. }
  361. else {
  362. if(w->left->color == 1) {
  363. cout << "case 3 second" << endl;
  364. w->right->color = 1; //Case 3
  365. w->color = 0; //Case 3
  366. left_rot(w); //Case 3
  367. w = x->parent->left; //Case 3
  368. }
  369. cout << "case 4 second" << endl;
  370. w->color = x->parent->color; //Case 4
  371. x->parent->color = 1; //Case 4
  372. w->left->color = 1; //Case 4
  373. right_rot(x->parent);
  374. x = root;
  375. }
  376. }
  377. }
  378. x->color = 1;
  379. }
  380. br_node* br_tree::del(br_node* succ, br_node* n) {
  381. br_node* temp = succ;
  382. if(succ->right != NULL) {
  383. succ->right = del(succ->right, n);
  384. }
  385. else {
  386. br_node* current;
  387. br_node* parent;
  388. current = root;
  389. while(current) {
  390. if(current->data == succ->data) {
  391. break;
  392. }
  393. else {
  394. parent = current;
  395. if(succ->data > current->data) {
  396. current = current->right;
  397. }
  398. else if(succ->data < current->data) {
  399. current = current->left;
  400. }
  401. }
  402. }
  403. if(root->left->data == succ->data) {
  404. root->data = succ->data;
  405. n->right = root->right;
  406. succ = succ->left;
  407. delete temp;
  408. return succ;
  409. }
  410. else if(root->right->data == succ->data) {
  411. root->data = succ->data;
  412. n->right = root->right;
  413. succ = succ->right;
  414. delete temp;
  415. return succ;
  416. }
  417. temp = succ;
  418. n->data = succ->data;
  419. succ = succ->left;
  420. delete temp;
  421. }
  422. return succ;
  423. }
  424. void br_tree::right_rot(br_node* x) {
  425. br_node* y = x->left;
  426. x->left = y->right;
  427. if(y->right != NULL) {
  428. y->right->parent = x;
  429. }
  430. y->parent = x->parent;
  431. if(x->parent == NULL) {
  432. root = y;
  433. }
  434. else if(x == x->parent->right) {
  435. x->parent->right = y;
  436. }
  437. else {
  438. x->parent->left = y;
  439. }
  440. y->right = x;
  441. x->parent = y;
  442. }
  443. void br_tree::left_rot(br_node* x) {
  444. br_node* y = x->right;
  445. x->right = y->left;
  446. if(y->left != NULL) {
  447. y->left->parent = x;
  448. }
  449. y->parent = x->parent;
  450. if(x->parent == NULL) {
  451. root = y;
  452. }
  453. else if(x == x->parent->left) {
  454. x->parent->left = y;
  455. }
  456. else {
  457. x->parent->right = y;
  458. }
  459. y->left = x;
  460. x->parent = y;
  461. }
  462. void br_tree::printer(br_node* node, int a) {
  463. a++;
  464. if(node == NULL) {
  465. return;
  466. }
  467. printer(node->right, a);
  468. for(int i = 0; i <= a; i++) {
  469. cout << "\t";
  470. }
  471. printf("%s %d", node->data.c_str(), node->color);
  472. cout << endl << endl;
  473. printer(node->left, a);
  474. }
  475. string br_tree::printTree() {
  476. string tree;
  477. if(root == NULL) {
  478. return "";
  479. }
  480. cout << root->data << endl;
  481. cout << endl;
  482. printer(root, 0);
  483. cout << endl;
  484. return tree;
  485. }