|
| 1 | +// |
| 2 | +// bst.cpp |
| 3 | +// |
| 4 | + |
| 5 | +#include "BST.h" |
| 6 | + |
| 7 | +/* constructs an empty Binary Search Tree */ |
| 8 | +BST::BST() |
| 9 | +{ |
| 10 | + root = nullptr; |
| 11 | +} |
| 12 | + |
| 13 | +/* constructs a one-node Binary Search Tree with the given key */ |
| 14 | +BST::BST(int num) |
| 15 | +{ |
| 16 | + root = new Node; |
| 17 | + root->key = num; |
| 18 | + root->left = nullptr; |
| 19 | + root->right = nullptr; |
| 20 | +} |
| 21 | + |
| 22 | +/* constructs a Binary Search Tree by repeatedly inserting ints in <nums> |
| 23 | + in the order they appear in the array */ |
| 24 | +BST::BST(vector<int>& nums) |
| 25 | +{ |
| 26 | + root = new Node; |
| 27 | + root->key = nums[0]; |
| 28 | + root->left = nullptr; |
| 29 | + root->right = nullptr; |
| 30 | + for (int i=1; i<nums.size(); i++) |
| 31 | + insert(nums[i]); |
| 32 | +} |
| 33 | + |
| 34 | +/* insert num as a new key to the BST */ |
| 35 | +void BST::insert(int num) |
| 36 | +{ |
| 37 | + insert(num, root); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +/* insert num as a new key to the BST at node 'leaf' */ |
| 42 | +void BST::insert(int num, Node *leaf) |
| 43 | +{ |
| 44 | + if (num > leaf->key) // insert to the right |
| 45 | + { |
| 46 | + if (leaf->right == nullptr) |
| 47 | + { |
| 48 | + leaf->right = new Node; |
| 49 | + leaf->right->key = num; |
| 50 | + leaf->right->left = nullptr; |
| 51 | + leaf->right->right = nullptr; |
| 52 | + } |
| 53 | + else |
| 54 | + insert(num, leaf->right); |
| 55 | + } |
| 56 | + else // insert to the left |
| 57 | + { |
| 58 | + if (leaf->left == nullptr) |
| 59 | + { |
| 60 | + leaf->left = new Node; |
| 61 | + leaf->left->key = num; |
| 62 | + leaf->left->left = nullptr; |
| 63 | + leaf->left->right = nullptr; |
| 64 | + } |
| 65 | + else |
| 66 | + insert(num, leaf->left); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/* return the BST rooted at this node as a human-readable string */ |
| 71 | +string BST::print() |
| 72 | +{ |
| 73 | + return print(root, 0); |
| 74 | +} |
| 75 | + |
| 76 | +/* rturn the BST rooted at leaf as a human-readable string, |
| 77 | + indented by <depth> characters */ |
| 78 | +string BST::print(Node *leaf, int depth) |
| 79 | +{ |
| 80 | + string result = ""; |
| 81 | + |
| 82 | + result.append(to_string(leaf->key)); // output the key |
| 83 | + result.append("\n"); |
| 84 | + |
| 85 | + if (leaf->left != nullptr) |
| 86 | + { |
| 87 | + result.append(RepeatChar(depth)); |
| 88 | + result.append("L"); |
| 89 | + result.append(print(leaf->left, depth+1)); |
| 90 | + } |
| 91 | + if (leaf->right != nullptr) |
| 92 | + { |
| 93 | + result.append(RepeatChar(depth)); |
| 94 | + result.append("R"); |
| 95 | + result.append(print(leaf->right, depth+1)); |
| 96 | + } |
| 97 | + |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
| 101 | +/* return a string of <depth> spaces */ |
| 102 | +string BST::RepeatChar(int depth) |
| 103 | +{ |
| 104 | + string result = ""; |
| 105 | + |
| 106 | + for (int i=0; i<depth; i++) |
| 107 | + result.append("-"); |
| 108 | + |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +/** |
| 114 | + * Represents one rotation (ZIG or ZAG) around one key in a Binary Search Tree |
| 115 | + */ |
| 116 | +Rotation::Rotation(int num, RotationType rot) |
| 117 | +{ |
| 118 | + myKey = num; |
| 119 | + myRot = rot; |
| 120 | +} |
| 121 | + |
| 122 | +/* return A human-readable description of the rotation */ |
| 123 | +string Rotation::print() |
| 124 | +{ |
| 125 | + string result=""; |
| 126 | + |
| 127 | + switch (myRot) |
| 128 | + { |
| 129 | + case ZIG: |
| 130 | + result="ZIG"; |
| 131 | + break; |
| 132 | + case ZAG: |
| 133 | + result="ZAG"; |
| 134 | + break; |
| 135 | + } |
| 136 | + result.append(" on "); |
| 137 | + result.append(to_string(myKey)); |
| 138 | + |
| 139 | + return result; |
| 140 | +} |
0 commit comments