0% found this document useful (0 votes)
9 views16 pages

DS Group M

Uploaded by

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

DS Group M

Uploaded by

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

DATA STRUCTURE

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

When you write an arithmetic expression such as B * C, the form of the


expression provides you with information so that you can interpret it correctly. In
this case we know that the variable B is being multiplied by the variable C since
the multiplication operator * appears between them in the expression. This type
of notation is referred to as infix since the operator is in between the two
operands that it is working on.

Different algebraic expression are:

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>

/* The below structure node is defined as a node of a binary tree consists


of left child and the right child, along with the pointer next which points to the next node */
struct node
{
char info ;
struct node* l ;
struct node* r ;
struct node* nxt ;
};
struct node *head=NULL;
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newnode(char data)
{
struct node* node = (struct node*) malloc ( sizeof ( struct node )
);
node->info = data ;
node->l = NULL ;
node->r = NULL ;
node->nxt = NULL ;
return ( node ) ;
}
void Inorder(struct node* node)
{
if ( node == NULL)
return ;
else
{
/* first recur on left child */
Inorder ( node->l ) ;

/* then print the data of node */


printf ( "%c " , node->info ) ;

/* now recur on right child */


Inorder ( node->r ) ;
}
}
void push ( struct node* x )
{
if ( head == NULL )
head = x ;
else
{
( x )->nxt = head ;
head = x ;
}
// struct node* temp ;
// while ( temp != NULL )
// {
// printf ( " %c " , temp->info ) ;
// temp = temp->nxt ;
// }
}
struct node* pop()
{
// Poping out the top most [pointed with head] element
struct node* n = head ;
head = head->nxt ;
return n ;
}
int main()
{
char t[] = { 'X' , 'Y' , 'Z' , '*' , '+' , 'W' , '/' } ;
int n = sizeof(t) / sizeof(t[0]) ;
int i ;
struct node *p , *q , *s ;
for ( i = 0 ; i < n ; i++ )
for ( i = 0 ; i < n ; i++ )
{
// if read character is operator then popping two
// other elements from stack and making a binary
// tree
if ( t[i] == '+' || t[i] == '-' || t[i] == '*' || t[i] == '/' || t[i] == '^' )
{
s = newnode ( t [ i ] ) ;
p = pop() ;
q = pop() ;
s->l = q ;
s->r = p;
push(s); }
else {
s = newnode ( t [ i ] ) ;
push ( s ) ; } }
printf ( " The Inorder Traversal of Expression Tree: " ) ;
Inorder ( s ) ;
return 0 ; }
Generation of Expression
Tree
Let’s understand the process of generation of an expression tree intuitively using the expression tree described in
the previous section.
Expression: A * B + C / D

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

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