Tree Operations
Tree Operations
int data;
struct node * left, right;
};
Binary Tree 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);
// 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;
• 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) {
• }