Chap 3 Trees-1
Chap 3 Trees-1
• 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 right child of the root
node have a level number 1. Similarly, every node is at one level higher
than its parents.
• Parent: If N has child node N1, then N is called parent node of N1.
• Sibling: all nodes that are at the same level and share the same parent are
called siblings (brothers)
• Ancestor node: any predecessor node on the path from root to the node
• Descendant node: any successor node on any path from the node to leaf
Tree representation
• 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 tree such that every node has at most two child
nodes.
• 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.
• If root = NULL, then it means the tree is empty.
1 ROOT NODE
T1 T2
2 3
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 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
2 * K and its right child can be calculated as 2 * K + 1. 1
For example, the children of node 4 are 8 (2*4) and 9 (2* 4 + 1). 2 3
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 and the new
nodes added are called the external nodes.
Binary tree
Binary Tree Representation
•Sequential(Arrays) representation
•Linked representation
153
Sequential Representation of Binary Tree
• Sequential representation of trees is done using a single or 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. That is, TREE[1] will store
the data of the root element.
• 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. 20
15 35
12 39
17 21
36 45
16 18
Sequential Representation of Binary Tree
This representation uses only a single linear
array tree as follows:
i)The root of the tree is stored in tree[0].
ii)if a node occupies tree[i],then its left child is
stored in tree[2*i+1],its right child is stored in
tree[2*i+2],and the parent is stored in tree[(i-
1)/2].
154
Sequential Representation
[1] A
A [2] B
[ 3] C
[4] D
B C [ 5] E
[6] F
[7] G
D E F G [8] H
[9] I
. .
. .
H I . .
. .
155
Sequential Representation
55
[0] 55
[1] 44
44 66 [ 2] 66
[3] 33
[ 4] 50
33 50 [5]
[6]
[7] 22
[8]
22
156
Advantages of sequential representation
The only advantage with this type of representation is that
the direct access to any node can be possible and finding the
parent or left right children of any particular node is fast
because of the random access.
157
Disadvantages of sequential representation
158
Linked Representation of Binary Trees
• In computer’s memory, a binary tree can be maintained either using a
linked representation or using sequential representation.
• In linked representation of binary tree, every node will have three parts:
the data element, a pointer to the left node and a pointer to the right
node. So in C, the binary tree is built with a node type given as below.
2 3
4 5 6 7
X 8 X X 9 X X 10 X X 11 X X 12 X
Linked Representation
struct node
{
int data;
struct node * left_child, *right_child;
};
data
159
Linked Representation
root
55
44 X 66 X
33 X X 50 X
X 22 X 55,44,66,33,50,22
160
Advantages of Linked representation
161
Disadvantages of linked representation
162
Tree operations
• Traversing a binary tree is the process of visiting each node in the tree exactly once in a
systematic way.
• There are four different algorithms for tree traversals, which differ in the order in which
the nodes are visited:
– Pre-order
– In-order
– Post-order
– Breadth-first-order
Pre-order
• The algorithm starts with the root node of the tree and continues by:
o Visiting the root node
o Traversing the left subtree
o Traversing the right subtree
B C
D E G F
Pre-order A, B, D, E, C, G, F
In-order
• The algorithm starts with the root node of the tree and continues by,
o Traversing the left subtree
o Visiting the root node
o Traversing the right subtree
In-order Implementation
In-order
A
B C
D E G F
In-order: D, B, E, A, G, C, F
Post-order
B C
D E G F
Post-order: D, E, B, G, F, C, A
Breadth-first-order
• Visit every node on a level before going to a lower level,
broadened as much as possible on each depth before
going to the next depth.
• Implemented by queue data structure
A
B C
D E G F
Breath-first-order: A, B, C, D, E, G, F
Tree search
• Search tree is to find a node with matched key value.
• Algorithms: traverse the tree and return the matched node if found
• Depth-first search (DFS): deepened search as much as possible on each child
before going to the next sibling.
• Implemented by recursive function
• Breadth-first search (BFS): visit every node on a level before going to a lower
level, broadened as much as possible on each depth before going to the next
depth.
• Implemented by queue data structure
Applications of Trees
1.Trees are used to store simple as well as complex data. Here simple
means an int value, char value and complex data (structure).
2.Trees are often used for implementing other types of data structures
like hash tables, sets, and maps.
5.B-trees are also used for secondary indexes in databases, where the
index facilitates a select operation to answer some range criteria.
9.Trees are also widely used for information storage and retrieval in
symbol tables.
Expression Trees
• Binary trees can be used to store algebraic expressions.
expression exp = (a – b ) + ( c * d)
• This expression can be represented using a binary tree
• Postfix expression a b - c d* +
derived by post-order traversal
• Prefix expression + - a b * c d
derived by pre-order traversal
Huffman Tree
• Huffman coding is an entropy encoding algorithm developed by David A.
Huffman that is widely used as a lossless data compression technique.
• The Huffman coding algorithm uses a variable length code table to encode a
source character where the variable-length code table is derived on the basis of
the estimated probability of occurrence of the source character. The idea of
Huffman algorithm is to encode the frequently used characters using shorter
strings.
Example
symbol Frequency Huffman code
A 24 0
B 12 100
C 10 101
D 8 110
E 8 111
BADDEC ?
Binary Search Trees
• Stores keys in the nodes in a way so that searching, insertion and deletion can be
done efficiently.
Binary search tree property
• For every node X, all the keys in its left subtree are smaller than the key value in X, and all the
keys in its right subtree are larger than the key value in X
y z
Binary Search Trees