DS Group M
DS Group M
TOPIC
ALGEBRAIC EXPRESSIOS, EXTENDED BINARY
TREES
SUBMITTED BY
PRITAM KUMAR GHOSH (EN22CS301756)
PUSHPRAJ SINGH CHOUHAN(EN22CS301774)
EXTENDED BINARY TREES
INTRODUCTION
Extended binary tree is a type of binary tree in which all the null sub tree of the
original tree are replaced with special nodes called external nodes whereas
other nodes are called internal nodes
Properties of External binary tree
1.The nodes from the original tree are internal nodes and the
special nodes are external nodes.
2.All external nodes are leaf nodes and the internal nodes are non-
leaf nodes.
3.Every internal node has exactly two children and every external
node is a leaf. It displays the result which is a complete binary
tree.
Application of extended binary
tree:
1.Calculate weighted path length: It is used to calculate total path length in
case of weighted tree.
2.Here, the sum of total weights is already calculated and stored in the external
nodes and thus makes it very easier to calculate the total path length of a tree
with given weights. The same technique can be used to update routing tables
in a network.
3.To convert binary tree in Complete binary tree: The above-given tree
having removed all the external nodes, is not a complete binary tree. To
introduce any tree as complete tree, external nodes are added onto it. Heap is
a great example of a complete binary tree and thus each binary tree can be
expressed as heap if external nodes are added to it.
C# program to make an extended binary tree
// C++ program to make an extended binary
tree
#include <bits/stdc++.h>
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to
// create a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Function for inorder
traversal
void traverse(Node* root)
{
if (root != NULL) {
traverse(root->left); Output:
cout << root->key << " -1 5 -1 2 -1 1 -1 3 -1 4 -1
";
traverse(root->right);
}
else {
// Making external
nodes
root = newNode(-1);
cout << root->key << "
";
}
}
// Driver code
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->right->right = newNode(4);
root->left->left =
newNode(5);
traverse(root);
return 0;
}
ALGEBRAIC EXPRESSIOS
Prefix expression notation requires that all operators precede the two operands
that they work on. Postfix, on the other hand, requires that its operators come
after the corresponding operands.
Examples of Infix, Prefix, and Postfix
An Expression with Parentheses
Additional Examples of Infix, Prefix,
and Postfix
Implementation of Expression tree in C Programming
language
// C program for expression tree implementation
#include <stdio.h>
#include <stdlib.h>
Scan the expression and according to the associativity and precedence find the operator which will be evaluated
at last.
In our example, the + operator will be evaluated in the last so keep it as the root node and divide the remaining
expression into left and right subtrees.
After solving the right and left subtree our final expression tree would become like the image given
below:
Thank you