TREES
MODULE 3
1
PRELIMINARIES 2
• A tree is a collection of nodes.
• The collection can be empty, which is sometimes denoted as A.
• Otherwise, a tree consists of a distinguished node r, called the root,
and zero or more (sub)trees T1, T2, . . . , Tk, each of whose roots are
connected by a directed edge to r.
• The root of each subtree is said to be a child of r, and r is the parent
of each subtree root.
• Finite collection of data arranged in the hierarchical order.
3
ROOT
T1 T2 T3 T4 Tn
A
4
B C D E
F G H I J
K L
M
5
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one
root per tree and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node
called parent.
• Child − The node below a given node connected by its edge downward is
called its child node.
• Leaf − The node which does not have any child node is called the leaf
node.
• Subtree − Subtree represents the descendants of a node. 6
• Visiting − Visiting refers to checking the value of a node when control is
on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root
node is at level 0, then its next child node is at level 1, its grandchild is at
level 2, and so on.
• keys − Key represents a value of a node based on which a search
operation is to be carried out for a node.
7
8
•Height: For any node n, the height of the node n is the length of the longest path
from n to the leaf
•The height of the leaf is zero
•Depth: For any node n, the depth of n is the length of the unique path from root
to n
•The depth of the root is zero
•Length – Number of edges in the path
•Degree – The number of subtrees of a node is called a degree
•The degree of the tree is the maximum degree of any node in the tree
TYPES 9
• Trees are of following 6 types:
1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees
6. Tournament trees
GENERAL TREES
10
• 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.
• However, the number of sub-trees for any node may be variable.
• For example, a node can have 1 sub-tree, whereas some other
node can have 3 sub-trees.
10 11
General tree
20 30 40
FORESTS
12
• 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.
• every node of a tree is the root of some sub-tree. Therefore, all the sub-trees
immediately below a node form a forest.
• A forest can also be defined as an ordered set of zero or more general trees. While
a general tree must have a root, a forest on the other hand may be empty because
by definition it is a set, and sets can be empty.
• We can convert a forest into a tree by adding a single node as the root node of the
tree.
• Similarly, we can convert a general tree into a forest by deleting the root node of
the tree
13
Forest
BINARY TREE 14
• Binary tree is a tree in which no node have maximum of two
children, namely left child and the right child.
• A binary tree either is empty or consists of left and right subtrees
both of which are again binary trees.
• Binary trees are commonly used to implement binary search trees,
expression trees, tournament trees, and binary heaps.
• Maximum number of nodes at the level i of a binary tree is 2 i-1
• The types are,
• Full binary tree
• Complete binary tree
FULL BINARY TREE
15
• 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.
• In a Full Binary Tree, number of leaf nodes is the number of
internal nodes plus 1
L = I + 1
Where L = Number of leaf nodes, I = Number of internal nodes
• A full binary tree of height h has 2h+1 -1 nodes
COMPLETE BINARY TREE 16
• A complete binary tree is a binary tree in which every level, except
possibly the last, is completely filled, and all nodes are as far left as
possible.
• A complete binary tree of height h has between 2h and 2h+1-1 nodes.
• In the bottom level elements should be filled from the left to right.
• A full binary tree can be a complete binary tree but a complete
binary tree cannot be a full binary tree.
17
Complete
Binary Tree
BINARY TREE NODE DECLARATION 18
Struct TreeNode
{
int Element;
Struct TreeNode *Left;
Struct TreeNode *Right;
};
REPRESENTATION OF A BINARY TREE 19
• Linear representation
• Linked representation
LINEAR REPRESENTATION 20
• The elements are represented using arrays. For any element in
position i, the left child is in position 2i, the right child is in position
2i+1, and the parent is in position i/2.
B C A B C D E F G
1 2 3 4 5 6 7
D E F G
LINEAR REPRESENTATION IN MEMORY 21
LINKED REPRESENTATION 22
• The elements are represented using pointers, each node
in the linked representation has three fields namely,
• Pointer to the left subtree
• Data field
• Pointer to the right subtree
• In the leaf nodes both the pointers are assigned to null.
23
LINKED REPRESENTATION OF A BINARY TREE 24
BINARY SEARCH TREES 25
• 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.
• Binary Search Tree is a node-based binary tree data structure
which has the following properties:
• The left subtree of a node contains only nodes with keys lesser
than the node’s key.
• The right subtree of a node contains only nodes with keys
greater than the node’s key.
• The left and right subtree each must also be a binary search
tree.
26
BST
EXPRESSION TREES
• 27
Binary trees are widely used to store algebraic expressions. For
example, consider the algebraic expression given as:
Exp = (a – b) + (c * d)
This expression can be represented using a binary tree as shown
+
- *
a b c d
TOURNAMENT TREES 28
• In a tournament tree (also called a selection tree), each external
node represents a player and each internal node represents the
winner of the match played between the players represented by its
children nodes.
• 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
29
30
FOR PRACTICE
31
• Given an expression,
Exp = ((a + b) – (c * d)) % ((e ^f) / (g – h))
construct the corresponding binary tree.
• Given the binary tree, write down the
expression that it represents.
• Given the expression,
Exp = a + b / c * d – e,
construct the corresponding binary tree.
BINARY TREE OPERATIONS 32
• The rules for converting a general tree to a binary tree are given
below.
• Note that a general tree is converted into a binary tree and not a
binary search tree.
• Rule 1: Root of the binary tree = Root of the general tree
• Rule 2: Left child of a node = Leftmost child of the node in the
binary tree in the general tree
• Rule 3: Right child of a node in the binary tree = Right sibling of
the node in the general tree
33
• Now let us build the binary tree. 34
• Step 1: Node A is the root of the general tree, so it will also be
the root of the binary tree.
• Step 2: Left child of node A is the leftmost child of node A in the
general tree and right child of node A is the right sibling of the
node A in the general tree. Since node A has no right sibling in
the general tree, it has no right child in the binary tree.
• Step 3: Now process node B. Left child of B is E and its right child
is C (right sibling in general tree).
35
• Step 4: Now process node C. Left child of C is F (leftmost child) and its
right child is D (right sibling in general tree). 36
• Step 5: Now process node D. Left child of D is I (leftmost child). There
will be no right child of D because it has no right sibling in the general
tree.
• Step 6: Now process node I. There will be no left child of I in the binary
tree because I has no left child in the general tree. However, I has a
right sibling J, so it will be added as the right child of I.
• Step 7: Now process node J. Left child of J is K (leftmost child). There
will be no right child of J because it has no right sibling in the general
tree.
• Step 8: Now process all the unprocessed nodes (E, F, G, H, K) in the
same fashion, so the resultant binary tree can be given as follows.
TRAVERSING A BINARY TREE 37
• Traversing a binary tree is the process of visiting each node in the
tree exactly once in a systematic way.
• Unlike linear data structures in which the elements are traversed
sequentially, tree is a nonlinear 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.
• Pre order traversal
• In order traversal
• Post order traversal
PRE ORDER TRAVERSAL 38
• To traverse a non-empty binary tree in pre-order, the following
operations are performed recursively at each node. The algorithm
works by:
• 1. Visiting the root node,
• 2. Traversing the left sub-tree, and finally
• 3. Traversing the right sub-tree.
• The pre-order traversal of the tree is given as A, B, C. Root node
first, the left sub-tree next, and then the right sub-tree. Pre-order
traversal is also called as depth-first traversal.
• Pre-order algorithm is also known as the NLR traversal algorithm
(Node-Left-Right).
ALGORITHM FOR PRE-ORDER TRAVERSAL 39
IN-ORDER TRAVERSAL 40
• To traverse a non-empty binary tree in in-order, the following
operations are performed recursively at each node.
• The algorithm works by:
• 1. Traversing the left sub-tree,
• 2. Visiting the root node, and finally
• 3. Traversing the right sub-tree.
• The in-order traversal of the tree is given as B, A, and C. Left sub-tree
first, the root node next, and then the right sub-tree. In-order
traversal is also called as symmetric traversal.
• In-order algorithm is also known as the LNR traversal algorithm (Left-
Node-Right).
ALGORITHM FOR IN-ORDER 41
TRAVERSAL
POST-ORDER TRAVERSAL
42
• To traverse a non-empty binary tree in post-order, the following
operations are performed recursively at each node.
• The algorithm works by:
• 1. Traversing the left sub-tree,
• 2. Traversing the right sub-tree,
• 3. Visiting the root node.
• The post-order traversal of the tree is given as B, C, and A. Left sub-
tree first, the right sub-tree next, and finally the root node.
• In this algorithm, the left sub-tree is always traversed before the
right sub-tree and the root node.
• Post-order algorithm is also known as the LRN traversal algorithm
(Left-Right-Node).
ALGORITHM FOR POST-ORDER TRAVERSAL 43
LEVEL-ORDER TRAVERSAL
44
• 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.
CONSTRUCTING A BINARY TREE FROM
45
TRAVERSAL RESULTS
• 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.
• Example
• In–order traversal: D B E A F C G
• Pre–order traversal: A B D E C F G
• Follow the steps given below to construct the tree:
46
• Step 1 Use the pre-order sequence to determine the root node
of the tree. The first element would be the root node.
• 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 traversal
sequence and create its left and right sub-trees from the in-
order traversal sequence.
Pre–order traversal: A B D E C F G
In–order traversal: D B E A F C G 47
PRACTICE – CONSTRUCT THE BINARY 48
TREE
In–order Traversal: D B H E I A F J C G
Post order Traversal: D H I E B J F G C A
Pre-order Traversal: F B A D C E G I H
Post order Traversal: A C E D B H I G F
Pre-order Traversal: 1,2,4,8,9,10,11,5,3,6,7
In-order Traversal: 8,4,1,0,9,11,2,5,1,6,3,7
49
https://www.youtube.com/watch?v=LnHSOy7ctms
(link for constructing binary tree from given Preorder and post order)