Module 6-BT
Module 6-BT
• Root node:
• Sub Trees:
• Leaf node:
• Path: A sequence of consecutive edges
• Ancestor node: Any predecessor of a node on a path from Root node to that node
• Descendent Node: Any successor node on a path from a node to a leaf node.
• Level No: Root node at level 0. Level(child node)=1+Level(Parent node)
• Degree of a node: No. of children of a node.
1 ROOT NODE
• In-degree: No of edges arriving at that node
•
T T
Out-degree: No of edges leaving that node. 1
2 3
2
4
5 6 7
8 9 10 11 12
Trees
• Types of Trees
General Trees
Forests
Binary Trees
Expression Trees
Tournament Trees
General Trees
• General trees are data structures 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.
• 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.
Forests
• A forest is a disjoint union of trees. A set of disjoint trees (or forest) 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.
Binary Trees
• A binary tree is a data structure which is 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.
• 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.
T1 T2
2 3
• Depth: The depth of a node N is given as the length of the path from the root to the
node N. The depth of the root node is zero.
• Height: It is the total number of nodes on the path from the root node to the deepest
node in the tree. A tree with only a root node has a height of 1.
• A binary tree of height h has at least h nodes and at most 2h – 1 nodes. This is because
every level will have at least one node and can have at most 2 nodes.
• The height of a binary tree with n nodes is at least log2(n+1) and at most n.
Binary Trees - Key Terms
• Similar binary trees: Given two binary trees T and T’ are said to be similar if both these
trees have the same structure.
TREE T
TREE T”
A F
B C G H
D I
E J
• Copies of binary trees: Two binary trees T and T’ are said to be copies if they have similar
structure and same content at the corresponding nodes.
TREE T
TREE T”
A A
B C B C
E
D E D
Complete Binary Trees
• A complete binary tree is a binary tree which satisfies two properties.
• First, in a complete binary tree, every level, except possibly the last, is completely
filled.
• Second, all nodes appear as far to the left as possible
• In a complete binary tree Tn, there are exactly n nodes and level r of T can have at most
2r nodes.
• The formula to find the parent, left child and right child can be given as:
• If K is a parent node, then its left child can be calculated as 1
For example, the children of node 4 are 8 (2*4) and 9 (2* 4 + 1).
4 7
• Similarly, the parent of node K can be calculated as | K/2 |. 5 6
8 9 1 1 1 1
0 1 2 3
Extended Binary Trees
• A binary tree T is said to be an extended binary tree (or a 2-tree) if each node in
the tree has either no child or exactly two children.
• In an extended binary tree, nodes that have two children are called internal nodes
and nodes that have no child or zero children are called external nodes. In the
figure internal nodes are represented using a circle and 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(represented
by circles) and the new nodes added are called the external nodes(represented by
squares).
int data; 4 5 6 7
};
Linked Representation of Binary Trees
Sequential Representation of Binary Trees
• Sequential representation of trees is done using a one dimensional array. Though,
it is the simplest technique for memory representation, it is very inefficient as it
requires a lot of memory space.
• A sequential binary tree follows the rules given below:
• One dimensional array called TREE is used.
• The root of the tree will be stored in the first location i.e , TREE[1].
• The children of a node K will be stored in location (2*K) and (2*K+1).
• The maximum size of the array TREE is given as (2h-1), where h is the height of
the tree.
• An empty tree or sub-tree is specified using NULL. If TREE[1] = NULL, then the
tree is empty.
Sequential Representation of Binary Trees
Binary Search Trees
• Also known as ordered binary tree.
• It is a variant of binary tree in which nodes are arranged in an
order.
Expression Trees
• Binary trees are widely used to store algebraic expressions. For
example, consider the algebraic expression Exp given as:
Exp = (a – b ) + ( c * d)
• This expression can be represented using a binary tree as shown
in figure
+
- *
a b c d
Tournament Trees
• Tournament tree is a complete binary tree with n external nodes and n-1
internal nodes. The external nodes represent the players and internal nodes
represent the winner of the match between the two players.
Step 1) We will first draw min Step 2) Now we will store losers of
winner tree for given data. the match in each internal nodes.
Application of Tournament Tree
• It is used for finding the smallest and largest element in the array.
• It is used for sorting purpose.
• Tournament tree may also be used in M-way merges.
Creating a Binary Tree from a General Tree
Example:
General Tree
Binary Tree
Traversing a Binary Tree
• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.
• There are three different algorithms for tree traversals, which differ in
the order in which the nodes are visited.
D E
A, B, D, C, E, F, G, H and I
F
H I
Pre-order Algorithm: DLR
Pre-order Examples: DLR
In-order Algorithm: LDR
• To traverse a non-empty binary tree in in-order, the following operations are
performed recursively at each node.
• The algorithm starts with the root node of the tree and continues by,
Traversing the left subtree
Visiting the root node
A
Traversing the right subtree
B C
D E
B, D, A, E, H, G, I, F and C
G
H I
In-order Algorithm: LDR
In-order Examples: LDR
Post-order Algorithm: LRD
• To traverse a non-empty binary tree in post-order, the following
operations are performed recursively at each node.
• The algorithm starts with the root node of the tree and continues
by,
Traversing the left subtree
Traversing the right subtree A
D E
D, B, H, I, G, F, E, C and A G
H I
Post-order Algorithm: LRD
Post-order Examples: LRD
Level-order Traversal
• All the nodes at a level are accessed before going to next level.
• This algorithm is also called Breadth First Traversal Algorithm.
Constructing a Binary tree from Traversal results
• The pre-order or post-order sequence can be used to determine the root node and in-
order traversal is used to determine left and right child nodes.
Step1: Use the pre-order sequence to determine the root node of the tree.
Step2: Elements on the left side of the root node in in-order traversal
sequence form the left subtree of the root node . Similarly elements on
RHS of the root node form right subtree of root node.
Step 3: Recursively select each element from preorder traversal sequence
and create its left and right subtrees from inorder traversal sequence.
• Example
Constructing a Binary tree from Traversal results
Ex : Inorder: INFORMATION
Postorder: INOFMAINOTR
• Trees are often used for implementing other types of data structures like hash tables, sets,
and maps.
• Another variation of tree, B-trees are used to store tree structures on disc. They are used
to index a large number of records.
• B-trees are also used for secondary indexes in databases, where the index facilitates a
select operation to answer some range criteria.