0% found this document useful (0 votes)
9 views

Binary Tree Implementation

Binary

Uploaded by

salmamoahmmed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Binary Tree Implementation

Binary

Uploaded by

salmamoahmmed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Binary Tree

1
Binary tree

This content of this file is an extension of the previous file for tree

2
Binary tree
Binary tree operation

 Inserting an element.
 Searching for an element.
 Deletion for an element.
 Traversing an element.
 Finding the height of the tree
 Finding the size of the entire tree.

3
Binary tree implementation
#include <iostream>
using namespace std;

#include <queue> //queue is a standard library container in C++

// Definition of the Node struct to represent a node in the binary tree


template<typename T>
struct Node {
T data; // The data of the node
Node *left; // Pointer to the left child node
Node *right; // Pointer to the right child node
};

4
Binary tree implementation
// Definition of the BinaryTree class to represent a binary tree
template<typename T>
class BinaryTree {
public:
Node<T>* root; // Pointer to the root node of the BT
BinaryTree() { root = NULL; } // Constructor for the BinaryTree class
Node<T>* createNode(T data); // Function to create a new Node with a given data
void insertLevelOrder(T data); // Function to insert a data (level order) into the BT.
void inOrder(Node<T>* node); // Function to perform an inorder traversal of the BT.
void preOrder(Node<T>* node);// Function to perform a preorder traversal of the BT.
void postOrder(Node<T>* node); // Function to perform a postorder traversal of the BT
int getHeight(Node<T>* node) ;//Returns the Height of the binary tree.
bool isEmpty(); // Check if the binary tree is empty
int getSize(Node<T>* node);//Function to return the size (number of nodes) in the BT.
void levelOrder(Node<T>* node);//Function to perform a Level-order traversal of the BT.
void printLevel(Node<T>* node, int level) ;
void levelOrderUsingQueue(Node<T>* node);// Function to perform a Level-order traversal
of the BT using using queue-based approach.
};
5
Binary tree implementation
// Function to create a new Node with a given data and return a pointer to it
template<typename T>
Node<T>* BinaryTree<T>::createNode(T data) {
Node<T>* newNode = new Node<T>; // Create a new node on the heap
newNode->data = data; // Assign the data to the new node
newNode->left = NULL; // Set the left child to null
newNode->right = NULL; // Set the right child to null
return newNode; // Return a pointer to the new node
}

// Function to perform an inorder traversal of the binary tree


template<typename T>
void BinaryTree<T>::inOrder(Node<T>* node) {
if (node == NULL) { // If the node is null
return; } // Exit the function
inOrder(node->left); // Recursively traverse the left subBT
cout << node->data << " "; // Visit the node and print its data
inOrder(node->right); // Recursively traverse the right subBT
}
6
Binary tree implementation
// Function to perform a preorder traversal of the binary tree
template<typename T>
void BinaryTree<T>::preOrder(Node<T>* node) {
if (node == NULL) { // If the node is null
return; } // Exit the function
cout << node->data << " "; // Visit the node and print its data
preOrder(node->left); // Recursively traverse the left subBT
preOrder(node->right); // Recursively traverse the right subBT
}

// Function to perform a postorder traversal of the binary tree


template<typename T>
void BinaryTree<T>::postOrder(Node<T>* node) {
if (node == NULL) { // If the node is null
return; } // Exit the function
postOrder(node->left); // Recursively traverse the left subBT
postOrder(node->right); // Recursively traverse the right subBT
cout << node->data << " "; // Visit the node and print its data
}
7
Binary tree implementation
//function to calculate the height of a binary search tree
template<typename T>
int BinaryTree<T>::getHeight(Node<T>* node) {
if (node == nullptr) { // base case: null node has height of 0
return 0;}
int leftHeight = getHeight(node->left);//recursively calculate left subtree height
int rightHeight = getHeight(node->right);//recursively calculate right subtree height
return (leftHeight > rightHeight) ? leftHeight + 1 : rightHeight + 1; // compare
heights and return the larger one plus 1
}

// Check if the binary tree is empty


template<typename T>
bool BinaryTree<T>::isEmpty() {
return root == nullptr; // if the root is null, the tree is empty
}

8
Binary tree implementation
//Function to return the size (number of nodes) in the binary tree.
template<typename T>
int BinaryTree<T>::getSize(Node<T>* node) {
if (node == nullptr) { // base case: empty tree has size of 0
return 0; }
// recursively calculate the size of the left and right subtrees
int leftSize = getSize(node->left);
int rightSize = getSize(node->right);
// return the sum of the sizes of the left and right subtrees plus 1 for the root node
return leftSize + rightSize + 1;
}

9
Binary tree implementation
// Function to perform a Level-order traversal of the BT.
template<typename T>
void BinaryTree<T>::levelOrder(Node<T>* node) {
int height = getHeight(root); // get the height of the binary tree
for (int i = 1; i <= height; i++) { // iterate through each level
printLevel(root, i); // print nodes at current level
}
}
// Function to perform a Level-order traversal of the the binary tree.
template<typename T>
void BinaryTree<T>::printLevel(Node<T>* node, int level) {
if (node == nullptr) { // base case: null node
return; }
if (level == 1) { // current level is 1
cout << node->data << " "; }
else if (level > 1) { // current level is greater than 1
printLevel(node->left, level-1); // print nodes at lower level
printLevel(node->right, level-1); // print nodes at lower level
}
} 10
Binary tree implementation
// Function to insert a data into the binary tree
template<typename T>
void BinaryTree<T>::insertLevelOrder(T data) {
// If the binary tree is empty, create a new node as the root node
if (root == NULL) {
root = createNode(data);
return;}
queue<Node<T>*> q; // Create an empty queue for level order traversal
q.push(root);
// Traverse the binary tree level by level using a queue
while (!q.empty()) {
Node<T>* node = q.front(); // Get the current node from the front of the queue
q.pop();

Note that push(), pop(), front() function is found in the standard


library in c++ #include<queue>
11
Binary tree implementation
// Check if the current node has a left child
if (node->left == NULL) {
// If it does not have a left child, create a new node with the data and make it the left
child
node->left = createNode(data);
break; } // Exit the loop since we have inserted the new node
else
// If it has a left child, add the left child to the queue so that we can visit it later
{ q.push(node->left); }
if (node->right == NULL) // Check if the current node has a right child
// If it does not have a right child, create a new node with the data and make it the right
child
{ node->right = createNode(data);
break; } // Exit the loop since we have inserted the new node
else
// If it has a right child, add the right child to the queue so that we can visit it later
{ q.push(node->right);}
} Note that push(), pop(), front() function is found in the standard
} library in c++ #include<queue>
12
Binary tree implementation
// function to displays the nodes (level order) in a binary tree using a queue-based
approach
template<typename T>
void BinaryTree<T>::levelOrderUsingQueue(Node<T>* node) {
if (node == nullptr) { // If the root node is null, there's nothing to display.
return; }
queue<Node<T>*> q; // Create an empty queue to hold the nodes we'll be visiting.
q.push(node); // Add the root node to the queue.
// While there are still nodes left to visit...
while (!q.empty()) {
Node<T>* current = q.front(); // Get the next node from the front of the queue.
q.pop(); // Remove the node from the queue.
cout << current->data << " "; // Display the data in the current node.
// If there's a left child, add it to the queue.
if (current->left != nullptr) {q.push(current->left);}
// If there's a right child, add it to the queue.
if (current->right != nullptr) { q.push(current->right);
}
} Note that push(), pop(), front() function is found in the standard
} library in c++ #include<queue> 13
Binary tree implementation
// Check if the current node has a left child
if (node->left == NULL) {
// If it does not have a left child, create a new node with the data and make it the left
child
node->left = createNode(data);
break; // Exit the loop since we have inserted the new node
} else {
// If it has a left child, add the left child to the queue so that we can visit it later
q.push(node->left);}
// Check if the current node has a right child
if (node->right == NULL) {
// If it does not have a right child, create a new node with the data and make it the right
child
node->right = createNode(data);
break; // Exit the loop since we have inserted the new node
} else {
// If it has a right child, add the right child to the queue so that we can visit it later
q.push(node->right);}
} Note that push(), pop(), front() function is found in the standard
} library in c++ #include<queue> 14
Binary tree implementation
#include <iostream>
using namespace std;
#include "binaryTree.h"
int main() {
BinaryTree<int> BT; // Create a new binary tree object of type int
// Insert some nodes into the binary tree
BT.insertLevelOrder(5); BT.insertLevelOrder(8); BT.insertLevelOrder(10);
BT.insertLevelOrder(15); BT.insertLevelOrder(20);
cout << "In-order traversal: "; BT.inOrder(BT.root);
cout << "Pre-order traversal: "; BT.preOrder(BT.root);
cout << "Post-order traversal: "; BT.postOrder(BT.root);
cout << "Level-order traversal: "; BT.levelOrder(BT.root);
cout << "Level-order traversal using queue ";
BT.levelOrderUsingQueue(BT.root);
cout << "Height of Binary Tree: " << BT.getHeight(BT.root) << endl;
cout << "Size of Binary Tree: " << BT.getSize(BT.root) << endl;
return 0;
}

15

You might also like