Professional Documents
Culture Documents
Binary Tree
A binary tree is a tree like structure that is rooted and in which each node has at most two children and
each child of a node is designated as its left or right child. In this kind of tree, the maximum degree of any
NEXT
node is at most 2.
A binary tree T is de ned as a nite set of elements such that
Any node N in a binary tree T has either 0, 1 or 2 successors. Level l of a binary tree T can have at most 2l
nodes. Creation of a binary tree Search an element into binary tree Delete an element from binary tree
if(!(*tree)) {
temp->data = val;
*tree = temp;
return;
insert(&(*tree)->left, val);
if(!(*tree)) {
return NULL;
if(val == (*tree)->data) {
return *tree;
search(&((*tree)->left), val);
search(&((*tree)->right), val);
if (tree) {
deltree(tree->left);
deltree(tree->right);
free(tree);
If every non-terminal node in a binary tree consist of non-empty left subtree and right subtree. In other
words, if any node of a binary tree has either 0 or 2 child nodes, then such tree is known as strictly binary
tree or extended binary tree or 2- tree. Complete Binary Tree: A complete binary tree is a tree in which
every level, except possibly the last, is completely lled. Tree Traversal: Three types of tree traversal are
given below Breadth First Traversal (BFT): The breadth rst traversal of a tree visits the nodes in the order
of their depth in the tree. BFT rst visits all the nodes at depth zero (i.e., root), then all the nodes at depth
NEXT
1 and so on. At each depth, the nodes are visited from left to right.
/* Recursive function to print the elements of a binary tree with preorder traversal*/
if (node != NULL)
printf("%d", node->data);
preorder(node->left);
preorder(node->right);
Inorder
Traverse the left subtree of R in inorder.
Process the root R.
Traverse the right subtree of R in inorder.
/* Recursive function to print the elements of a binary tree with inorder traversal*/
if (node != NULL)
{
NEXT
inorder(node->left);
printf("%d", node->data);
inorder(node->right);
Postorder
Traverse the left subtree of R in postorder.
Traverse the right subtree of R in postorder.
Process the root R.
/* Recursive function to print the elements of a binary tree with postorder traversal*/
if (node != NULL)
postorder(node->left);
postorder(node->right);
printf("%d", node->data);
Depth First Traversal (DFT): In DFT, one starts from root and explores as far as possible along each
Here, all leaves (D, E, F, G) are at depth 3 or level 2 and every parent is having exactly 2 children. An
expression tree is a binary tree which represents a binary arithmetic expression. All internal nodesNEXT in the
expression tree are operators, and leaf nodes are the operands. Expression tree will help in precedence
relation of operators. (2+3)*4 and 2+(3*4) expressions will have different expression trees.
Let a binary tree contain MAX, the maximum number of nodes possible for its height h. Then h=
log(MAX + 1) –1.
The height of the Binary Search Tree equals the number of links of the path from the root node to
the deepest node.
Number of internal/leaf nodes in a full binary tree of height h
2h leaves
2h -1 internal nodes
Expression Tree
Example1: Recursive function for size (number of nodes) of a binary tree
if (node == NULL)
return 0;
else
if (tempnode->left)
if (tempnode->right)
*front = *rear = 0;
return queue;
NEXT