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

Unit-III Tree

The document provides an introduction to tree data structures, explaining their hierarchical nature and key terminology such as root, edge, parent, child, and leaf nodes. It discusses various types of trees, including binary trees and their properties, as well as tree traversal methods like pre-order, in-order, and post-order. Additionally, it highlights applications of trees in various fields, such as file directory structures and video game rendering.

Uploaded by

malviyat42
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)
2 views

Unit-III Tree

The document provides an introduction to tree data structures, explaining their hierarchical nature and key terminology such as root, edge, parent, child, and leaf nodes. It discusses various types of trees, including binary trees and their properties, as well as tree traversal methods like pre-order, in-order, and post-order. Additionally, it highlights applications of trees in various fields, such as file directory structures and video game rendering.

Uploaded by

malviyat42
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/ 47

Tree

1
So far we discussed Linear data structures like

stack

2
Introduction to trees
• So far we have discussed mainly linear data structures – arrays, lists, stacks
and queues.

• Now we will discuss a non-linear data structure called tree.

• Trees are mainly used to represent data containing a hierarchical


relationship between elements, for example, records, family trees and table
of contents.

• Consider a parent-child relationship


3
4
Tree Definition
• Definition (recursively): A tree is a finite set of one or more nodes

such that

• There is a specially designated node called root.

• The remaining nodes are partitioned into n>=0 disjoint set T1,…,Tn, where

each of these sets is a tree. T1,…,Tn are called the subtrees of the root.

• Every node in the tree is the root of some subtree


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
⚫ Node below a given node connected by its edge downward is called its child
node.
⚫ In a tree, any parent node can have any number of 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
⚫ Degree of a Node : The degree of a node of a tree is the number of subtrees having this node as
a root. In other words, the degree is the number of descendants of a node. If the degree is zero,
it is called a terminal or leaf node of a tree.
⚫ Degree of a Tree : The degree of a tree is defined as the maximum of degree of the nodes of the
tree, that is, degree of tree = max (degree(node i) for I = 1 to n)
⚫ Degree of the below tree is 3.
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
⚫ It is the sequence of consecutive edges from source node to destination
node.
⚫ In below example the path A - B - E - J has length 3.
13. Sub Tree
⚫ Every child node will form a subtree on its parent
node.
19
Application
• Directory structure of a file store
• Structure of an arithmetic expressions
• Used in almost every 3D video game to determine what objects need to be
rendered.
• Used in almost every high-bandwidth router for storingrouter-tables.
• Used in compression algorithms, such as those used by the .jpeg and .mp3
file-formats.

20
Type of Trees
• General tree
• Binary tree
• Binary Search Tree
• AVL Tree
• B Tree
• B+ Tree
• Etc…….
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.
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.
Binary Tree Properties
• The maximum number of nodes at level ‘l’ of a binary tree is 2l
• The Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1.
(if root is at height 1)
• The Maximum number of nodes in a binary tree of height ‘h’ is 2h+1 – 1.
(if root is at height 0)
• In a Binary Tree with N nodes, minimum possible height
or the minimum number of levels is Log2(N+1).
• A Binary Tree with L leaves has at least | Log2L |+ 1 levels.
• In Binary tree where every node has 0 or 2 children, the number of leaf
nodes is always one more than nodes with two children.
• In a non empty binary tree, if n is the total number of nodes and e is
the total number of edges, then e = n-1
24
Representation of Binary Tree
1. Array Representation
2. Linked List Representation.
Array Representation

If a complete binary tree with n nodes (depth = log n + 1) is


represented sequentially, then for any node with index i, 1<=i<=n,
we have:
• parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no
parent.
• left_child(i) is at 2i if 2i<=n. If 2i>n, then i has no left child.
• right_child(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no
right child.

26
[1] A
Array Representation [2] B
[3] C
A [4] D
A [1] [5] E
[2] B
[3] -- [6] F
B C [7] G
[4]
-- A [8] H
[5]
C -- [9] I
[6]
[7] --
D B C
D [8]
[9] --
. .
E D E F G
[16] E

H I
Linked Representation
Struct node
{
int data;
struct node * left;
struct node *right;
};
Binary Tree Types

1. Complete Binary Tree


2. Full Binary Tree
3. Skewed Binary Tree
4. Strictly Binary Tree
5. Expression Binary tree
6. Extended Binary Tree
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 children. Every level is completely filled up.
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 nod 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.
Extended Binary Tree

Extended binary tree is a type of binary tree in which all the null
sub tree of the original tree are replaced with special nodes called
external nodes whereas other nodes are called internal nodes
Operations on Binary Tree

• Searching an existing node.


• Inserting a new node.
• Deleting an existing node.
• Traversing the tree.
• Preorder
• Inorder
• Postorder

37
Tree traversal
• Traversal is a process to visit all the nodes of a tree and may print their
values too.

• All nodes are connected via edges (links) we always start from the root
(head) node.

• There are three ways which we use to traverse atree


• In-order Traversal
• Pre-order Traversal
• Post-order Traversal

• Generally we traverse a tree to search or locate given item or key in the tree or
to print all the values it contains.
38
Pre-order, In-order, Post-order
• Pre-order
<root><left><right>

• In-order
<left><root><right>

• Post-order
<left><right><root>
39
Pre-order Traversal
The preorder traversal of a non empty binary tree is defined as follows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder

40
Pre-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Preorder(Node *root)
{
if (root==NULL) return;
printf (“%c”, root->data);
Preorder(root->left);
Preorder(root->right);
}

41
In-order traversal
• The in-order traversal of a nonempty binary tree is defined asfollows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder

• The in-order traversal output


of the given tree is
HD IBEAFC G

42
In-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Inorder(Node *root)
{
if (root==NULL) return;
Inorder(root->left);
printf (“%c”, root->data);
Inorder(root->right);
}

43
Post-order traversal
• The post-order traversal of a nonempty binary tree is defined
asfollows:
• Traversethe left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node

• The post-order traversal


output of the given tree is
HIDEBFGCA

44
Post-order Pseudocode
struct Node{
char data;
Node *left;
Node *right;
}
void Postorder(Node *root)
{
if (root==NULL) return;
Postorder(root->left);
Postorder(root->right);
printf (“%c”, root->data);
}

45
Level-order Traversal

20
1
8 22
23
4 12
45
10 14

46
Level-order Traversal using Queue

•Create an empty queue q and push root in q.


•Run While loop until q is not empty.
• Initialize temp_node = q.front() and print temp_node->data.
• Push temp_node’s children i.e. temp_node -> left then temp_node
-> right to q
• Pop front node from q.

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