0% found this document useful (0 votes)
31 views51 pages

Chap 3 Trees-1

Chapter 3 discusses trees and graphs, focusing on the structure and terminology of trees, including types such as binary trees, general trees, and forests. It covers tree representation methods, advantages and disadvantages of sequential and linked representations, and various tree operations like traversal and searching. Additionally, the chapter highlights applications of trees in data storage, compiler construction, and encoding techniques such as Huffman coding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views51 pages

Chap 3 Trees-1

Chapter 3 discusses trees and graphs, focusing on the structure and terminology of trees, including types such as binary trees, general trees, and forests. It covers tree representation methods, advantages and disadvantages of sequential and linked representations, and various tree operations like traversal and searching. Additionally, the chapter highlights applications of trees in data storage, compiler construction, and encoding techniques such as Huffman coding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Chapter 3

Trees and Graphs


Concepts of Trees
• A non-linear data structure represents hierarchical structure of data, with
a root and subtrees of children with a parent node, represented as a set of
linked nodes.
• A tree is recursively defined as a set of one or more nodes where 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.
• Each node is a data structure consisting of a value, together with a list of
references to child nodes , such that no reference is duplicated, no cycles.
Concepts of Trees

left right Root node, level 0


Left subtree
right subtree

left right left right level 1

left right left right left right level 2


Leaf node
Tree Terms
• Edge: It is the line connecting a node N to any of its successors.

• path: a sequence of consecutive edges.


• 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.
• 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.
Tree Terms

• 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 tree node can be represented by a strcut


struct node
{
int data;
struct node* left;
……..
struct node* right;
};

• A tree is represented by it root node


Types of trees
Types of Trees
 General Trees
 Forests
 Binary Trees
 Expression 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 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

R – Root node (node 1)


4
5 6 7
T1- left sub-tree (nodes 2, 4, 5, 8, 9)
T2- right sub-tree (nodes 3, 6, 7, 10, 11, 12)
8 9 1 1 1
0 1 2
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 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

• Similarly, the parent of node K can be calculated as | K/2 |. 4


5 6
7

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.

Extended binary tree

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

• The major disadvantage with this type of


representation is wastage of memory.
• The maximum depth of the tree has to be
fixed.
• The insertions and deletion of any node in the
tree will be costlier as other nodes has to be
adjusted at appropraite positions so that the
meaning of binary tree can be preserved.

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

left_child data right_child


left_child right_child

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

•This representation is superior to our


representation as there is no wastage of memory.
•Insertions and deletions which are the most
common operations can be done without moving the
other nodes.

161
Disadvantages of linked representation

• This representation does not provide direct


access to a node and special algorithms are
required.
• This representation needs additional space in
each node for storing the left and right sub-
trees.

162
Tree operations

• Tree traversal, display, print, …


• Tree search
• Insert node
• Delete node
• Clean tree
• Node count, tree height, leaf count, …
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 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

• To traverse a non-empty binary tree in preorder, the following operations are


performed recursively at each node.

• 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

• Implemented by recursive function


Pre-order Implementation
Pre-order
A

B C

D E G F

Pre-order A, B, D, E, C, G, F
In-order

• 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,
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

• 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,
o Traversing the left subtree
o Traversing the right subtree
o Visiting the root node
• Implemented by recursive function
Post-order Implementation
Post-order
A

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.

3.A self-balancing tree, Red-black tree is used in kernel scheduling to


preempt massively multi-processor computer operating system use.

4.Another variation of tree, B-trees are used to store tree structures on


disc. They are used to index a large number of records.
Applications of Trees

5.B-trees are also used for secondary indexes in databases, where the
index facilitates a select operation to answer some range criteria.

6.Trees are used for compiler construction.

7.Trees are also used in database design.

8.Trees are used in file system directories.

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

• The infix expression (a – b ) + ( c * d) can be derived by


in-order traversal, add ( at left, add ) at right

• 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

A binary search tree Not a binary search tree


Binary search trees
Two binary search trees representing the same set:

Average depth of a node is O(log N);


maximum depth of a node is O(N)
Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we should search in the left
subtree.
• If we are searching for a key > 15, then we should search in the right
subtree.
In-order traversal of BST
• Print out all the keys in sorted order

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20


Insert
• Proceed down the tree as you would with a find
• If X is found, do nothing (or update something)
• Otherwise, insert X at the last spot on the path traversed

• Time complexity = O(height of the tree)


Delete
Three cases:
(1) the node is a leaf
• Delete it immediately
(2) the node has one child
• Adjust a pointer from the parent to bypass that node
Delete
(3) the node has 2 children
• replace the key of that node with the minimum element at the right subtree
• delete the minimum element
• Has either no child or only right child because if it has a left child, that left child would be smaller and would
have been chosen. So invoke case 1 or 2.

• Time complexity = O(height of the tree)

You might also like