Last active
June 7, 2020 03:06
-
-
Save misterpoloy/79cd6a76a99bfb0fdb22af25f560cec1 to your computer and use it in GitHub Desktop.
Binary tree example implementation in cpp, including mirrow function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <queue> | |
#include <climits> | |
struct BSTNode { | |
int value; | |
BSTNode* left; | |
BSTNode* right; | |
BSTNode() { | |
left = right = nullptr; | |
} | |
BSTNode(int value): | |
value(value) { | |
left = right = nullptr; | |
} | |
}; | |
class BSTree { | |
private: | |
BSTNode* head; | |
public: | |
BSTree() { | |
head = nullptr; | |
} | |
BSTNode* getHead() { | |
return head; | |
} | |
void add(int value) { | |
if (head == nullptr) { | |
head = new BSTNode(value); | |
return; | |
} | |
addNode(head, value); | |
} | |
void remove (int value) { | |
removeNode(head, value); | |
}; | |
static BSTNode* addNode(BSTNode* root, int value) { | |
if(root == nullptr) { | |
root = new BSTNode(value); | |
} | |
else if (value <= root->value) { | |
root->left = addNode(root->left, value); | |
} else { | |
root->right = addNode(root->right, value); | |
} | |
return root; | |
}; | |
static BSTNode* removeNode (BSTNode* root, int value) { | |
if(root == nullptr) { | |
std::cout << "Does not exist" << std::endl; | |
return root; | |
} | |
if (root->value < value) { | |
root->right = removeNode(root->right, value); | |
} | |
else if (root->value > value) { | |
root->left = removeNode(root->left, value); | |
} | |
else { | |
// Case 1: Leaf node | |
if (root->left == nullptr && root->right == nullptr) { | |
delete root; | |
root = nullptr; | |
} | |
// Case 2: Only one children | |
else if (root->right == nullptr) { | |
BSTNode* temp = root; | |
root = root->left; | |
delete temp; | |
} | |
else if (root->left == nullptr) { | |
BSTNode* temp = root; | |
root = root->right; | |
delete temp; | |
} | |
// Case 3: Have both childrens | |
else { | |
// Pick min from left | |
BSTNode* temp = findMinRecursive(root->right); | |
root->value = temp->value; | |
root->right = removeNode(root->right, temp->value); | |
} | |
} | |
return root; | |
}; | |
bool search(int value) { | |
return searchRecursive(head, value); | |
}; | |
bool searchRecursive(BSTNode* root, int value) { | |
if (root == nullptr) return false; | |
if (root->value == value) return true; | |
if (value <= root->value) { | |
return searchRecursive(root->left, value); | |
} else { | |
return searchRecursive(root->right, value); | |
} | |
} | |
BSTNode* findMinIterative() { | |
BSTNode* helper = head; | |
while (helper->left != nullptr) { | |
helper = helper->left; | |
} | |
return helper; | |
} | |
static BSTNode* findMinRecursive(BSTNode* root) { | |
if (root->left == nullptr) return root; | |
return findMinRecursive(root->left); | |
} | |
static BSTNode* findMaxRecursive(BSTNode* root) { | |
if (root->right == nullptr) return root; | |
return findMaxRecursive(root->right); | |
} | |
/** CodeChallenge count the sum of all of its node depths. | |
int countNodeDepth(BinaryTree* root, int depth = 0) { | |
if (root == NULL) return 0; | |
int left = countNodeDepth(root->left, depth + 1); | |
int right = countNodeDepth(root->right, depth + 1); | |
return left + right + depth; | |
} **/ | |
// Always double check the definition of root node | |
static int getHeight(BSTNode* root) { | |
if (root == nullptr) return -1; | |
int leftNode = getHeight(root->left); | |
int rightNode = getHeight(root->right); | |
return std::max(leftNode, rightNode) + 1; | |
} | |
void traverseLevelOrder() { | |
if (head == nullptr) return; | |
std::cout << "-- LEVEL ORDER TRAVERSE" << std::endl; | |
std::queue<BSTNode*> queue; | |
queue.push(head); | |
while (!queue.empty()) { | |
BSTNode* current = queue.front(); | |
std::cout << queue.front()->value << std::endl; | |
if (current->left != nullptr) queue.push(current->left); | |
if (current->right != nullptr) queue.push(current->right); | |
queue.pop(); | |
} | |
}; | |
// Time Complexity O(h) header. | |
// Sorted order | |
void static inOrderTraversal(BSTNode* root) { | |
if (root == nullptr) return; | |
inOrderTraversal(root->left); | |
std::cout << root->value << std::endl; | |
inOrderTraversal(root->right); | |
} | |
static void mirrow(BSTNode* root) { | |
if (root == nullptr) return; | |
// Order here does not matter | |
mirrow(root->left); | |
mirrow(root->right); | |
// Temporal can be at the end | |
BSTNode* temporal; | |
temporal = root->left; | |
root->left = root->right; | |
root->right = temporal; | |
} | |
}; | |
bool isBinaryTree(BSTNode* root, int min = INT_MIN, int max = INT_MAX) { | |
if (root == nullptr) { | |
return true; | |
} | |
if ( | |
root->value > min && | |
root->value < max && | |
isBinaryTree(root->left, min, root->value) && | |
isBinaryTree(root->right, root->value, max) | |
) { | |
return true; | |
} | |
return false; | |
} | |
int main() { | |
BSTree Root; | |
Root.add(3); | |
Root.add(2); | |
Root.add(1); | |
Root.add(5); | |
Root.add(4); | |
Root.add(6); | |
Root.remove(7); | |
// BSTNode* Min = Root.findMinIterative(); | |
// BSTNode* Max = BSTree::findMaxRecursive(Root.getHead()); | |
// int height = BSTree::getHeight(Root.getHead()); | |
// std::cout << Root.search(3) << std::endl; | |
// std::cout << Root.search(6) << std::endl; | |
//std::cout << "Minimal value (iterative) " << Min->value << std::endl; | |
//std::cout << "Max value (recursive): " << Max->value << std::endl; | |
// std::cout << "height: " << height << std::endl; | |
Root.traverseLevelOrder(); | |
// std::cout << "-- PRE ORDER TRAVERSE" << std::endl; | |
// BSTree::inOrderTraversal(Root.getHead()); | |
// BSTree::mirrow(Root.getHead()); | |
// std::cout << "Mirrow: " << std::endl; | |
// Root.traverseLevelOrder(); | |
std::cout << "is binary= " << isBinaryTree(Root.getHead()) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment