Tree Terminology.pptx
Tree Terminology.pptx
Tree Definition
int data;
struct node * left, right;
};
Array Representation
1. To represent a tree in one dimensional array nodes are marked sequentially from left to
right start with root node.
2. First array location can be used to store no of nodes in a tree.
Linked Representation
1. This type of representation is more efficient as compared to array.
2. Left and right are pointer type fields left holds address of left child and right holds address of right child.
3. Struct node
{
int data;
struct node * left,*right;
};
Binary Tree Types
1. Extended Binary Tree
2. Complete Binary Tree
3. Full Binary Tree
4. Skewed Binary Tree
5. Strictly Binary Tree
6. Expression Binary tree
Extended Binary Tree
An extended binary tree is a transformation of any binary tree into a complete
binary tree. This transformation consists of replacing every null subtree of the
original tree with “special nodes” or “failure nodes”. The nodes from the
original tree are then internal nodes, while the “special nodes” are external
nodes.
Complete Binary Tree
A complete binary tree is a tree in which
1. All leaf nodes are at n or n-1 level
2. Levels are filled from left to right
Full Binary Tree
A full binary tree (sometimes proper binary tree or 2-tree) is
a tree in which every node other than the leaves has two children
or no childeren. Every level is completely filled up.
No of nodes= 2h+1 -1
Skewed Binary Tree
A binary tree is said to be Skewed Binary Tree if every node in the tree contains either
only left or only right sub tree. If the node contains only left sub tree then it is
called left-skewed binary tree and if the tree contains only right sub tree then it is
called right-skewed binary tree.
Strictly Binary Tree
A node will have either two children or no child at all.
Expression Binary tree
• Expression trees are a special kind of binary tree used to evaluate certain expressions.
• Two common types of expressions that a binary expression tree can represent are
algebraic and boolean.
• These trees can represent expressions that contain both unary and binary operators.
• The leaves of a binary expression tree are operands, such as constants or variable names,
and the other nodes contain operators.
• Expression tree are used in most compilers.
Binary Tree Traversal
In this traversal method first process left element, then root element
and then the right element.
Procedure:-
Step 1: Visit left sub tree in inorder
Step 2: Visit root node
Step 3: Visit right sub tree in inorder
3. Postorder traversal:-
In this traversal first visit / process left sub tree, then right sub tree and
then the root element.
Procedure:-
Step 1: Visit left sub tree in postorder
Step 2: Visit right sub tree in postorder
Step 3: Visit root node
Arithmetic Expression Using BT
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/ABCDE
* D prefix expression
postorder traversal
AB/C*D*E+
/ C
postfix expression
level order traversal
A B +*E*D/CAB
CHAPTER 5 36
Inorder Traversal (recursive version)
void inorder(tree_pointer ptr)
/* inorder tree traversal */
{
A/B*C*D+E
if (ptr) {
inorder(ptr->left_child);
printf(“%d”, ptr->data);
indorder(ptr->right_child);
}
}
CHAPTER 5 37
Preorder Traversal (recursive
version)
void preorder(tree_pointer ptr)
/* preorder tree traversal */
{
+**/ABCDE
if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->left_child);
predorder(ptr->right_child);
}
}
CHAPTER 5 38
Postorder Traversal (recursive
version)
void postorder(tree_pointer ptr)
/* postorder tree traversal */
{
AB/C*D*E+
if (ptr) {
postorder(ptr->left_child);
postdorder(ptr->right_child);
printf(“%d”, ptr->data);
}
}
CHAPTER 5 39
Iterative Inorder Traversal
(using stack)
void iter_inorder(tree_pointer node)
{
int top= -1; /* initialize stack */
tree_pointer stack[MAX_STACK_SIZE];
for (;;) {
for (; node; node=node->left_child)
add(&top, node);/* add to stack */
node= delete(&top);
/* delete from stack */
if (!node) break; /* empty stack */
printf(“%D”, node->data);
node = node->right_child;
}
} O(n) CHAPTER 5 40
Mirror Image
• Mirror image of a binary tree is another binary tree which can be
created by swapping left child and right child at each node of a tree.
So, to find the mirror image of a binary tree, we just have to swap the
left child and right child of each node in the binary tree.
Mirror Image
• Write a struct node.
• Create the binary tree with dummy data.
• Write a recursive function to find the mirror of the given binary tree.
• Recursively call the function with left and right nodes.
• Swap the left node data with the right node data.
• Print the tree.
Mirror Image
Height
• The height of a Binary Tree is defined as the maximum depth of any
leaf node from the root node. That is, it is the length of the longest
path from the root node to any leaf node.
Finding Height of Binary Tree
// Find height of a tree, defined by the root node
int tree_height (Node* root) {
if (root == NULL)
return 0;
else {
// Find the height of left, right subtrees
left_height = tree_height(root->left);
right_height = tree_height(root->right);