Unit 2 Trees
Unit 2 Trees
1 Trees
TOPICS
Tree- basic terminology, General tree and its
representation, representation using sequential
and linked organization,
Binary tree- properties, converting tree to binary
tree, binary tree traversals inorder, preorder, post
order, level wise -depth first and breadth first,
Operations on binary tree.
Binary Search Tree (BST), BST operations,
Threaded binary tree- concepts, threading,
insertion and deletion of nodes in in-order
threaded binary tree, in order traversal of in-order
threaded binary tree.
2
Case Study- Use of binary tree in expression
tree-evaluation and Huffman's coding
3
4
WHAT IS A TREE
A tree is a finite nonempty set of elements.
It is an abstract model of a hierarchical structure.
consists of nodes with a parent-child relation.
Applications:
Organization charts
File systems
Programming environments
......
General trees differ from binary trees in three ways:
- A node in a general tree may have more than two children.
- A node in a general tree has a (possibly empty) sequence of
children. Among binary trees, there is a difference between a
tree with a left subtree but no right subtree and a tree with a
right subtree but no left subtree. No such distinction is made
for general trees
- General trees cannot be empty. 5
TREE TERMINOLOGY
Root: node without parent (A)
Siblings: nodes share the same parent
Internal node: node with at least one
child (A, B, C, F)
External node (leaf ): node without
children (E, I, J, K, G, H, D)
Ancestors of a node: parent,
grandparent, grand-grandparent, etc.
Descendant of a node: child, grandchild,
grand-grandchild, etc.
Depth of a node: number of ancestors
Height of a tree: maximum depth of any node
(3)
Degree of a node: the number of its children
Degree of a tree: the maximum number of its
node. 6
Subtree: tree consisting of a node and its
descendants
HOW TO REPRESENT TREE?
The simplest is to represent each node as an
item plus a linear structure (a simple array or
some kind of List) containing references to its
children. This is called the array of children
or list of children representation.
8
HOW TO REPRESENT TREE?
A less intuitive but more space-efficient
representation has each node keeping track
of its first child and its next sibling. This is
called the first-child, next-sibling
representation.
9
LEFT CHILD, RIGHT SIBLING
REPRESENTATION
10
CONVERTING GENERAL TREE TO
BINARY TREE
The usual left pointer really points to the first
child of the node and the usual right pointer
points to the next sibling of the node
Moving right from a node accesses the siblings
of the node
Moving left and then right accesses all of the
11
CONVERTING GENERAL TREE TO
BINARY TREE
Use the root of the general tree as the root of the binary tree
Determine the first child of the root. This is the leftmost node in the
general tree at the next level
Insert this node. The child reference of the parent node refers to this
node
Continue finding the first child of each parent node and insert it
below the parent node with the child reference of the parent to this
node.
When no more first children exist in the path just used, move back to
the parent of the last node entered and repeat the above process. In
other words, determine the first sibling of the last node entered.
Complete the tree for all nodes. In order to locate where the node
fits you must search for the first child at that level and then follow
the sibling references to a nil where the next sibling can be inserted.
The children of any sibling node can be inserted by locating the
parent and then inserting the first child. Then the above process is
repeated. 12
EXAMPLE
13
BINARY TREE
Definition: A binary tree is a finite set of nodes that is
either empty or consists of a root and two disjoint binary
trees called the left subtree and the right subtree.
Finite (possibly empty) collection of elements.
A nonempty binary tree has a root element.
Is a tree in which no node can have more than two
subtrees; the maximum outdegree for a node is 2
These are called the left and right subtrees of the binary
tree. A
B C
D E F G
14
H I
DIFFERENCES BETWEEN A TREE &
A BINARY TREE
No node in a binary tree may have a degree
more than 2, whereas there is no limit on the
degree of a node in a tree.
A binary tree may be empty; a tree cannot
be empty
The subtrees of a binary tree are ordered;
15
MINIMUM NUMBER OF NODES
Minimum number of nodes in a binary tree whose
height is h.
At least one node at each of first h levels.
minimum number of
nodes is h
Nmin=H
16
MAXIMUM NUMBER OF NODES
All possible nodes at first h levels are present.
Nmax= 2h - 1
NUMBER OF NODES & HEIGHT
18
FULL BINARY TREE
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.
A full binary tree of a given height h has 2h – 1 nodes.
19
2 3
4 5 6 7
20
8 9 10 11 12 13 14 15
BINARY TREE REPRESENTATION
Array representation.
Linked representation.
21
ARRAY REPRESENTATION
Number the nodes using the numbering scheme for
a full binary tree. The node that is numbered i is
stored in tree[i].
a1
2 3
b c
4 5 6 7
d e f g
8 9 10
h i j
tree[] a b c d e f g h i j 22
0 5 10
RIGHT-SKEWED BINARY TREE
1
a
b 3
7
c
15
d
tree[] a - b - - - c - - - - - - - d
0 5 10 15
23
LINKED REPRESENTATION
24
THE CLASS BINARYTREENODE
template <class T>
class TreeNode
{
T data;
TreeNode<T> *leftChild,
*rightChild;
TreeNode()
{leftChild = rightChild = NULL;}
// other constructors come here
};
25
LINKED REPRESENTATION EXAMPLE
root a
b c
d e
g
f
leftChild
leftChild
data
data h 26
rightChild
rightChild
BINARY TREE AS ADT
Objects: a finite set of nodes either empty or
consisting of a root node, left binarytree, left
binarytree
Operations
Binarytree()
Boolean isempty()
Binarytree lchild()
Binarytree lchild()
Element data()
27
BINARY TREE TRAVERSAL
Many binary tree operations are done by
performing a traversal of the binary tree.
In a traversal, each element of the binary tree is
28
BINARY TREE TRAVERSAL METHODS
Depth-first
we process all the descendants of a child
before going on to next child
-Preorder
-Inorder
-Postorder
Level order (breadth first )each level is
29
PREORDER EXAMPLE (ROOT, LEFT,
RIGHT)
a
b c
abc
30
PREORDER EXAMPLE
a
b c
f
d e
g h i j
abdghei cf j 31
PREORDER OF EXPRESSION TREE
* +
e f
+ -
a b c d
/ * +a b - c d +e f
32
Gives prefix form of expression!
PREORDER TRAVERSAL
Algorithm PreOrder(root)
if (root != NULL)
print(root->data);
PreOrder(root->leftChild);
PreOrder(root->rightChild);
end if
End PreOrder
33
INORDER EXAMPLE
(LEFT,ROOT,RIGHT)
a
b c
bac
34
INORDER EXAMPLE
(LEFT,ROOT,RIGHT)
a
b c
f
d e
g h i j
gdhbei af j c 35
INORDER OF EXPRESSION TREE
/
* +
e f
+ -
a b c d
a + b * c - d/ e + f
36
Gives infix form of expression!
INORDER TRAVERSAL
Algorithm InOrder(root)
if (root != NULL)
InOrder(root->leftChild);
print(root->data);
InOrder(root->rightChild);
end if
End InOrder
37
POSTORDER EXAMPLE
(LEFT,RIGHT,ROOT)
a
b c
bca
38
POSTORDER EXAMPLE
(LEFT,RIGHT,ROOT)
a
b c
f
d e
g h i j
ghdi ebj f ca 39
POSTORDER OF EXPRESSION TREE
/
* +
e f
+ -
a b c d
a b +c d - * e f + /
40
Gives postfix form of expression!
POSTORDER TRAVERSAL
Algorithm PostOrder(root)
if (root != NULL)
PostOrder(root->leftChild);
PostOrder(root->rightChild);
print(root->data);
end if
End PostOrder
41
LEVEL-ORDER EXAMPLE (VISIT = PRINT)
a
b c
f
d e
g h i j
abcdef ghi j 42
LEVEL ORDER
Let t be the tree root.
while (t != NULL)
{
visit t and put its children on a FIFO queue;
If FIFO queue is empty, set t = NULL;
otherwise, pop a node from the FIFO queue
and call it t;
}
43
CREATION OF BINARY TREE
44
Iterative Inorder Traversal
45
ITERATIVE INORDER TRAVERSAL
B C
D E F G
H I
D
J K
B
A L 46
NULL
ITERATIVE INORDER TRAVERSAL
D E F G
H I
J K
B
A L 47
NULL
ITERATIVE INORDER TRAVERSAL
D E F G
H I
H
J K
B
A L 48
NULL
ITERATIVE INORDER TRAVERSAL
D E F G
H I
J K
B
A L 49
NULL
ITERATIVE INORDER TRAVERSAL
H I
J K
A L 50
NULL
ITERATIVE INORDER TRAVERSAL
H I
J K
F
E
A L 51
NULL
ITERATIVE INORDER TRAVERSAL
H I
J K
F
I
C L 52
NULL
53
ITERATIVE INORDER TRAVERSAL
H I
J
J K
I
G L 54
NULL
ITERATIVE INORDER TRAVERSAL
H I
J K
K
G L 55
NULL
ITERATIVE INORDER TRAVERSAL
G L 56
NULL
Inorder( node * T)
{
T=root;
while (T != NULL || stack.empty() == false)
{
while (T != NULL)
{
stack.push( T);
T = T ->left;
}
T = stack.top();
stack.pop();
T = T ->right;
} /* end of while */
}
57
ITERATIVE POSTORDER TRAVERSAL
push reverse postorder traversal to a stack.
reverse postorder traversal is similar to the
preorder traversal
right child is visited before left child
Algorithm:
1. Push root to first stack.
2. while first stack is not empty
2.1 Pop a node from first stack and push it to
second stack
2.2 Push left and right children of the popped
node to first stack 58
b c
f
d e
g h i j
59
SOME BINARY TREE OPERATIONS
Determine the height.
Determine the number of nodes.
Make a clone.
Determine if two binary trees are clones.
Display the binary tree.
Evaluate the arithmetic expression represented
by a binary tree.
Obtain the infix form of an expression.
Obtain the prefix form of an expression.
Obtain the postfix form of an expression.
60
NODE NUMBER PROPERTIES
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
61
NODE NUMBER PROPERTIES
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
64