0% found this document useful (0 votes)
47 views4 pages

Tree Traversal in C

This document discusses different traversal methods for binary trees in C++ including in-order, pre-order, post-order and level-order traversal. It defines a TreeNode struct and functions for inserting nodes, traversing trees using each method, and provides a main function that builds a sample tree and calls each traversal.

Uploaded by

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

Tree Traversal in C

This document discusses different traversal methods for binary trees in C++ including in-order, pre-order, post-order and level-order traversal. It defines a TreeNode struct and functions for inserting nodes, traversing trees using each method, and provides a main function that builds a sample tree and calls each traversal.

Uploaded by

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

Tree traversal in c++:

#include <iostream>
#include <queue>

// Node structure for the binary tree


struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;

TreeNode(int val) : data(val), left(nullptr), right(nullptr) {}


};

// Function to insert a new node into the binary tree


TreeNode* insert(TreeNode* root, int val) {
if (root == nullptr) {
return new TreeNode(val);
}

if (val < root->data) {


root->left = insert(root->left, val);
} else if (val > root->data) {
root->right = insert(root->right, val);
}

return root;
}
// In-order traversal of the binary tree
void inorderTraversal(TreeNode* root) {
if (root == nullptr) {
return;
}

inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->right);
}

// Pre-order traversal of the binary tree


void preorderTraversal(TreeNode* root) {
if (root == nullptr) {
return;
}

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


preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Post-order traversal of the binary tree


void postorderTraversal(TreeNode* root) {
if (root == nullptr) {
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
std::cout << root->data << " ";
}

// Level-order traversal (Breadth-first traversal) of the binary tree


void levelOrderTraversal(TreeNode* root) {
if (root == nullptr) {
return;
}

std::queue<TreeNode*> nodeQueue;
nodeQueue.push(root);

while (!nodeQueue.empty()) {
TreeNode* current = nodeQueue.front();
std::cout << current->data << " ";
nodeQueue.pop();

if (current->left) {
nodeQueue.push(current->left);
}
if (current->right) {
nodeQueue.push(current->right);
}
}
}
int main() {
TreeNode* root = nullptr;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);

std::cout << "In-order Traversal: ";


inorderTraversal(root);
std::cout << std::endl;

std::cout << "Pre-order Traversal: ";


preorderTraversal(root);
std::cout << std::endl;

std::cout << "Post-order Traversal: ";


postorderTraversal(root);
std::cout << std::endl;

std::cout << "Level-order Traversal: ";


levelOrderTraversal(root);
std::cout << std::endl;

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy