Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include "hed.h"
  2.  
  3. using namespace std;
  4. address allocation(int x) {
  5.     address Root = new Node;
  6.     info(Root) = x;
  7.     left(Root) = NULL;
  8.     right(Root) = NULL;
  9.     return Root;
  10. }
  11.  
  12. void createTree(BinarySrcTree &T) {
  13.     T.Root = NULL;
  14. }
  15.  
  16. bool isLeaf(address Root) {
  17.     return (left(Root) == NULL) && (right(Root) == NULL);
  18. }
  19.  
  20. void insertBST(int x, address &Root) {
  21.     if (Root == NULL) {
  22.             Root = allocation(x);
  23.     }
  24.     else {
  25.             if(x < info(Root)) {
  26.                     insertBST(x,left(Root));
  27.             }else if (x > info(Root)) {
  28.                     insertBST(x, right(Root));
  29.             }
  30.     }
  31. }
  32.  
  33. void inOrder(address Root) {
  34.     if (Root != NULL) {
  35.             inOrder(left(Root));
  36.             cout<<info(Root)<<", ";
  37.             inOrder(right(Root));
  38.     }
  39. }
  40.  
  41. address findNode(address Root, int x) {
  42.     if ((Root == NULL) || (info(Root) == x)) {
  43.             return Root;
  44.     }
  45.     else {
  46.             if (x < info(Root)) {
  47.                     findNode(left(Root), x);
  48.             }else if (x > info(Root)) {
  49.                     findNode(right(Root), x);
  50.             }
  51.     }
  52. }
  53.  
  54. int CountInternalNode(address Root) {
  55.     if (Root == NULL || (isLeaf(Root))) return 0;
  56.     return (1 + CountInternalNode(left(Root)) + CountInternalNode(right(Root)));
  57. }
  58.  
  59.  
  60. void printLeaves(address Root) {
  61.     if (Root == NULL) {
  62.             return;
  63.     }
  64.     else {
  65.             if (isLeaf(Root)) {
  66.                     cout<<info(Root)<<", ";
  67.             }
  68.             else if ((left(Root) != NULL) || (right(Root) != NULL)) {
  69.                     printLeaves(left(Root));
  70.                     printLeaves(right(Root));
  71.             }
  72.     }
  73. }
  74.  
  75. int countLeaves(address Root) {
  76.     if (Root == NULL) return 0;
  77.     int a = countLeaves(left(Root));
  78.     int b = countLeaves(right(Root));
  79.     if (isLeaf(Root)) return 1;
  80.     return a + b;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement