0% found this document useful (0 votes)
46 views10 pages

Binary Tree Traversal

The document contains C++ code for creating and traversing a binary tree using various methods, including recursive and iterative approaches. It defines a TreeNode structure and implements functions for inorder, preorder, postorder, and level order traversals, both recursively and iteratively. Additionally, it includes helper functions to create a sample tree and to free the allocated memory.

Uploaded by

mirajramadan00
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)
46 views10 pages

Binary Tree Traversal

The document contains C++ code for creating and traversing a binary tree using various methods, including recursive and iterative approaches. It defines a TreeNode structure and implements functions for inorder, preorder, postorder, and level order traversals, both recursively and iteratively. Additionally, it includes helper functions to create a sample tree and to free the allocated memory.

Uploaded by

mirajramadan00
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

#include <iostream>

// Definition for a binary tree node.

struct TreeNode {

int val;

TreeNode *left;

TreeNode *right;

};

// Function to create a new TreeNode

TreeNode* createTreeNode(int val) {

TreeNode* newNode = new TreeNode;

newNode->val = val;

newNode->left = nullptr;

newNode->right = nullptr;

return newNode;

// --- Recursive Traversals ---

// Inorder Traversal (Left -> Root -> Right)

void inorderRecursive(TreeNode* root) {

if (root == nullptr) {

return;

inorderRecursive(root->left);

std::cout << root->val << " ";

inorderRecursive(root->right);
}

// Preorder Traversal (Root -> Left -> Right)

void preorderRecursive(TreeNode* root) {

if (root == nullptr) {

return;

std::cout << root->val << " ";

preorderRecursive(root->left);

preorderRecursive(root->right);

// Postorder Traversal (Left -> Right -> Root)

void postorderRecursive(TreeNode* root) {

if (root == nullptr) {

return;

postorderRecursive(root->left);

postorderRecursive(root->right);

std::cout << root->val << " ";

// --- Iterative Traversals (Manual Stack) ---

// Structure for a simple stack node

struct StackNode {

TreeNode* node;

StackNode* next;
};

// Function to push onto the stack

void push(StackNode** top, TreeNode* node) {

StackNode* newNode = new StackNode;

newNode->node = node;

newNode->next = *top;

*top = newNode;

// Function to pop from the stack

TreeNode* pop(StackNode** top) {

if (*top == nullptr) {

return nullptr;

StackNode* temp = *top;

TreeNode* poppedNode = temp->node;

*top = (*top)->next;

delete temp;

return poppedNode;

// Function to check if the stack is empty

int isEmpty(StackNode* top) {

return top == nullptr;

// Iterative Inorder Traversal (Manual Stack)


void inorderIterative(TreeNode* root) {

StackNode* stackTop = nullptr;

TreeNode* current = root;

while (current != nullptr || !isEmpty(stackTop)) {

while (current != nullptr) {

push(&stackTop, current);

current = current->left;

current = pop(&stackTop);

std::cout << current->val << " ";

current = current->right;

// Iterative Preorder Traversal (Manual Stack)

void preorderIterative(TreeNode* root) {

if (root == nullptr) {

return;

StackNode* stackTop = nullptr;

push(&stackTop, root);

while (!isEmpty(stackTop)) {

TreeNode* node = pop(&stackTop);

std::cout << node->val << " ";

if (node->right != nullptr) {
push(&stackTop, node->right);

if (node->left != nullptr) {

push(&stackTop, node->left);

// Iterative Postorder Traversal (Manual Stack - requires careful handling)

void postorderIterative(TreeNode* root) {

if (root == nullptr) {

return;

StackNode* s1Top = nullptr;

StackNode* s2Top = nullptr;

push(&s1Top, root);

while (!isEmpty(s1Top)) {

TreeNode* node = pop(&s1Top);

push(&s2Top, node);

if (node->left != nullptr) {

push(&s1Top, node->left);

if (node->right != nullptr) {

push(&s1Top, node->right);

}
while (!isEmpty(s2Top)) {

std::cout << pop(&s2Top)->val << " ";

// --- Iterative Level Order Traversal (Manual Queue) ---

// Structure for a simple queue node

struct QueueNode {

TreeNode* node;

QueueNode* next;

};

// Function to enqueue

void enqueue(QueueNode** front, QueueNode** rear, TreeNode* node) {

QueueNode* newNode = new QueueNode;

newNode->node = node;

newNode->next = nullptr;

if (*rear == nullptr) {

*front = newNode;

*rear = newNode;

} else {

(*rear)->next = newNode;

*rear = newNode;

}
// Function to dequeue

TreeNode* dequeue(QueueNode** front, QueueNode** rear) {

if (*front == nullptr) {

return nullptr;

QueueNode* temp = *front;

TreeNode* dequeuedNode = temp->node;

*front = (*front)->next;

if (*front == nullptr) {

*rear = nullptr; // Queue is now empty

delete temp;

return dequeuedNode;

// Function to check if the queue is empty

int isQueueEmpty(QueueNode* front) {

return front == nullptr;

// Iterative Level Order Traversal (Manual Queue)

void levelOrderIterative(TreeNode* root) {

if (root == nullptr) {

return;

QueueNode* front = nullptr;

QueueNode* rear = nullptr;

enqueue(&front, &rear, root);


while (!isQueueEmpty(front)) {

TreeNode* current = dequeue(&front, &rear);

std::cout << current->val << " ";

if (current->left != nullptr) {

enqueue(&front, &rear, current->left);

if (current->right != nullptr) {

enqueue(&front, &rear, current->right);

// Helper function to create a sample binary tree

TreeNode* createSampleTree() {

TreeNode* root = createTreeNode(1);

root->left = createTreeNode(2);

root->right = createTreeNode(3);

root->left->left = createTreeNode(4);

root->left->right = createTreeNode(5);

return root;

// Helper function to free the memory of the tree

void deleteTree(TreeNode* root) {

if (root == nullptr) return;

deleteTree(root->left);
deleteTree(root->right);

delete root;

int main() {

TreeNode* root = createSampleTree();

std::cout << "Inorder Traversal (Recursive): ";

inorderRecursive(root);

std::cout << std::endl;

std::cout << "Inorder Traversal (Iterative): ";

inorderIterative(root);

std::cout << std::endl;

std::cout << "Preorder Traversal (Recursive): ";

preorderRecursive(root);

std::cout << std::endl;

std::cout << "Preorder Traversal (Iterative): ";

preorderIterative(root);

std::cout << std::endl;

std::cout << "Postorder Traversal (Recursive): ";

postorderRecursive(root);

std::cout << std::endl;

std::cout << "Postorder Traversal (Iterative): ";


postorderIterative(root);

std::cout << std::endl;

std::cout << "Level Order Traversal (Iterative): ";

levelOrderIterative(root);

std::cout << std::endl;

deleteTree(root); // Clean up memory

return 0;

You might also like