0% found this document useful (0 votes)
3 views

Tree Terminology.pptx

The document provides a comprehensive overview of tree data structures, including definitions, terminology, types, and traversal methods. It explains key concepts such as root, edges, nodes, and various types of trees like binary and general trees. Additionally, it covers tree representations, traversal algorithms, and the concept of mirror images in binary trees.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tree Terminology.pptx

The document provides a comprehensive overview of tree data structures, including definitions, terminology, types, and traversal methods. It explains key concepts such as root, edges, nodes, and various types of trees like binary and general trees. Additionally, it covers tree representations, traversal algorithms, and the concept of mirror images in binary trees.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Trees

Tree Definition

Tree is a non-linear data structure which organizes data


in hierarchical structure and this is a recursive definition.
Logic of Tree
Logic of Tree
Tree terminology
● 1.Root:
− The first node is called as Root Node.
− Every tree must have root node, there must be only one
root node.
− Root node doesn't have any parent.
2. Edge
● In a tree data structure, the connecting link between any two
nodes is called as EDGE. In a tree with 'N' number of nodes
there will be a maximum of 'N-1' number of edges.
3. Parent
● In a tree data structure, the node which is predecessor of any
node is called as PARENT NODE. In simple words, the node which
has branch from it to any other node is called as parent node.
Parent node can also be defined as "The node which has child /
children".
4. Child
● The node which has a link from its parent node is called as child node.
● In a tree, any parent node can have any number of child nodes.
● In a tree, all the nodes except root are child nodes.
5. Siblings
● The nodes with same parent are called as Sibling
nodes.
6. Leaf Node
● The node which does not have a child is called as LEAF Node.
● The leaf nodes are also called as External Nodes or 'Terminal'
node.
7. Internal Nodes
● An internal node is a node with atleast one child.
● Nodes other than leaf nodes are called as Internal Nodes.
● The root node is also said to be Internal Node if the tree has more
than one node. Internal nodes are also called as 'Non-Terminal'
nodes.
8. Degree
● In a tree data structure, the total number of
children of a node is called as DEGREE of that Node.
9. Level
● In a tree data structure, the root node is said to be at Level 0 and the
children of root node are at Level 1 and the children of the nodes which
are at Level 1 will be at Level 2 and so on...
● In a tree each step from top to bottom is called as a Level and the Level
count starts with '0' and incremented by one at each level (Step).
10. Height
● The total number of egdes from leaf node to a particular node in the
longest path is called as HEIGHT of that Node.
● In a tree, height of the root node is said to be height of the tree.
11. Depth
● In a tree data structure, the total number of egdes from root node to a
particular node is called as DEPTH of that Node.
12. Path
● In a tree data structure, the sequence of Nodes and Edges from one
node to another node is called as PATH between that two Nodes.
● Length of a Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.
13. Sub Tree
● Every child node will form a subtree on its parent
node.
Type of Trees
• General tree
• Binary tree
• Binary Search Tree
General Tree
• A general tree is a data structure in that each node can have infinite number of
children .
• In general tree, root has in-degree 0 and maximum out-degree n.
• Height of a general tree is the length of longest path from root to the leaf of tree.
Height(T) = {max(height(child1) , height(child2) , … height(child-n) ) +1}
Binary tree
• A Binary tree is a data structure in that each node has at most two
nodes left and right
• In binary tree, root has in-degree 0 and maximum out-degree 2.
• In binary tree, each node have in-degree one and maximum
out-degree 2.
• Height of a binary tree is : Height(T) = { max (Height(Left Child) ,
Height(Right Child) + 1}
Representation of Binary Tree
1. Array Representation
2. Linked List Representation.
Representation of Binary Tree
Struct node
{

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

1. Preorder traversal:-In this traversal method first process root


element, then left sub tree and then right sub tree.
Procedure:-
Step 1: Visit root node
Step 2: Visit left sub tree in preorder
Step 3: Visit right sub tree in preorder
2. Inorder 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);

// Find max(subtree_height) + 1 to get the height of the tree


return max(left_height, right_height) + 1;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy