Sei sulla pagina 1di 16

Binary tree

A tree in which every node can have a maximum


of two children is called as Binary Tree.
In a normal tree, every node can have any number
of children.

Binary tree is a special type of tree data structure


in which every node can have a maximum of 2
children. One is known as left child and the other
is known as right child
In a binary tree, every node can have either 0 children
or 1 child or 2 children but not more than 2 children.
Strictly Binary Tree
A binary tree in which every node has either two or zero
number of children is called Strictly Binary Tree

Strictly binary tree is also called as Full Binary Tree or Proper


Binary Tree
Perfect Binary Tree
A binary tree in which every internal node has exactly
two children and all leaf nodes are at same level is
called Perfect Binary Tree.
Complete Binary Tree
A Binary Tree is complete Binary Tree if all levels are
completely filled except possibly the last level and the
last level has all keys as left as possible

Binary Tree Traversals

Displaying (or) visiting order of nodes in a binary tree is


called as Binary Tree Traversal.

There are three types of binary tree traversals.


In - Order Traversal
Pre - Order Traversal
Post - Order Traversal
Traversing a binary tree is the process of visiting each
node in the tree exactly once in a systematic way.
Algorithm Inorder(tree)

1.Traverse the left subtree, i.e.,


call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e.,
call Inorder(right-subtree)

Inorder traversal for the above given figure is 4 2 5 1 3.


Algorithm Preorder(tree)

1. Visit the root.


2. Traverse the left subtree, i.e.,
call Preorder(left-subtree)
3. Traverse the right subtree, i.e.,
call Preorder(right-subtree)

Preorder traversal for the above given figure is 1 2 4 5 3.


Algorithm Postorder(tree)

1. Traverse the left subtree, i.e., call


Postorder(left-subtree)
2. Traverse the right subtree, i.e., call
Postorder(right-subtree)
3. Visit the root.

Postorder traversal for the above given figure is 4 5 2 3 1.


Task

In-orderI - D - J - B - F - A - G - K - C - H
Pre-orderA - B - D - I - J - F - C - G - K - H
Post-orderI - 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

• A binary expression tree is a specific application of a binary


tree to evaluate certain expressions

• The leaves of a binary expression tree are operands, such as


constants or variable names, and the other nodes contain
operators.
Rules for constructing Expression Tree
1.If the current token is a '(', add a new node as the left child
of the current node, and descend to the left child.

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.

4.If the current token is a ')', go to the parent of the current


node.
Example- ( 3 + ( 4 * 5 ) )

James Mathew Asst.professor Dept.CSE


CHRIST UNIVERSITY
Task:-((a+b)+c*(d+e)+f)*(g+h)

+
+

f h
+ g

+ *

a b c +

d e

Potrebbero piacerti anche