Skip to main content
TREE
BY:
Samrin Ahmed Riya
ID: 011142021
Sanzida Akter
ID: 011142032
leaves
branches
root
root
nodes
leaves
branches
 Connected graph.Connected graph.
 Acyclic graph.
 A finite non-empty set of elements.
 CConsisting of nodes with a parent-child
relation.
Introduction of a Tree
B C
FD E G
A
Family Structures of a Tree
 Root-node without parent (A)
 Siblings-nodes share the same parent.
Internal node-node with at least one child (A, B, C, E)
External node (leaf): node without children (D,F,G, H, I)
Ancestors of a node: parent, grandparent, grand-
grandparent, etc.
Descendant of a node: child, grandchild, grand-grandchild,
etc.
Depth of a node: number of ancestors
Height of a tree: maximum depth of any node (3)
Degree of a node: the number of its children
Degree of a tree: the maximum number of its node.
 Subtree: tree consisting of a node and its descendants
Left Child Right Sibling
Representation
Data
Left Child Right
Sibling
A
B C D
IHGFE
J K L
 Preorder Sequence.
 Inorder Sequence.
 Postorder Sequence.
 Level order Sequence
Tree Traversal Technique
Tree Traversal Technique
Preorder Inorder Postorder Level order
Tree Traversal Technique
(CONT.)
Preorder Sequence:
 Visits the nodes of a tree in a systematic manner
 A node is visited before its descendants
 Application: print a structured document
Algorithm:
void preorder(node *root){
if(root!=NULL){
printf(“%c”,root->data);
if(root->left!=NULL)
preorder(root->left);
if(root->right!=NULL)
preorder(root->right);
}
}
Inorder Sequence:
 A node is visited after its left subtree and before its right sub
tree
Algorithm :
void inorder(node *root){
if(root!=NULL){
if(root->left!=NULL)
inorder(root->left);
printf(“%c”,root->data);
if(root->right!=NULL)
inorder(root->right);
}
}
Postorder Sequence:
 A node is visited after its descendants
 Application: compute space used by files in a
directory and its subdirectories
Algorithm :
void postorder(node *root){
if(root!=NULL){
if(root->left!=NULL)
postorder(root->left);
if(root->right!=NULL)
postorder(root->right);
printf(“%c”,root->data);
}
}
Level order Sequence:
 A node is traversed in a level by level
order.
Algorithm :
Queue<-root
while Queue !=Empty
v<-Queue
visit v
Visited Sequence<-v
if left(v) !=NULL then
Queue<-left(v)
if right(v) !=NULL then
Queue<-right(v)
endwhile
return Visited Sequence
 Each internal node has at most two children
(degree of two)
 The children of a node are an ordered pair
Alternative Recursive Defintion:
 A tree consisting of a single node, or
 A tree whose root has an ordered pair of children.
Applications:
 Arithmetic expressions.
 Decisions process searching.
 Searching.
A
B C
F GD E
H I
Strict Binary Tree:
 Each node exactly having two or zero child.
Full Binary Tree:
 Each node exactly having two node or zero
child and leaf node at the same level.
Complete Binary Tree:
 All leaf node is at the same height.
 No missing node in the sequence.
Strict Binary Tree
Full Binary Tree
A
B C
GE
I
D
H
F
Complete Binary Tree
A
B C
GF
Basic Operations:
 Inserting an element into tree.
 Deleting an element from tree.
 Searching for an element.
 Traversing the tree.
Auxiliary Operations:
 Finding size of tree.
 Calculating height of tree.
 Expressions trees are used in compiler.
 Huffman coding trees which are used in data
compression technique.
 Binary Search Tree (BST) supports insertion & deletion
of node with o(log n)
 Priority queue supports search and deletion of min or
max on a collection of items in a logarithmic times.
Method 1:
 height=max(level(i))
Method 2:
 height= log2(n)
Method 3:
 height(T)=max(height(Tl),height(Tr))+1
Algorithm :
int height(node *root){
if(root==NULL)
return -1;
else if(root->left==NULL && root->right==NULL)
return 0;
else
return max(height(root->left),height(root->right))+1;
}
Int max(int x,int y){
if(x>y)
return x;
else
return y;
}
https://en.wikipedia.org/wiki/Tree_%28data_stru cture%29
 http://www.slideshare.net/samsumitgupta/trees-data-structure
 http://www.i-programmer.info/babbages-bag/477-trees.html
 https://www.cse.iitb.ac.in/~cs101/pdfs/Trees.ppt
Tree-In Data Structure
Tree-In Data Structure