0% found this document useful (0 votes)
45 views39 pages

Module 4.14.2

Uploaded by

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

Module 4.14.2

Uploaded by

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

Module 4

Trees
TREES
• A tree is recursively defined as a set of one or
more nodes
• One node is designated as the root of the tree
and all the remaining nodes can be partitioned
into non-empty sets each of which is a sub-tree
of the root.
• Figure shows a tree where node A is the root
node; nodes B, C, and D are children of the root
node and form sub-trees of the tree rooted at
node A.
Basic Terminology
• Root node - The root node R is the topmost node in
the tree. If R = NULL, then it means the tree is
empty.
• Sub-trees - If the root node R is not NULL, then the
trees T1 , T2 , and T3 are called the sub-trees of R.
• Leaf node - A node that has no children is called
the leaf node or the terminal node.
• Path - A sequence of consecutive edges is called a
Root – A
path.
Sub Trees – T1, T2, T3
• Ancestor node - An ancestor of a node is any Leaf Nodes – E, F, J, K, H, I
predecessor node on the path from root to that Path – A->J - A->C->G->J
node. The root node does not have any ancestors. Ancestors of K – A, C, G
• Descendant node - A descendant node is any successor
node on any path from the node to a leaf node. Leaf
nodes do not have any descendants.
• Level number - Every node in the tree is assigned a level
number in such a way that the root node is at level 0,
children of the root node are at level number 1.
• Thus, every node is at one level higher than its parent. So,
all child nodes have a level number given by parent’s Descendant of C – G, J, K
level number + 1. Level of E – 2, J - 3
Degree of B – 2, C – 1
• Degree - of a node is equal to the number of children that
In-degree of A – 0, B – 1, K – 1
a node has. The degree of a leaf node is zero. Out- degree of A – 3, C – 1, G – 2
• In-degree - of a node is the number of edges arriving at
that node.
• Out-degree - of a node is the number of edges leaving
that node.
TYPES OF TREES
Trees are of following 6 types:
1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees
6. Tournament trees
1. General Trees
• General trees are data structure that store
elements hierarchically.
• The top node of a tree is the root node and
each node, except the root, has a parent.
• A node in a general tree (except the leaf
nodes) may have zero or more sub-trees.
• General trees which have 3 sub-trees per node
are called ternary trees.
• It must have atleast one node - Root.
2. Forests
• A forest is a disjoint union of trees.
• A set of disjoint trees (or forests) is
obtained by deleting the root and the edges
connecting the root node to nodes at level 1
• A forest can also be defined as an ordered
set of zero or more general trees.
• A forest may be empty because by
definition it is a set, and sets can be empty
3. Binary Trees
• A binary tree is a data structure that defined as a
collection of elements called nodes.
• In a binary tree, the topmost element is called the
root node, and each node has 0, 1, or at the most
2 children.
• A node that has zero children is called a leaf node
or a terminal node.
• Every node contains a data element, a left pointer
which points to the left child, and a right pointer
which points to the right child.
• The root element is pointed by a root pointer.
• In the figure, R is the root node and the two trees T1 and T2 are called the left and right sub-trees
of R. T1 is said to be the left successor of R. and T2 is called the right successor of R.
• The left sub-tree of the root node consists of the nodes: 2, 4, 5, 8, and 9.
• The right sub-tree of the root node consists of nodes: 3, 6, 7, 10, 11, and 12.
• In the tree, root node 1 has two successors: 2 and 3. Node 2 has two successor nodes: 4 and 5.
• Node 4 has two successors: 8 and 9. Node 5 has no successor.
• Node 3 has two successor nodes: 6 and 7. Node 6 has two successors: 10 and 11.
• Finally, node 7 has only one successor: 12.
• A binary tree is recursive by definition as every node in the
tree contains a left sub-tree and a right sub-tree.
• Even the terminal nodes contain an empty left sub-tree and an
empty right sub-tree.
• In Fig. nodes 5, 8, 9, 10, 11, and 12 have no successors and thus
said to have empty sub-trees.
Terminologies
• Parent: If N is any node in T that has left successor S1 and right
successor S2, then N is called the parent of S1 and S2.
Correspondingly, S1 and S2 are called the left child and the right
child of N. Every node other than the root node has a parent.
• Level number: Every node in the binary tree is assigned a level
number . The root node is defined to be at level 0. The left and the
right child of the root node have a level number 1.
Every node is at one level higher than its parents. So all child nodes
are defined to have level number as parent's level number + 1.
• Degree of a node: It is equal to the number of children that a
node has. The degree of a leaf node is zero.
For example, in the tree, degree of node 4 is 2, degree of node 5 is
zero and degree of node 7 is 1.
• Sibling: All nodes that are at the same level and share the same
parent are called siblings (brothers).
For example, nodes 2 and 3; nodes 4 and 5; nodes 6 and 7; nodes 8
and 9; and nodes 10 and 11 are siblings.

• Similar binary trees - Two binary trees T and T’ are said to be


similar if both have the same structure.
• Copies -Two binary trees T and T’ are said to be copies if they have
similar structure and if they have same content at the
corresponding nodes.
• Edge: It is the line connecting a node N to any of its
successors. depth(1)=0

depth(2)=1
A binary tree of n nodes has exactly n – 1 edges because depth(3)=1

every node except the root node is connected to its parent via depth(5),(6/)&(7)=2
depth(4)=2
an edge.
• Depth of a node: It is the total number of edges from the
root node to that node. The depth of the root node is zero.
depth(8)=3 depth(9)=3 depth(10)(11)&(12)=3

height(1)=3

• Height of a node: Number of edges in the longest path from height(2)=2 height(3)=2

that node to leaf node.


• Height of a tree: It is the total number of nodes on the height(4)=1 height(5)=0
height(6)&(7)=1

longest path from the root node to leaf node in the tree. A
tree with only a root node has a height of 1.
height(8)=0 height(9)=0 height(10)(11)&(12)=0
Find the following
1. Height of the tree T A

2. Level(H), level(C), level(K)


B C D
3. Degree of E
4. Height of C
G H I J
5. The longest path E F

6. Parent(M), Sibling(I)
and Child(B) K L M
Properties of Binary tree
• In any binary tree, the maximum no. of nodes on level l is 2l , where l>=0.

• Maximum no. of nodes possible in a binary tree of height h is 2h-1.

• Minimum no. of nodes possible in a binary tree of height h is h.


Complete Binary Trees
A complete binary tree is a binary tree that satisfies two properties.
1. Every level, except the last, is completely filled (should have the maximum no. of
possible nodes).
2. All nodes appear as far left as possible.
Level Nodes

0 1

1 2

2 4

3 6 (last
Level)
Extended Binary Trees (2-tree)
• A binary tree T is said to be an extended binary
tree if each node in the tree has either no child
or exactly two children.
• In an extended binary tree, nodes having two
children are called internal nodes and nodes
having no children are called external nodes.
• The internal nodes are represented using
circles and the external nodes are represented
using squares.
• To convert a binary tree into an extended tree,
every empty sub-tree is replaced by a new node.
• The original nodes in the tree are the internal
nodes, and the new nodes added are called the
external nodes.
Representation of binary tree in the Memory
• In the computer’s memory, a binary tree can be maintained either by using a linked
representation or by using a sequential representation.

Linked representation of binary trees


• In the linked representation of a binary tree, every node will have three parts: the data
element, a pointer to the left node, and a pointer to the right node.
struct node {
struct node *left;
int data;
struct node *right;
};
• Every binary tree has a pointer ROOT, which points to the
root element the tree. If ROOT = NULL, then the tree is
empty.
• Consider the binary tree given. The schematic diagram of
the linked representation of the binary tree is shown below

• In Fig. the left position is used to store the address of the left child of the node.
• The middle position is used to store the data.
• Finally, the right position is used to store the address of the right child of the node.
• Empty sub-trees are represented using X (meaning NULL).
Tree is represented in the main memory using
a linked list
Sequential representation of binary trees
• It is done using single or one-dimensional arrays.
• Though it is the simplest technique for memory representation, it is inefficient as it requires a lot of memory
space.
• A sequential binary tree follows the following rules:
 A one-dimensional array, called TREE, is used to store the
elements of tree.
 The root of the tree will be stored in the first location. That is,
TREE[1] will store the data of the root element.
 The children of a node stored in location K will be stored in
locations (2 × K) and (2 × K+1).
 The maximum size of the array TREE is given as (2^h–1), where
h is the height of the tree.
• Figure shows a binary tree and its
 An empty tree or sub-tree is specified using NULL. If TREE[1]=
corresponding sequential representation.
NULL, then the tree is empty. The tree has 11 nodes and its height is 4.
4. Binary Search Trees
A binary search tree, also known as an ordered binary tree, is a variant of
binary tree in which the nodes are arranged in an order.

5. Expression Trees
Binary trees are widely used to store algebraic expressions. For example,
consider the algebraic expression given as:
Exp = (a – b) + (c * d)
• A+B*C/D%E
Tournament Trees
• We all know what is tournament……Lets consider the game ‘chess’ there ‘n’ number of players
are ready to play chess. To declare the winner couple of matches are played among them.

• In every match of round 1, a number of matches are played in which two players play the game
against each other. The number of matches that will be played in round 1 will depend on the
number of players. For example, if there are 8 players participating in a chess tournament, then
4 matches will be played in round 1.

• round 2, the winners of round 1 will play against each other.

• Similarly, in round 3, the winners of round 2 will play against each other.

• and the person who wins round 3 is declared the winner. Tournament trees are used to
represent this concept.
• Tournament tree also called as selection tree/winner tree.
• In a tournament tree each external node represents a player and each
internal node represents the winner.
• These tournament trees are also called winner trees because they are
being used to record the winner at each level. We can also have a loser tree
that records the loser at each level.

Consider the tournament tree given in Fig. 9.14. There are 8 players
in total whose names are represented using a, b, c, d, e, f, g, and h.
In round 1, a and b; c and d; e and f; and finally g and h play against
each other. In round 2, the winners of round 1, that is, a, d, e, and g
play against each other. In round 3, the winners of round 2, a and e
play against each other. Whosoever wins is declared the winner. In
the tree, the root node a specifies the winner.
TRAVERSING A BINARY TREE
• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.
• Tree is a non-linear data structure in which the elements can be traversed in
many different ways.
• There are different algorithms for tree traversals.
• These algorithms differ in the order in which the nodes are visited.
1. Preorder Traversal (R’LR)
• R’ – Root
2. Inorder Traversal(LR’R)
• L – Left
3. Postorder Traversal (LRR’) • R - Right
Pre-order Traversal
To traverse a non-empty binary tree in pre-order, the following operations are
performed recursively at each node.
1. Visiting the root node
2. Traversing the left sub-tree in preorder
3. Traversing the right sub-tree in preorder
• Pre-order traversal is also called as depth-first traversal.
• Pre-order algorithm is also known as the NLR traversal algorithm (Node-
Left-Right)
• Pre-order traversal algorithms are used to extract a prefix notation from an
expression tree
Pre-Order Traversal
ABDGHLECF IJ K ABDCEFGHI
Application - Infix to prefix conversion
• (a – b) + (c * d) ------Infix----- ((a + b) – (c * d)) % ((f ^g) / (h – i))

•+–ab*cd -------Prefix------ %–+ab*cd/^fg–hi


In-order Traversal
To traverse a non-empty binary tree in-order, the following operations are
performed recursively at each node.
1. Traversing the left sub-tree in inorder
2. Visiting the root node
3. Traversing the right sub-tree in inorder
• In-order traversal is also called as symmetric traversal.
• In-order algorithm is also known as the LNR traversal algorithm (Left-Node-
Right).
• Usually it is used to display the elements of a binary search tree.
• Here, all the elements with a value lower than a given value are accessed before
the elements with a higher value.
In-Order Traversal
GDHLBEACI F KJ B DAE H GI F C
Post-order Traversal
• To traverse a non-empty binary tree in post-order, the following operations are
performed recursively at each node.
1. Traversing the left sub-tree in post order
2. Traversing the right sub-tree in post order
3. Visiting the root node.

• Post-order algorithm is also known as the LRN traversal algorithm (Left-Right-


Node).
• Post-order traversals are used to extract postfix notation from an expression tree.
Post-Order Traversal
GLHDEBI KJ FCA DBHI GF ECA
Application - Infix to postfix conversion
• (a – b) + (c * d) ------Infix----- ((a + b) – (c * d)) % ((f ^g) / (h – i))

• a b – c d *+ -------Postfix------ ab+cd*-fg^hi-/%
Traverse – Inorder, Preorder and Post order

Inorder – 10, 12, 32, 34, 39, 45, 54, 56, 67, 78, 81, 89

Preorder – 45, 39, 12, 10, 34, 32, 56, 54, 78, 67, 89, 81

Postorder – 10, 32, 34, 12, 39, 54, 67, 81, 89, 78, 56, 45
Level-order Traversal
• In level-order traversal, all the nodes at a level are accessed before going to the
next level.
• This algorithm is also called as the breadth-first traversal algorithm.
ABCDEFGHI JLK ABCDEFGHI
Constructing a Binary Tree from Traversal Results

• We can construct a binary tree if we are given at least two traversal


results.

• The first traversal must be the in-order traversal and the second can be
either pre-order or post-order traversal.

• The in-order traversal result will be used to determine the left and the right
child nodes

• and the pre-order/post-order can be used to determine the root node.


Follow the steps to construct the tree:
• Step 1 Use the pre-order/post-order sequence to determine the root node of the tree.
The first element would be the root node if pre-order, last element would be the root
node if post-order.

• Step 2 Elements on the left side of the root node in the in-order traversal sequence
form the left sub-tree of the root node. Similarly, elements on the right side of the root
node in the in-order traversal sequence form the right sub-tree of the root node.

• Step 3 Recursively select each element from pre-order /post-order traversal


sequence and create its left and right sub-trees from the in-order traversal sequence
Example 1:
In–order Traversal: D B E A F C G Pre–order Traversal: A B D E C F G
Example 2:  Now consider the in-order traversal
and post-order traversal sequences .
 Before constructing the binary tree,
remember that in post-order
In–order Traversal: D B H E I A F J C G traversal the root node is the last
node. Rest of the steps will be the
Post order Traversal: D H I E B J F G C A same.
A A
A A
A

B FJC B B
B FJ G C C
DBH FJ CG
EI CG
D E D E FJ G F G
D E
D HEI

I H I J
H H I

You might also like