0% found this document useful (0 votes)
7 views63 pages

Unit 2 Trees

This document provides an overview of tree data structures, including basic terminology, types of trees (general, binary, binary search, and threaded binary trees), and their representations. It covers tree properties, traversal methods (inorder, preorder, postorder, and level order), and operations associated with binary trees. Additionally, it discusses the conversion of general trees to binary trees and includes examples and algorithms for various tree operations.

Uploaded by

sharma9103867592
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views63 pages

Unit 2 Trees

This document provides an overview of tree data structures, including basic terminology, types of trees (general, binary, binary search, and threaded binary trees), and their representations. It covers tree properties, traversal methods (inorder, preorder, postorder, and level order), and operations associated with binary trees. Additionally, it discusses the conversion of general trees to binary trees and includes examples and algorithms for various tree operations.

Uploaded by

sharma9103867592
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

UNIT 2

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

children of the node

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;

those of a tree are not 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.

Maximum number of nodes


= 1 + 2 + 4 + 8 + … + 2h-1 17

Nmax= 2h - 1
NUMBER OF NODES & HEIGHT

 Let n be the number of nodes in a binary tree


whose height is h.
 h <= n <= 2h – 1
 log (n+1) <= h <= n
2
 Given N no of nodes
- Tree height Hmax=N, Hmin=log2N+1

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

Height 4 full binary tree.


NUMBERING NODES IN A FULL BINARY TREE
 Number the nodes 1 through 2h – 1.
 Number by levels from top to bottom.

 Within a level number from left to right.

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

 An n node binary tree needs an array whose


length is between n+1 and 2n.

23
LINKED REPRESENTATION

 Each binary tree node is represented as


an object whose data type is TreeNode.
 The space required by an n node binary
tree is n * (space required by one node).

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

visited exactly once in predetermined sequence.


 During the visit of an element, all action (make a

clone, display, evaluate the operator, etc.) with


respect to this element is taken.

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

completely processed before next level is


started

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

1.First, we prepare a array stack and a binary tree


2.Push the node to stack and find left child until NU

B C

D E F G

H I
D
J K
B
A L 46

NULL
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


D 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and go to step 2
B C

D E F G

H I

J K
B
A L 47

NULL
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


D 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and go to step 2
B C

D E F G

H I
H
J K
B
A L 48

NULL
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


DH 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and go to step 2
B C

D E F G

H I

J K
B
A L 49

NULL
ITERATIVE INORDER TRAVERSAL

1First, we prepare a array stack . and a binary tree


D HB 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and 5.If
go to stepchild
right 2 is NULL,
B C pop top term from stack
and go to step 4
D E F G

H I

J K

A L 50

NULL
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


D H B EA 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and 5.If
go to stepchild
right 2 is NULL,
B C pop top term from stack
and go to step 4
D E F G

H I

J K
F
E
A L 51

NULL
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


D H B EAF C 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and 5.If
go to stepchild
right 2 is NULL,
B C pop top term from stack
and go to step 4
D E F G

H I

J K
F
I
C L 52

NULL
53
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


D H B EAF C J I 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and 5.If
go to stepchild
right 2 is NULL,
B C pop top term from stack
and go to step 4
D E F G

H I
J
J K
I
G L 54

NULL
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


D H B EAF C J I KL 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and 5.If
go to stepchild
right 2 is NULL,
B C pop top term from stack
and go to step 4
D E F G

H I

J K
K
G L 55

NULL
ITERATIVE INORDER TRAVERSAL

1.First, we prepare a array stack and a binary tree


D H B E A F C J I K LG 2.Push the node to stack and find left child until NU
3.If the node is NULL, pop top term from stack
4.Print the node and find right
A child
and 5.If
go to stepchild
right 2 is NULL,
B C pop top term from stack
and go to step 4
D E F G 6.If right child is
NULL
H I and stack is
empty,
J K stop the 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();

cout << T ->data << " ";

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

3. Pop and Print contents of second stack


POSTORDER:

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

 Parent of node i is node i / 2, unless i = 1.


 Node 1 is the root and has no parent.

61
NODE NUMBER PROPERTIES
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

 Left child of node i is node 2i, unless 2i > n, where


n is the number of nodes.
 If 2i > n, node i has no left child.
62
NODE NUMBER PROPERTIES
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

 Right child of node i is node 2i+1, unless 2i+1 > n,


where n is the number of nodes.
 If 2i+1 > n, node i has no right child.
63
COMPLETE BINARY TREE
 A complete binary tree is a binary tree in
which every level, except possibly the last, is
completely filled, and all nodes are as far left
as possible.

64

You might also like