Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
In-orderI - D - J - B - F - A - G - K - C - H
Pre-orderA - B - D - I - J - F - C - G - K - H
Post-orderI - J - D - F - B - K - G - H - C - A
#include<stdio.h> node *create()
struct node {
{ node *p;
int data; int x;
struct node *left; printf(“Enter -1 for no data\t");
struct node *right; scanf(“%d”,&x);
}node; if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
printf(“Enter left child of %d:”,x);
p->left=create();
printf(“Enter right child of %d:”,x);
p->right=create();
return p;
}
void preorder(node *t)
{
if(t!=NULL)
{
printf(“n%d”,t->data);
preorder(t->left);
preorder(t->right);
}
}
int main()
{
node *root;
root=create();
printf(“nThe preorder traversal of tree is:n”);
preorder(root);
return 0;
}
Expression Tree
2.If the current token is in the list ['+','-','/','*'], set the root
value of the current node to the operator represented by the
current token. Add a new node as the right child of the current
node and descend to the right child.
3.If the current token is a number, set the root value of the
current node to the number and return to the parent.
+
+
f h
+ g
+ *
a b c +
d e