0% found this document useful (0 votes)
20 views21 pages

Tree Operations

The document provides an overview of binary tree operations, including various traversal methods such as preorder, inorder, and postorder, along with their recursive implementations. It also discusses concepts like mirror images of binary trees, calculating the height, printing leaf nodes, counting nodes, and level-wise traversal. Each section includes algorithms and code snippets to illustrate the operations on binary trees.

Uploaded by

OML series
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)
20 views21 pages

Tree Operations

The document provides an overview of binary tree operations, including various traversal methods such as preorder, inorder, and postorder, along with their recursive implementations. It also discusses concepts like mirror images of binary trees, calculating the height, printing leaf nodes, counting nodes, and level-wise traversal. Each section includes algorithms and code snippets to illustrate the operations on binary trees.

Uploaded by

OML series
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/ 21

Operations on Binary Trees

Representation of Binary Tree


Struct node
{

int data;
struct node * left, right;
};
Binary Tree Traversal

1. Preorder traversal:-In this traversal method first process root


element, then left sub tree and then right sub tree.
Procedure:-
Step 1: Visit root node
Step 2: Visit left sub tree in preorder
Step 3: Visit right sub tree in preorder
2. Inorder traversal:-

In this traversal method first process left element, then root element
and then the right element.
Procedure:-
Step 1: Visit left sub tree in inorder
Step 2: Visit root node
Step 3: Visit right sub tree in inorder
3. Postorder traversal:-

In this traversal first visit / process left sub tree, then right sub tree and
then the root element.
Procedure:-
Step 1: Visit left sub tree in postorder
Step 2: Visit right sub tree in postorder
Step 3: Visit root node
Arithmetic Expression Using BT
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/ABCDE
* D prefix expression
postorder traversal
AB/C*D*E+
/ C
postfix expression
level order traversal
A B +*E*D/CAB

CHAPTER 5 6
Inorder Traversal (recursive version)
void inorder(tree_pointer ptr)
/* inorder tree traversal */
{
A/B*C*D+E
if (ptr) {
inorder(ptr->left_child);
printf(“%d”, ptr->data);
indorder(ptr->right_child);
}
}
CHAPTER 5 7
Preorder Traversal (recursive
version)
void preorder(tree_pointer ptr)
/* preorder tree traversal */
{
+**/ABCDE
if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->left_child);
predorder(ptr->right_child);
}
}
CHAPTER 5 8
Postorder Traversal (recursive
version)
void postorder(tree_pointer ptr)
/* postorder tree traversal */
{
AB/C*D*E+
if (ptr) {
postorder(ptr->left_child);
postdorder(ptr->right_child);
printf(“%d”, ptr->data);
}
}
CHAPTER 5 9
Iterative Inorder Traversal
(using stack)
void iter_inorder(tree_pointer node)
{
int top= -1; /* initialize stack */
tree_pointer stack[MAX_STACK_SIZE];
for (;;) {
for (; node; node=node->left_child)
add(&top, node);/* add to stack */
node= delete(&top);
/* delete from stack */
if (!node) break; /* empty stack */
printf(“%D”, node->data);
node = node->right_child;
}
} O(n) CHAPTER 5 10
Mirror Image
• Mirror image of a binary tree is another binary tree which can be
created by swapping left child and right child at each node of a tree.
So, to find the mirror image of a binary tree, we just have to swap the
left child and right child of each node in the binary tree.
Mirror Image
• Create the binary tree with dummy data.
• Write a recursive function to find the mirror of the given binary tree.
• Recursively call the function with left and right nodes.
• Swap the left node data with the right node data.
• Print the tree.
Mirror Image
Height
• The height of a Binary Tree is defined as the maximum depth of any
leaf node from the root node. That is, it is the length of the longest
path from the root node to any leaf node.
Finding Height of Binary Tree
// Find height of a tree, defined by the root node
int tree_height (Node* root) {
if (root == NULL)
return 0;
else {
// Find the height of left, right subtrees
left_height = tree_height(root->left);
right_height = tree_height(root->right);

// Find max(subtree_height) + 1 to get the height of the tree


return max(left_height, right_height) + 1;
}
Finding Leaf Nodes
• Given a binary tree, we need to write a program to print all leaf nodes
of the given binary tree from left to right. That is, the nodes should be
printed in the order they appear from left to right in the given tree.

• Leaf Nodes: 4,6,7,9,10


Algorithm to Print Leaf Nodes
• Check if the given node is null. If null, then return from the function.
• Check if it is a leaf node. If the node is a leaf node, then print its data.
• If in the above step, the node is not a leaf node then check if the left
and right children of node exist. If yes then call the function for left
and right child of the node recursively.
Algorithm to Print Leaf Nodes
void printLeafNodes(Node *root)
{ // if left child exists, check for leaf
// if node is null, return // recursively
if (!root) if (root->left)
return; printLeafNodes(root->left);

// if node is leaf node, print its data // if right child exists, check for leaf
if (!root->left && !root->right) // recursively
{ if (root->right)
cout << root->data << " "; printLeafNodes(root->right);
return; }
}
Counting the number of nodes
static int count = 0;

int countnodes(struct node *root)


{
if(root != NULL)
{
countnodes(root->left);
count++;
countnodes(root->right);
}
return count;
}
Level wise Traversal
• First find the height of the tree
• Print the nodes corresponding to every level.

• Iterate all levels until the height, and print nodes at every level.
void print_tree_level_order(Node* root) {
int height = tree_height(root);
for (int i=0; i<height; i++) {
// Print the ith level
print_level(root, i);
}
}
Level wise Traversal
void print_level(Node* root, int level_no)
{ else {
// Prints the nodes in the tree // Make the auxiliary root node to
// having a level = level_no // be the left and right nodes for
// We have a auxiliary root node // the sub-trees and decrease level by 1,
// for printing the root of every sub-tree print_level(root->left, level_no - 1);
if (!root) print_level(root->right, level_no - 1);
return; }
if (level_no == 0) {
• }

// We are at the top of a sub-tree


// So print the auxiliary root node
printf("%d -> ", root->value);
}

You might also like