Binary Tree
Trees
A tree consists of Nodes that points to multiple nodes (In this case, binary tree has been consider where each node can point to at max 2 nodes). The Node that points to other Node is called it’s parent and the Node that is being pointed is called it’s child Node. A parent can have multiple child nodes but a child Node can’t have multiple parent Nodes. A child can also be a parent Node to other child Nodes. The last child Node at the end of the tree which further does not point to any Node is called a leaf.
- A Tree is called Full if all parent Nodes point to 2 child Nodes or does not point to any Node (In case of binary trees).
- A Tree is called Perfect if all parent Nodes point to 2 child Nodes (In case of binary trees).
- A Tree is called Complete if the Tree is filled from left to right (In case of binary trees).
Binary Search Tree
The arrangement of the Nodes is as per the values compared to the already existing Node. Just follow a simple rule:
- If the value of the Node is greater than the Parent Node, keep it to it’s right.
- If the value of the Node is less than the Parent Node, keep it to it’s left.
Operation Complexity of a Binary Search Tree
For a binary search tree, number of elements in a full tree with each levels filled will be (2^n) - 1. For larger values, 1 becomes insignificant. Thus to get to a point, n operations have to be applied which makes the operational complexity equal to O(log base 2 N) where N is the number of Nodes in the binary search tree.
But consider a case where there are all elements attached in increasing order in which case, all the further appending Nodes are at the right side. This will technically be a Linked List making it’s operational complexity to be O(n).
No two Nodes in a Binary tree can have same value.
#include <iostream>
using namespace std;
// Node Class will create a Node
class Node{
public:
int value;
Node* right;
Node* left;
Node(int value){
this->value = value;
right = nullptr;
left = nullptr;
}
};
// BinarySearchTree Class develops a Binary Search Tree
class BinarySearchTree{
public:
Node* root;
public:
/*
BinarySearchTree(int value){
Node* newNode = new Node(value);
root = newNode;
}
In this case, a Node is being created as soon as the BinarySearchTree class is called.
This can be a approach can also be done differently.
Rather than creating a new Node, keep the Binary Tree empty.
Set the root pointer nullptr.
*/
// Constructor for BinarySearchTree Class
BinarySearchTree(){
root = nullptr;
}
// The insert function will insert a Node of given value to the Binary Search Tree
bool insert(int value){
Node* newNode = new Node(value);
if (root == nullptr){
root = newNode;
return true;
}
Node* temp = root;
while (true){
if (newNode->value == temp->value) return false;
if (newNode->value < temp->value){
if (temp->left == nullptr){
temp->left = newNode;
return true;
}
temp = temp->left;
}
else{
if (temp->right == nullptr){
temp->right = newNode;
return true;
}
temp = temp->right;
}
}
}
// The contains function will search the given value in the Binary Search Tree and return true if the value is present in the Tree or false if the value is not present in the Binary Search Tree
bool contains(int value){
Node* temp = root;
while (temp){
if (value < temp->value){
temp = temp->left;
}
else if (value > temp->value){
temp = temp->right;
}
else{
return true;
}
}
return false;
}
};
int main(){
BinarySearchTree* myBST = new BinarySearchTree;
myBST->insert(12);
myBST->insert(4);
myBST->insert(23);
myBST->insert(45);
myBST->insert(21);
myBST->insert(76);
myBST->insert(44);
myBST->insert(54);
cout << myBST->root->right->left->value << endl;
}