Trees
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
•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
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.
L = I + 1
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
- *
a b c d
TOURNAMENT TREES 28
• 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.
• 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 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.
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
https://www.youtube.com/watch?v=LnHSOy7ctms
(link for constructing binary tree from given Preorder and post order)