C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
tree.c
Go to the documentation of this file.
1 
5 #include "tree.h"
6 #include "stack.h"
7 #include "queue.h"
8 
11 int tree_height_bst (t_gen n);
12 
18 void tree_inorder(t_gen);
19 void tree_preorder(t_gen);
20 void tree_postorder(t_gen);
21 void print_tree(t_gen);
22 int tree_node_count(t_gen n);
23 
26 int tree_height_avl(t_gen n);
27 void destroy_tree(t_gen);
28 
31 
34 
37 
45 t_gen create_tree(char *name, e_treetype ttype, t_dparams *prm)
46 {
47  t_tree *t = get_mem(1, sizeof(t_tree));
48 
49  // Initailze tree Params
50  t->name = name;
51  t->type = ttype;
52  t->count = 0;
53  t->root = NULL;
54 
55  // Initailze tree routines
56  t->insert = tree_insert[ttype];
57  t->del = tree_del[ttype];
58  t->find = tree_find_node;
61  t->min = tree_get_min;
62  t->max = tree_get_max;
63  t->print = print_tree;
64  t->inorder = tree_inorder;
67  t->height = tree_height[ttype];
69  t->destroy = destroy_tree;
70 
71  // Initailze datatype based operations req for prop working of tree
72  t->cmpr = prm->cmpr;
73  t->swap = prm->swap;
74  t->print_data = prm->print_data;
75  t->free = prm->free;
76 
77  return (t_gen)t;
78 }
79 
80 
87 {
88 
89  return ((t_tree*)d)->count;
90 }
91 
99 {
100  t_tree *t = (t_tree*)d;
101  t_tree_node *new;
102  t_tree_node *parent = NULL, *cur;
103  e_cmpr res;
104 
105  // key already present
106  if (t->find(t, data) != NULL) {
107  LOG_WARN("TREES", "%s: Key not present\n");
108  return;
109  }
110 
111  t->count++;
112  // Create Node and add data
113  new = get_mem(1, sizeof(t_tree_node));
114  new->key = data;
115  new->lchild = new->rchild = NULL;
116 
117  // tree is empty
118  if (t->root == NULL) {
119  t->root = new;
120  return;
121  }
122 
123  cur = t->root;
124  // get position in tree to insert node
125  while (cur != NULL) {
126  parent = cur;
127  // if new node < cur node
128  // new node to be inserted in left subtree
129  // else insert in right subtree
130  res = t->cmpr(new->key, cur->key);
131  if (res == eLESS) {
132  cur = cur->lchild;
133  } else {
134  cur = cur->rchild;
135  }
136  }
137 
138  // If the new key is less then the leaf node key
139  // Assign the new node to be its left child
140  // else Assign the new node to be its right child
141  res = t->cmpr(new->key, parent->key);
142  if (res == eLESS) {
143  parent->lchild = new;
144  } else {
145  parent->rchild = new;
146  }
147 }
148 
156 {
157  t_tree *t = (t_tree*)d;
158  t_tree_node *cur;
159  e_cmpr res;
160 
161  cur = t->root;
162  while (cur != NULL) {
163  // if new node < cur node
164  // node to be searched in left subtree
165  // else search in right subtree
166  res = t->cmpr(data, cur->key);
167 
168  if (res == eEQUAL) {
169  break;
170  } else if (res == eLESS) {
171  cur = cur->lchild;
172  } else {
173  cur = cur->rchild;
174  }
175  }
176 
177  // return NULL if not found else node
178  return cur;
179 }
180 
187 {
188  t_tree_node *cur = (t_tree_node*)root;
189 
190  // tree is empty
191  if (cur == NULL) {
192  return NULL;
193  }
194 
195  // left most node contains min
196  while (cur->lchild != NULL) {
197  cur = cur->lchild;
198  }
199 
200  return cur;
201 }
202 
209 {
210  t_tree_node *cur = (t_tree_node*)root;
211 
212  // tree is empty
213  if (cur == NULL) {
214  return NULL;
215  }
216 
217  // right most node contains min
218  while (cur->rchild != NULL) {
219  cur = cur->rchild;
220  }
221 
222  return cur;
223 }
224 
231 {
232  t_tree_node *cur = (t_tree_node*)root;
233 
234  // tree is empty
235  if (cur == NULL) {
236  return NULL;
237  }
238 
239  // recursively traverse to left most node
240  if (cur->lchild == NULL) {
241  return cur;
242  } else {
243  return tree_get_min_rcrs(cur->lchild);
244  }
245 }
246 
253 {
254  t_tree_node *cur = (t_tree_node*)root;
255 
256  // tree is empty
257  if (cur == NULL) {
258  return NULL;
259  }
260 
261  // recursively traverse to right most node
262  if (cur->rchild == NULL) {
263  return cur;
264  } else {
265  return tree_get_max_rcrs(cur->rchild);
266  }
267 }
268 
276 {
277  t_tree *t = (t_tree*)d;
278  t_tree_node *pred = NULL, *cur = t->root;
279  e_cmpr res;
280 
281  // Empty tree
282  if (cur == NULL) {
283  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
284  return pred;
285  }
286 
287  while (cur) {
288  res = t->cmpr(data, cur->key);
289 
290  // if key is less than cur, visit the left subtree
291  if (res == eLESS) {
292  cur = cur->lchild;
293  }
294  // if key is less than cur, visit the right subtree
295  else if (res == eGREAT) {
296  // set the pred as cur before visiting right subtree
297  pred = cur;
298  cur = cur->rchild;
299  }
300  // Node found and pred is max of left subtree (if any)
301  // else pred is root of last right subtreee traversal
302  else {
303  if (cur->lchild != NULL) {
304  pred = t->max(cur->lchild);
305  }
306  break;
307  }
308  }
309 
310  // key not present
311  if (cur == NULL || pred == NULL) {
312  LOG_WARN("TREES", "%s: Key not present\n",t->name);
313  pred = NULL;
314  }
315 
316  return pred;
317 }
318 
326 {
327  t_tree *t = (t_tree*)d;
328  t_tree_node *succ = NULL, *cur = t->root;
329  e_cmpr res;
330 
331  // Empty tree
332  if (cur == NULL) {
333  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
334  return succ;
335  }
336 
337  while (cur) {
338  res = t->cmpr(data, cur->key);
339 
340  // if key is less than cur, visit the left subtree
341  // set the succ as cur before visiting left subtree
342  if (res == eLESS) {
343  succ = cur;
344  cur = cur->lchild;
345  }
346  // if key is less than cur, visit the right subtree
347  else if (res == eGREAT) {
348  cur = cur->rchild;
349  }
350  // Node found and succ is min of right subtree (if any)
351  // else succ is root of last left subtreee traversal
352  else {
353  if (cur->rchild != NULL) {
354  succ = t->min(cur->rchild);
355  }
356  break;
357  }
358  }
359 
360  // key not present
361  if (cur == NULL || succ == NULL) {
362  LOG_WARN("TREES", "%s: Key not present\n",t->name);
363  succ = NULL;
364  }
365 
366  return succ;
367 }
368 
376 {
377  t_tree *t = (t_tree*)d;
378  t_tree_node *tmp, *prv, *cur = t->root;
379  e_cmpr res;
380  t_gen ret = NULL;
381 
382  // Empty tree
383  if (cur == NULL) {
384  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
385  return ret;
386  }
387 
388  t->count++;
389  tmp = prv = NULL;
390  // check if node present and get parent
391  while (cur) {
392  res = t->cmpr(data, cur->key);
393  if (res == eEQUAL) {
394  break;
395  }
396 
397  prv = cur;
398  if (res == eLESS) {
399  cur = cur->lchild;
400  } else {
401  cur = cur->rchild;
402  }
403  }
404 
405  // key not present
406  if (cur == NULL) {
407  LOG_WARN("TREES", "%s: Key not present\n",t->name);
408  return ret;
409  }
410 
411  // store the key to be returned
412  ret = cur->key;
413 
414  // node to be has just one child
415  if (cur->lchild == NULL || cur->rchild == NULL) {
416  // get the non-null child to be replaced with deleted node
417  tmp = (cur->lchild != NULL)? cur->lchild : cur->rchild;
418 
419  // root is deleted update root
420  if (prv == NULL) {
421  t->root = tmp;
422  }
423  // get where the new node is to updated ,i.e. l or r child
424  else if (cur == prv->lchild) {
425  prv->lchild = tmp;
426  }
427  else {
428  prv->rchild = tmp;
429  }
430  }
431 
432  else {
433  prv = NULL;
434  // compute successor and get parent of it
435  tmp = cur->rchild;
436  while (tmp->lchild) {
437  prv = tmp;
438  tmp = tmp->lchild;
439  }
440 
441  // Swap node to be deleted with succ
442  if (prv != NULL) {
443  prv->lchild = tmp->rchild;
444  }
445  else {
446  cur->rchild = tmp->rchild;
447  }
448 
449  cur->key = tmp->key;
450  cur = tmp;
451  }
452  // Delete node
453  cur->lchild = cur->rchild = NULL;
454  cur->key = NULL;
455  free_mem(cur);
456 
457  return ret;
458 }
459 
466 {
467  t_tree_node *cur = (t_tree_node*)n;
468  int height = 0,size;
469  t_queue *q;
470  t_dparams dp;
471 
472  if (cur == NULL) {
473  return height;
474  }
475 
476  // TODO: Queue count how to do??
477  init_data_params(&dp, eINT32);
478  q = create_queue("Qdel_tree", 40, eLL_QUEUE_CIRC, &dp);
479 
480  // enqueue the root node
481  q->enq(q, cur);
482  while (q->empty(q) != true) {
483  // nodes in current level
484  size = q->len(q);
485  // loop till queue is empty
486  while (size--) {
487  // get the node at cur level
488  cur = q->deq(q);
489 
490  // enqueue all the children at cur lvl
491  if (cur->lchild != NULL) {
492  q->enq(q, cur->lchild);
493  }
494 
495  if (cur->rchild != NULL) {
496  q->enq(q, cur->rchild);
497  }
498  }
499 
500  // Increment height
501  height++;
502  }
503 
504  q->destroy(q);
505 
506  return height;
507 
508 }
509 
516 {
517  t_tree *t = (t_tree*)d;
518  t_tree_node *cur = t->root;
519  t_queue *q;
520  t_dparams dp;
521 
522  // Empty tree
523  if (cur == NULL) {
524  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
525  } else {
526  init_data_params(&dp, eINT32);
527  q = create_queue("Qdel_tree", t->count,
528  eLL_QUEUE_CIRC, &dp);
529  // enqueue the root node
530  q->enq(q, cur);
531 
532  // loop till queue is empty
533  while (q->empty(q) != true) {
534  // delete nodes in the queue one by one after pushing
535  // their non-empty left and right child to the queue
536  cur = q->deq(q);
537 
538  if (cur->lchild != NULL) {
539  q->enq(q, cur->lchild);
540  }
541 
542  if (cur->rchild != NULL) {
543  q->enq(q, cur->rchild);
544  }
545 
546  cur->lchild = cur->rchild = NULL;
547  t->free(cur->key, __FILE__, __LINE__);
548  free_mem(cur);
549  }
550  q->destroy(q);
551  }
552 
553  free_mem(t);
554 }
555 
562 {
563  t_tree *t = (t_tree*)d;
564  t_tree_node *tmp, *cur = t->root;
565  t_dparams dp;
566  t_stack *s;
567 
568  // Empty tree
569  if (cur == NULL) {
570  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
571  return;
572  }
573  printf("%s: %d nodes postorder traversal\n", t->name, t->count);
574 
575  init_data_params(&dp, eINT32);
576  s = create_stack("Stk_postorder", t->count, eLL_STACK, &dp);
577 
578  do {
579  // Move to left most node
580  while (cur) {
581  // push root rchild and root to stack
582  if (cur->rchild != NULL) {
583  s->push(s, cur->rchild);
584  }
585  s->push(s, cur);
586  cur = cur->lchild;
587  }
588  // pop and print the node
589  cur = s->pop(s);
590 
591  // If popped item has right child and the right child is not
592  // processed, then make sure right child processed before root
593  tmp = (s->empty(s) != true)? s->peek(s, 0): NULL;
594 
595  if (cur->rchild != NULL && tmp == cur->rchild) {
596  // remove right child from stack
597  s->pop(s);
598  // push root back to stack
599  s->push(s, cur);
600  // update root to rchild to process it
601  cur = cur->rchild;
602  }
603  else {
604  // print data and update root as NULL
605  t->print_data(cur->key);
606  printf(" ");
607  cur = NULL;
608  }
609  } while (s->empty(s) != true);
610  printf("\n");
611 
612  s->destroy(s);
613 
614 }
615 
622 {
623  t_tree *t = (t_tree*)d;
624  t_tree_node *cur = t->root;
625  t_stack *s;
626  t_dparams dp;
627 
628  // Empty tree
629  if (cur == NULL) {
630  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
631  return;
632  }
633  printf("%s: %d nodes preorder traversal\n", t->name, t->count);
634 
635  init_data_params(&dp, eINT32);
636  s = create_stack("Stk_inorder", t->count, eLL_STACK, &dp);
637 
638  // push root node
639  s->push(s, cur);
640  while (s->empty(s) != true) {
641  // pop and print the node
642  cur = s->pop(s);
643  t->print_data(cur->key);
644  printf(" ");
645 
646  // push right and left child of node
647  if (cur->lchild != NULL) {
648  s->push(s, cur->lchild);
649  }
650  if (cur->rchild != NULL) {
651  s->push(s, cur->rchild);
652  }
653  }
654  printf("\n");
655 
656  s->destroy(s);
657 }
658 
665 {
666  t_tree *t = (t_tree*)d;
667  t_tree_node *cur = t->root;
668  t_stack *s;
669  t_dparams dp;
670 
671  // Empty tree
672  if (cur == NULL) {
673  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
674  return;
675  }
676  printf("%s: %d nodes inorder traversal\n", t->name, t->count);
677 
678  init_data_params(&dp, eINT32);
679  s = create_stack("Stk_inorder", t->count, eLL_STACK, &dp);
680 
681  while (s->empty(s) != true || cur != NULL) {
682  // Visit the left most child
683  while (cur != NULL) {
684  s->push(s, cur);
685  cur = cur->lchild;
686  }
687 
688  // pop and print the node
689  cur = s->pop(s);
690  t->print_data(cur->key);
691  printf(" ");
692 
693  // traverse the right subtree
694  cur = cur->rchild;
695  }
696  printf("\n");
697 
698  s->destroy(s);
699 }
700 
707 {
708  t_tree *t = (t_tree*)d;
709  t_tree_node *cur = t->root;
710  t_queue *q;
711  t_dparams dp;
712  int level = 0,size;
713 
714  // Empty tree
715  if (cur == NULL) {
716  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
717  return;
718  }
719  printf("%s: %d nodes lvl based print \n", t->name,t->count);
720 
721  init_data_params(&dp, eINT32);
722  q = create_queue("Qdel_tree", t->count, eLL_QUEUE_CIRC, &dp);
723 
724  // enqueue the root node
725  q->enq(q, cur);
726  while (q->empty(q) != true) {
727  printf(" %d :",level);
728  // nodes in current level
729  size = q->len(q);
730  // loop till queue is empty
731  while (size--) {
732  // get the node at cur level
733  cur = q->deq(q);
734 
735  printf("[");
736  t->print_data(cur->key);
737  printf(":");
738  // enqueue all the children at cur lvl
739  if (cur->lchild != NULL) {
740  q->enq(q, cur->lchild);
741  printf(" ");
742  t->print_data(cur->lchild->key);
743  }
744 
745  if (cur->rchild != NULL) {
746  q->enq(q, cur->rchild);
747  printf(" ");
748  t->print_data(cur->rchild->key);
749  }
750  // print all nodes at cur lvl
751  printf("] ");
752  }
753  printf("\n");
754  // Increment level
755  level++;
756  }
757 
758  q->destroy(q);
759 
760 }
761 
768 {
769  t_tree_node *root = (t_tree_node*)n;
770  int lh, rh;
771  int height;
772 
773  lh = (root->lchild == NULL)? -1: root->lchild->height;
774  rh = (root->rchild == NULL)? -1: root->rchild->height;
775 
776  height = (lh >rh)? lh: rh;
777 
778  return 1 + height;
779 
780 }
781 
788 {
789  t_tree_node *root = (t_tree_node*)n;
790  int lh, rh;
791 
792  lh = (root->lchild == NULL)? 0: root->lchild->height;
793  rh = (root->rchild == NULL)? 0: root->rchild->height;
794 
795  return (lh - rh);
796 }
797 
804 {
805  t_tree_node *root = (t_tree_node*)n;
806  t_tree_node *TLL, *TLR, *TR;
807  t_gen x, y;
808 
809  // update all ptrs;
810  x = root->key;
811  y = root->lchild->key;
812  TLL = root->lchild->lchild;
813  TLR = root->lchild->rchild;
814  TR = root->rchild;
815 
816  // rotate right
817  root->key = y;
818  root->rchild = root->lchild;
819  root->rchild->key = x;
820  root->lchild = TLL;
821  root->rchild->lchild = TLR;
822  root->rchild->rchild = TR;
823 }
824 
825 
832 {
833  t_tree_node *root = (t_tree_node*)n;
834  t_tree_node *TLL, *TLRL, *TLRR;
835  t_gen y, z;
836 
837  // update all ptrs;
838  y = root->key;
839  z = root->rchild->key;
840  TLL = root->lchild;
841  TLRL = root->rchild->lchild;
842  TLRR = root->rchild->rchild;
843 
844  // rotate left
845  root->key = z;
846  root->lchild = root->rchild;
847  root->lchild->key = y;
848  root->lchild->lchild = TLL;
849  root->lchild->rchild = TLRL;
850  root->rchild = TLRR;
851 }
852 
859 {
860  t_tree_node *root = (t_tree_node*)n;
861 
862  if (tree_slope(root) == 2) {
863  if (tree_slope(root->lchild) == -1) {
864  tree_rotate_left(root->lchild);
865  }
866  tree_rotate_right(root);
867  }
868 
869  if (tree_slope(root) == -2) {
870  if (tree_slope(root->rchild) == 1) {
871  tree_rotate_right(root->rchild);
872  }
873  tree_rotate_left(root);
874  }
875 
876 }
877 
885 {
886  t_tree *t = (t_tree*)d;
887  t_tree_node *new;
888  t_tree_node *parent = NULL, *cur;
889  e_cmpr res;
890  t_stack *s;
891  t_dparams dp;
892 
893  // key already present
894  if (t->find(t, data) != NULL) {
895  LOG_WARN("TREES", "%s: Key not present\n");
896  return;
897  }
898 
899  t->count++;
900  // Create Node and add data
901  new = get_mem(1, sizeof(t_tree_node));
902  new->key = data;
903  new->lchild = new->rchild = NULL;
904  new->height = 0;
905 
906  // tree is empty
907  if (t->root == NULL) {
908  t->root = new;
909  return;
910  }
911 
912  // stack to be used to find rebalance and update height
913  init_data_params(&dp, eINT32);
914  s = create_stack("Stk_inorder", t->count, eLL_STACK, &dp);
915 
916  cur = t->root;
917  // get position in tree to insert node
918  while (cur != NULL) {
919  parent = cur;
920  // if new node < cur node
921  // new node to be inserted in left subtree
922  // else insert in right subtree
923  res = t->cmpr(new->key, cur->key);
924  if (res == eLESS) {
925  cur = cur->lchild;
926  } else {
927  cur = cur->rchild;
928  }
929 
930  // push parent to stack
931  s->push(s, parent);
932  }
933 
934  // If the new key is less then the leaf node key
935  // Assign the new node to be its left child
936  // else Assign the new node to be its right child
937  res = t->cmpr(new->key, parent->key);
938  if (res == eLESS) {
939  parent->lchild = new;
940  } else {
941  parent->rchild = new;
942  }
943 
944  // check for rebalance and update height
945  while (s->empty(s) != true) {
946  cur = s->pop(s);
947  // rebalance the paraent
948  tree_rebalance(cur);
949  cur->height = tree_height_avl(cur);
950  }
951 
952  s->destroy(s);
953 }
954 
955 
963 {
964  t_tree *t = (t_tree*)d;
965  t_tree_node *tmp, *prv, *cur = t->root;
966  t_gen ret = NULL;
967  e_cmpr res;
968  t_dparams dp;
969  t_stack *s;
970 
971  // Empty tree
972  if (cur == NULL) {
973  LOG_WARN("TREES", "%s: TREE Empty\n",t->name);
974  return ret;
975  }
976 
977  // stack to be used to find rebalance and update height
978  init_data_params(&dp, eINT32);
979  s = create_stack("Stk_inorder", t->count, eLL_STACK, &dp);
980 
981  t->count++;
982  tmp = prv = NULL;
983  // check if node present and get parent
984  while (cur) {
985  res = t->cmpr(data, cur->key);
986  if (res == eEQUAL) {
987  break;
988  }
989 
990  prv = cur;
991  if (res == eLESS) {
992  cur = cur->lchild;
993  } else {
994  cur = cur->rchild;
995  }
996  // push parent to stack
997  s->push(s, prv);
998  }
999 
1000  // key not present
1001  if (cur == NULL) {
1002  LOG_WARN("TREES", "%s: Key not present\n",t->name);
1003  s->destroy(s);
1004  return ret;
1005  }
1006 
1007  // store the key to be returned
1008  ret = cur->key;
1009 
1010  // node to be has just one child
1011  if (cur->lchild == NULL || cur->rchild == NULL) {
1012  // get the non-null child to be replaced with deleted node
1013  tmp = (cur->lchild != NULL)? cur->lchild : cur->rchild;
1014 
1015  // root is deleted update root
1016  if (prv == NULL) {
1017  t->root = tmp;
1018  }
1019  // get where the new node is to updated ,i.e. l or r child
1020  else if (cur == prv->lchild) {
1021  prv->lchild = tmp;
1022  }
1023  else {
1024  prv->rchild = tmp;
1025  }
1026  }
1027 
1028  else {
1029  prv = NULL;
1030  // compute successor and get parent of it
1031  tmp = cur->rchild;
1032  while (tmp->lchild) {
1033  prv = tmp;
1034  tmp = tmp->lchild;
1035  }
1036 
1037  // Swap node to be deleted with succ
1038  if (prv != NULL) {
1039  prv->lchild = tmp->rchild;
1040  }
1041  else {
1042  cur->rchild = tmp->rchild;
1043  }
1044 
1045  cur->key = tmp->key;
1046  cur = tmp;
1047  }
1048  // Delete node
1049  cur->lchild = cur->rchild = NULL;
1050  cur->key = NULL;
1051  free_mem(cur);
1052 
1053  // check for rebalance and update height
1054  while (s->empty(s) != true) {
1055  cur = s->pop(s);
1056  // rebalance the paraent
1057  tree_rebalance(cur);
1058  cur->height = tree_height_avl(cur);
1059  }
1060 
1061  s->destroy(s);
1062  return ret;
1063 }
void init_data_params(t_dparams *, e_data_types)
Called to initalize data params for default data types Else for custom data types the data params str...
Definition: common.c:15
#define LOG_WARN(mod, fmt, args...)
Definition: logger.h:18
#define free_mem(mem_addr)
Definition: memory_manager.h:9
#define get_mem(nmemb, size)
Definition: memory_manager.h:8
t_gen create_queue(char *name, int max_size, e_queuetype qtype, t_dparams *prm)
Destroy queue instance
Definition: queue.c:37
Contains declations of queue types, operations and structure.
@ eLL_QUEUE_CIRC
Link List Based Queue.
Definition: queue.h:12
t_gen create_stack(char *name, int max_size, e_stacktype stype, t_dparams *prm)
Create an instance of stack
Definition: stack.c:37
Contains declations of stack types, operations and structure.
@ eLL_STACK
LinkList based Stack.
Definition: stack.h:13
data params struct defn
Definition: common.h:18
f_cmpr cmpr
Routine used for comparing two given elems of said type.
Definition: common.h:23
f_swap swap
Routine used for swaping two elemnts of goven data.
Definition: common.h:25
f_free free
Routine used for freeing elements of said data.
Definition: common.h:26
f_print print_data
Routine used for printing elem data.
Definition: common.h:33
stack struct defn
Definition: stack.h:19
f_vgen destroy
routine to destroy stack contents
Definition: stack.h:36
f_gen2 push
stack operations
Definition: stack.h:29
f_empty empty
routine to check if stack is empty
Definition: stack.h:33
f_genidx peek
routine to peek elements in stack
Definition: stack.h:31
f_gen pop
routine to pop element into stack
Definition: stack.h:30
queue struct defn
Definition: queue.h:17
f_len len
routine to get length queue
Definition: queue.h:31
f_gen deq
routine to pop elements out of queue
Definition: queue.h:30
f_destroy destroy
routine to detroy queue instance
Definition: queue.h:36
f_empty empty
routine to check if queue empty
Definition: queue.h:34
f_ins enq
routine to push elements to queue
Definition: queue.h:29
tree node
Definition: tree.h:9
struct tree_node * rchild
Pointer to node right child.
Definition: tree.h:12
t_gen key
Pointer to node key.
Definition: tree.h:10
int height
height of node
Definition: tree.h:13
struct tree_node * lchild
Pointer to node left child.
Definition: tree.h:11
tree struct defn
Definition: tree.h:24
f_gen min
routine to get minm element in tree
Definition: tree.h:39
f_find find
routine to find element in tree
Definition: tree.h:36
f_cmpr cmpr
Definition: tree.h:50
f_len height
routine to get height of tree
Definition: tree.h:41
f_gen2 pred
routine to get predecessor to given node
Definition: tree.h:37
f_ins insert
routine to insert element in tree
Definition: tree.h:34
f_len node_count
routine to get total nodes in tree
Definition: tree.h:42
f_swap swap
Definition: tree.h:51
f_print print
routine to print tree level by level
Definition: tree.h:46
e_treetype type
Tree Type.
Definition: tree.h:27
char * name
Tree instance name.
Definition: tree.h:26
f_print inorder
routine to print inorder traversal of tree
Definition: tree.h:43
f_gen max
routine to get maxm element in tree
Definition: tree.h:40
f_destroy destroy
routine to destroy the tree instance
Definition: tree.h:47
f_free free
Definition: tree.h:52
f_print preorder
routine to print preorder traversal of tree
Definition: tree.h:44
t_gen root
Root node of the tree.
Definition: tree.h:31
f_print print_data
Definition: tree.h:53
f_gen2 succ
routine to get successor to given node
Definition: tree.h:38
f_del del
routine to delete element in tree
Definition: tree.h:35
int count
Tree node count.
Definition: tree.h:28
f_print postorder
routine to print postorder traversal of tree
Definition: tree.h:45
void tree_rotate_right(t_gen n)
rotate subtree right
Definition: tree.c:803
f_ins tree_insert[]
Look Up function ptrs for inserting elem to tree.
Definition: tree.c:30
t_gen create_tree(char *name, e_treetype ttype, t_dparams *prm)
Create an instance of tree
Definition: tree.c:45
int tree_slope(t_gen n)
get slope of node subtree
Definition: tree.c:787
t_gen tree_node_successor(t_gen, t_gen)
find the successor of a given node in tree
Definition: tree.c:325
void tree_preorder(t_gen)
preorder traverse tree
Definition: tree.c:621
t_gen tree_get_min_rcrs(t_gen root)
get min node in tree recursively
Definition: tree.c:230
void tree_inorder(t_gen)
inorder traverse tree
Definition: tree.c:664
t_gen tree_get_min(t_gen)
get min node in tree
Definition: tree.c:186
t_gen tree_delete_node_bst(t_gen, t_gen)
Delete a node in a bst tree
Definition: tree.c:375
f_len tree_height[]
Look Up function ptrs for getting height of tree.
Definition: tree.c:36
int tree_height_bst(t_gen n)
get height of bst
Definition: tree.c:465
t_gen tree_delete_node_avl(t_gen, t_gen)
Delete a node in an avl tree
Definition: tree.c:962
void print_tree(t_gen)
print tree info level by level
Definition: tree.c:706
f_del tree_del[]
Look Up function ptrs for deleting elem to tree.
Definition: tree.c:33
void tree_rotate_left(t_gen n)
rotate subtree left
Definition: tree.c:831
void tree_rebalance(t_gen n)
rebalance subtree
Definition: tree.c:858
t_gen tree_get_max_rcrs(t_gen root)
get max node in tree recursively
Definition: tree.c:252
int tree_height_avl(t_gen n)
get height of avl subtree
Definition: tree.c:767
void tree_insert_node_avl(t_gen, t_gen)
Add element to an avl tree
Definition: tree.c:884
void tree_postorder(t_gen)
postorder traverse tree
Definition: tree.c:561
t_gen tree_get_max(t_gen)
get max node in tree
Definition: tree.c:208
t_gen tree_node_predecessor(t_gen, t_gen)
find the predecessor of a given node in tree
Definition: tree.c:275
void tree_insert_node_bst(t_gen, t_gen)
Add element to a bst tree
Definition: tree.c:98
void destroy_tree(t_gen)
Destroy the instance of the tree
Definition: tree.c:515
t_gen tree_find_node(t_gen, t_gen)
find an element in tree
Definition: tree.c:155
int tree_node_count(t_gen n)
get_num_nodes in tree;
Definition: tree.c:86
Contains declations of tree types, operations and structure.
e_treetype
Types of trees.
Definition: tree.h:17
int(* f_len)(t_gen)
fn type of get len function
Definition: typedefs.h:55
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41
f_vgen2 f_ins
fn type of insert elem function
Definition: typedefs.h:58
e_cmpr
Custom Compare function return type.
Definition: typedefs.h:34
@ eGREAT
Definition: typedefs.h:37
@ eLESS
Definition: typedefs.h:35
@ eEQUAL
Definition: typedefs.h:36
f_gen2 f_del
fn type of delete elem function
Definition: typedefs.h:59
@ eINT32
Definition: typedefs.h:24