Download as pdf or txt
Download as pdf or txt
You are on page 1of 105

Department of CSE

Second Year
UNIT 5
Trees
Lecture 1
Contents
Introduction to Trees,
Binary Trees,
Memory Representation of Binary Tree,
Traversing binary trees, Header nodes,
Binary Search Tree, Heap and heapsort,
Path length & Huffman’s algorithm.
Trees
Tree is data structure that is non linear and can be used to
represents data in hierarchy between those elements.
For example: Organization Structure,

Family Tree, the Tournament, records, and Table of


contents.

• A parent-child relationship
4 Prachi V. Kale
Used to represent hierarchical data.

Prachi(CEO)

Sohum (CTO ) Tarun (Presedent) Bithi

Vinay Moni Sila Rashel Raj

Dipa Sahil Riya Shakti

5 Prachi V. Kale
Types of Trees
There are various types of Trees Such as
➢ Binary Tree
➢ Complete Binary Tree
➢ 2 Tree or Extended Binary Tree
➢ Binary Search Tree
➢ Heap Tree

6 Prachi V. Kale
A Tree consist of nodes with a parent-child relationship.
● Tree data structure applications
➢ Organization charts
➢ File systems
➢ Programing environment

7 Prachi V. Kale
Nodes
▪ A Collection of entities called Nodes.

▪ Tree is a Non-Linear Data Structure.

▪ It’s a hierarchica Structure.

8 Prachi V. Kale
Relation Of Tree
➢ Root-The top most Node. 1

2
➢ Children
3
➢ Parents
➢ Siblings- Have same parents. 4 5 7
6 8
➢ Leaf- Has no Child.

9 10 11

9 Prachi V. Kale
Tree is a sequence of nodes
➢ There is a starting node known as a root node
➢ Every node other than the root has a parent node.
➢ Nodes may have any number of children

10 Prachi V. Kale
11 Prachi V. Kale
12 Prachi V. Kale
Some Key Terms
• Root − Node at the top of the tree is called root.

• Parent − Any node except root node has one edge upward to a node called parent.

• Child − Node below a given node connected by its edge downward is called its child node.

• Sibling – Child of same node are called siblings

• Leaf − Node which does not have any child node is called leaf node.

• Sub tree − Sub tree represents descendants of a node.

• Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child node is at level 1,

its grandchild is at level 2 and so on.

• Keys − Key represents a value of a node based on which a search operation is to be carried out for a node.

13 Prachi V. Kale
• Degree of a node:
• The degree of a node is the number of children of that node
• Degree of a Tree:
• The degree of a tree is the maximum degree of nodes in a given tree
• Path:
• It is the sequence of consecutive edges from source node to destination node.
• Height of a node:
• The height of a node is the max path length form that node to a leaf node.
• Height of a tree:
• The height of a tree is the height of the root
• Depth of a tree:
• Depth of a tree is the max level of any leaf in the tree

14 Prachi V. Kale
15 Prachi V. Kale
Characteristics of trees

➢ Non-linear data structure

➢ Combines advantages of an ordered array

➢ Searching as fast as in ordered array

➢ Insertion and deletion as fast as in linked list

➢ Simple and fast

16 Prachi V. Kale
• A binary tree, is a tree in which no node can have more than two children.

• Consider a binary tree T, here ‘A’ is the root node of the binary tree T.

• ‘B’ is the left child of ‘A’ and ‘C’ is the right child of
‘A’
• i.e A is a father of B and C.

• The node B and C are called siblings.

• Nodes D,H,I,F,J are leaf node

17 Prachi V. Kale
Binary Trees
• A binary tree, T, is either empty or such that
I. T has a special node called the root node
II. T has two sets of nodes LT and RT, called the left subtree and right subtree of
T, respectively.
LT and RT are binary trees.

18 Prachi V. Kale
Binary Tree Cont.…
• A binary tree is a finite set of elements that are either empty or is
partitioned into three disjoint subsets.

• The first subset contains a single element called the root of the tree.

• The other two subsets are themselves binary trees called the left and right
sub-trees of the original tree.

• A left or right sub-tree can be empty.

• Each element of a binary tree is called a node of the tree.

19 Prachi V. Kale
Binary Trees
The following figure shows a binary tree with 9 nodes where A is the root

20 Prachi V. Kale
• The root node of this binary tree is A.

• The left sub tree of the root node, which we denoted by LA, is the set
LA = {B,D,E,G} and the right sub tree of the root node,
RA is the set RA={C,F,H}

• The root node of LA is node B, the root node of RA is C and so on

21 Prachi V. Kale
Types of Binary Tree

➢ Complete binary tree

➢ Strictly binary tree

➢ Almost complete binary tree

22 Prachi V. Kale
Strictly Binary Tree
• If every non-leaf node in a binary tree has nonempty left and right sub-trees, then
such a tree is called a strictly binary tree.

• Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero
or two, never degree one.

• A strictly binary tree with N leaves always contains 2N – 1 nodes.

23 Prachi V. Kale
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.

• The left child of node K is 2*K and the right child is 2*K+1 and parent is floor of K/2

2 3

4 5 6 7
11
8 9 10

24 Prachi V. Kale
Almost Complete Binary Tree
• An almost complete binary tree is a tree where for a right child, there is always a

left child, but for a left child there may not be a right child.

25 Prachi V. Kale
Department of CSE
Second Year
UNIT 5
Trees
Lecture 2
Tree Traversal
• Traversal is a process to visit all the nodes of a tree and may print their values too.

• All nodes are connected via edges (links) we always start from the root (head)node.

• There are three ways which we use to traverse a tree


• In-order Traversal
• Pre-order Traversal
• Post-order Traversal

• Generally we traverse a tree to search or locate given item or key in the tree
or to print all the values it contains.

28 Prachi V. Kale
Pre-order, In-order, Post-order
• Pre-order
<root> <left> <right>

• In-order
<left> <root> <right>

• Post-order
<left> <right> <root>

29 Prachi V. Kale
Pre-order Traversal
• The preorder traversal of a nonempty binary tree is defined as follows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder

30 Prachi V. Kale
Example of Pre-order traversal

31 Prachi V. Kale
In-Order traversal
• The in-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder

• The in-order traversal output of the given tree is

H DI BEAFC G
E A
H D I B F
C G

32 Prachi V. Kale
Example of In-order traversal

33 Prachi V. Kale
Post-order traversal
• The post-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node

• The post-order traversal


output of the given tree is
HIDEBFGCA

34 Prachi V. Kale
Example of Post-order traversal

35 Prachi V. Kale
Question

• A Binary Tree T has 9 nodes. The inorder and preorder traversal of T yield the
following sequence of nodes
• Inorder : E A C K F H D B G
• Preorder : F A E K C D H G B

The Preorder Traversal has - Root Left Right


The Inorder Traversal has - Left Root Right
Got the Root in Inoder
based on Preorder
• Inorder : E A C K F H D B G traversal

• Preorder : F A E K C D H G B
Root 1st

36 Prachi V. Kale
Question

• In INORDER Traversal ROOT is in between Left & Right


• Inorder : E A C K F H D B G
The Preorder Traversal has - Root Left Right
Now find the Root in the left Subtree i.e. from E A C K
• Preorder : F A E K C D H G B
Root in left
subtree

• Now find the left and right in Left Subtree


• Inorder : E A C K F H D B G
Got the Root in Inoder based on
Preorder traversal
37 Prachi V. Kale
Question

• In Left subtree A is the Root and E is the left child of A


• Inorder : E A C K
There are two nodes in Right i.e C & K
Now find the Root from C & K based on preorder traversal
• Preorder : F A E K C D H G B
Root in right side of left
subtree

• Now find the left and right in Left Subtree


• Inorder : E A C K F H D B G
Got the Root in Inoder based on
Preorder traversal
38 Prachi V. Kale
Question

• From the inorder we let that C is the left child of K

E K

39 Prachi V. Kale
Question

• In INORDER Traversal ROOT is in between Left & Right


• Inorder : E A C K F H D B G
The Preorder Traversal has - Root Left Right
Now find the Root in the Right Subtree i.e. from H D B G
• Preorder : F A E K C D H G B
Root in Right
subtree

• Now find the left and right in Left Subtree


• Inorder : E A C K F H D B G
Got the Root in Inoder based on
Preorder traversal
40 Prachi V. Kale
Question

• In Right subtree D is the Root and H is the left child of D


• Inorder : H D B G
There are two nodes in Right i.e B & G
Now find the Root from C & K based on preorder traversal
• Preorder : F A E K C D H G B
Root in right side of
Right subtree

• Now find the left and right in Right Subtree


• Inorder : E A C K F H D B G
Got the Root in Inoder based on
Preorder traversal
41 Prachi V. Kale
Question

• From the inorder we let that C is the left child of K

A D

E K H G
C
B

42 Prachi V. Kale
Department of CSE
Second Year
UNIT 5
Trees
Lecture 3
Binary Search Tree
• Binary Search Tree works as Binary Search Algorithm

• If value of inserted node is bigger than parent then it will be right subtree.

• If value of inserted node is smaller than parent then it will be left subtree.

• This tree is known as binary search tree.

45 Prachi V. Kale
Example: H A K C B L J
H will be root
A<H:
A will be left child of H
K>H:
K will be right child of H H

C<H→C>A:
A K
C will be right child of A
B<H→B>A→B<C :
C J L
B will be left child of C L > H → L > K :
L will be right child of K B
J>H→J<K:
J will be left child of K

46 Prachi V. Kale
Example
• Suppose the following list of letters are inserted in order into an empty
Binary Search Tree T .

• J R D G T E M H P A F Q

• Find the Final Tree T

• Find the Inorder traversal of T

Soln. – A tree T is a Binary Search Tree if each node N of T has the value at N
greater than every value in Left subtree of N & is less than every value in the
right subtree of N.

47 Prachi V. Kale
J R > J go to Right of J

D < J go to Left of J
D R
H < J go to left of J and H > D , D
G < J go to Left of J and G> D , D
is a Left root go to right in right H> G so
A is a Left root
insert H as right child of G G M T
T > J go to right of J and T > R , R
F < J go to left of J and F > D , D is a Left is a right root
root again F< G , which is E P
H
right child of D, go to left then E < J go to left of J and E > D , D is a Left
here F> E which is the left child of G so Q root again E< G , which is right child of D,
F
insert F at right of E insert E at Left of G
A < J go to left of J and A < D , D is a left root , insert A as the left M > J go to right of J and M< R , R
child of D is a right root , so insert it to the left of R
P > J go to right of J and P < R , R is a right root , go to the left where
P>M which is the Root so insert it to the Right of M
Q > J go to right of J and Q < R , R is a right root , go to the left where
Q > M which is the Root again Q > P so insert it to the Right of P

48 Prachi V. Kale
J

D R

A
G M T

P
E H
Q
D
F

A
PQ R
G H T
F J
E M

• The inorder Traversal is : A D E F G H J M P Q R T

49 Prachi V. Kale
Binary Search Tree
Draw the final tree T if node 20 is added 50 Compare ITEM with the root node N of the tree
to T
20 < 50 go to left
ITEM =20 25
75

Again compare 20 with 25 20 is less than 25


60
22 40 90 Go to left

At left there is 22 , here 20 is less than 22 again


15
30 44
go to left

20 At left there is 15

20 > 15 and no child is there for 15 so insert 20 as

the right child of 20

50 Prachi V. Kale
Extended Binary Tree
• A Binary Tree T is said to be 2 Tree or an Extended Binary Tree if each node
N has either 0 or 2 children.
• The nodes with 2 children are called Internal Nodes and the nodes with 0
children are called as External Nodes.
• Sometimes the nodes are distinguish in diagram by using circles from
Internal Nodes and squares for External Nodes. J
J
D R
D R

A
A G M T
G M T

H
H

Binary Tree T Extended 2 Tree

51 Prachi V. Kale
Representing Binary Tree in Memory
• A Binary Tree T is represented in two ways in memory.
• The first way is called the link representation of tree &
• another is single array called sequential representation of T.
One node in binary tree

Left Data Field Right


Child (Info) Child

Link Representation of tree

52 Prachi V. Kale
Binary Tree Linked Representation
Binary Tree Binary Tree
(Linked List)
A Root

A
B

D C B

D C
E F
E F
I G

I G
H
H

53 Prachi V. Kale
Sequential representation of Binary Tree
• Suppose T is a binary tree which is complete or nearly complete. Then there
is an efficient way of maintaining T in memory called Sequential
Representation,
• The root is TREE[1]
• If a node N occupies TREE[K] , then left child is stored in TREE[2*K]
• right child is stored in TREE[2*K+1]
45
22 77

11
30 90

15 25 88

Binary Tree T

54 Prachi V. Kale
Array representation of Binary Tree
1 45
2 22
45
3 77
22 77
4 11
11 5 30
30 90
6
7 90
15 25 88
8 15
Binary Tree T
9
10 25
11
12
13
14 88
15
16 Prachi V. Kale
55
From General Tree
• First son in general tree will be left son in binary tree

• Next brother of first son in general tree will be right son in binary tree.

56 Prachi V. Kale
Example

General Tree Binary Tree

A A

B C B

D C
D E F G H
E F

I
I G

57 Prachi V. Kale
From General Tree (Program)

One node in general tree One node in binary tree

First Son Next Brother Data Field Left Son Data Field Right Son
(FS) (NB) (Info) (LS) (Info) (RS)

58 Prachi V. Kale
Linked Representation General Tree
General Tree General Tree
(Linked List)
Head

A A

B C B

D C
D E F G H
E F

I
I G

59 Prachi V. Kale
Exercise
Make binary tree from this general tree:

B C D

G J
E F H K

L M N

60 Prachi V. Kale
Department of CSE
Second Year
UNIT 5
Trees
Lecture 4
What is a Heap?
A heap is also known as a priority queue and can be represented by a
binary tree with the following properties:
Structure property: A heap is a completely filled binary tree with the exception
of the bottom row, which is filled from left to right
Heap Order property: For every node x in the heap, the parent of x greater than
or equal to the value of x.
(known as a maxHeap).

63 Prachi V. Kale
Example:
a heap 53

44 25

15 21 13 18

3 12 5 7

64 Prachi V. Kale
Heap data structure
• Binary tree

• Balanced

• Left-justified or Complete

• (Max) Heap property: no node has a value greater


than the value in its parent

65 Prachi V. Kale
Balanced binary trees

• Recall:
– The depth of a node is its distance from the root
– The depth of a tree is the depth of the deepest node
• A binary tree of depth n is balanced if all the nodes at
depths 0 through n-2 have two children

n-2
n-1
n
Balanced Balanced Not balanced

66 Prachi V. Kale
Left-justified binary trees

• A balanced binary tree of depth n is left-


justified if:
– it has 2n nodes at depth n (the tree is “full”), or
– it has 2k nodes at depth k, for all k < n, and all
the leaves at depth n are as far left as possible

Left-justified Not left-justified

67 Prachi V. Kale
Building up to heap sort
• How to build a heap

• How to maintain a heap

• How to use a heap to sort data

68 Prachi V. Kale
The heap property
• A node has the heap property if the value in the
node is as large as or larger than the values in its
children
12 12 12

8 3 8 12 8 14
Red node has Red node has Red node does not
heap property heap property have heap property

• All leaf nodes automatically have the heap property


• A binary tree is a heap if all nodes in it have the
heap property
69 Prachi V. Kale
siftUp
• Given a node that does not have the heap property, you
can give it the heap property by exchanging its value with
the value of the larger child

12 14

8 14 8 12
Blue node does not Blue node has
have heap property heap property

• This is sometimes called sifting up

70 Prachi V. Kale
Constructing a heap I

• A tree consisting of a single node is automatically


a heap
• We construct a heap by adding nodes one at a time:
– Add the node just to the right of the rightmost
node in the deepest level
– If the deepest level is full, start a new level
• Examples:
Add a new Add a new
node here node here

71 Prachi V. Kale
Constructing a heap II

• Each time we add a node, we may destroy the heap property of its parent
node
• To fix this, we sift up
• But each time we sift up, the value of the topmost node in the sift may
increase, and this may destroy the heap property of its parent node
• We repeat the sifting up process, moving up in the tree, until either
– We reach nodes whose values don’t need to be swapped (because the
parent is still larger than both children), or
– We reach the root

72 Prachi V. Kale
Constructing a heap III
8 8 10 10

10 8 8 5

1 2 3

10 10 12

8 5 12 5 10 5

12 8 8
4

73 Prachi V. Kale
Other children are not affected
12 12 14

10 5 14 5 12 5

8 14 8 10 8 10

• The node containing 8 is not affected because its parent gets larger, not
smaller
• The node containing 5 is not affected because its parent gets larger, not
smaller
• The node containing 8 is still not affected because, although its parent got
smaller, its parent is still greater than it was originally

74 Prachi V. Kale
A sample heap
• Here’s a sample binary tree after it has been heapified

25

22 17

19 22 14 15

18 14 21 3 9 11

• Notice that heapified does not mean sorted


• Heapifying does not change the shape of the binary tree;
this binary tree is balanced and left-justified because it
started out that way
75 Prachi V. Kale
Removing the root (animated)
• Notice that the largest number is now in the root
• Suppose we discard the root:
11

22 17

19 22 14 15

18 14 21 3 9 11

• How can we fix the binary tree so it is once again balanced


and left-justified?
• Solution: remove the rightmost leaf at the deepest level
and use it for the new root
76 Prachi V. Kale
The reHeap method I
• Our tree is balanced and left-justified, but no longer a heap
• However, only the root lacks the heap property
11

22 17

19 22 14 15

18 14 21 3 9

• We can siftDown() the root


• After doing this, one and only one of its children may have
lost the heap property
77 Prachi V. Kale
The reHeap method II
• Now the left child of the root (still the number 11) lacks
the heap property
22

11 17

19 22 14 15

18 14 21 3 9

• We can siftDown() this node


• After doing this, one and only one of its children may have
lost the heap property
78 Prachi V. Kale
The reHeap method III
• Now the right child of the left child of the root (still the
number 11) lacks the heap property:
22

22 17

19 11 14 15

18 14 21 3 9

• We can siftDown() this node


• After doing this, one and only one of its children may have
lost the heap property —but it doesn’t, because it’s a leaf
79 Prachi V. Kale
The reHeap method IV
• Our tree is once again a heap, because every node in it
has the heap property
22

22 17

19 21 14 15

18 14 11 3 9

• Once again, the largest (or a largest) value is in the root


• We can repeat this process until the tree becomes empty
• This produces a sequence of values in order largest to smallest
80 Prachi V. Kale
Department of CSE
Second Year
UNIT 5
Trees
Lecture 5
Sample Run
• Start with unordered array of data
Array representation:

21 15 25 3 5 12 7 19 45 2 9

Binary tree representation:

21

15 25

3 5 12 7

19 45 2 9

83
Sample Run
• Heapify the binary tree -
21 21
21
15 25 15 25
15 25

3 5 12 7 3 9 12 7 45 9 12 7

19 45 2 9 19 45 2 5 19 3 2 5

45 21 21
21 25 45 25 15 25
19 9 12 7 19 9 12 7 45 9 12 7

15 3 2 5 15 3 2 5 19 3 2 5

84 Prachi V. Kale
Step 2 – perform n – 1 deleteMax(es), and replace last element in heap with
first, then re-heapify. Place deleted element in the last nodes position.

45 25

21 25 21 12

19 9 12 7 19 9 5 7

15 3 2 5 15 3 2

45 21 25 19 9 12 7 15 3 2 5 25 21 12 19 9 5 7 15 3 2 45

25 21

21 12 19 12

19 9 5 7 15 9 5 7

15 3 2 2 3
25 21 12 19 9 5 7 15 3 2 45 21 19 12 15 9 5 7 2 3 25 45
21 19
19 12 15 12
15 9 5 7 3 9 5 7

2 3 2
21 19 12 15 9 5 7 2 3 25 45 19 15 12 3 9 5 7 2 21 25 45

19 15
15 12 9 12
3 9 5 7 3 2 5 7

2
15 9 12 3 2 5 7 19 21 25 45
19 15 12 3 9 5 7 2 21 25 45
15 12
9 12 9 7
3 2 5 7 3 2 5

15 9 12 3 2 5 7 19 21 25 45 12 9 7 3 2 5 15 19 21 25 45

12 9

9 7 5 7

3 2 3 2
5
9 5 7 3 2 12 15 19 21 25 45
12 9 7 3 2 5 15 19 21 25 45

…and finally 2 3 5 7 9 12 15 19 21 25 45
Huffman Coding

An extended binary tree or 2-Tree , is a Binary tree T in which each node has either 0 or 2 children.

The nodes with 0 children are called External Nodes and the nodes with 2 children are called Internal
Nodes .

The internal nodes are denoted by circles and the external nodes are denoted by Squares.
In any 2-Tree , the number NE of External nodes is 1 more than the number NI of Internal Nodes .
J
NE= NI + 1
An algorithm can be represented by a 2-Tree D R
where the internal nodes represent tests and
the external nodes represent action A
G M T

88 Prachi V. Kale
Path Length
Suppose a Tree T is given with n external nodes and suppose each of the external
M
node is assigned a non – negative weight,
The External weighted path length P of the tree T is defined to be the sum of the
M T
Weighted Path Length
2 3 5 P= W1L1+ W2L2
11
W is the weight and L is the Path length
T

For tree T path is


P= 2.2+3.2+5.2+11.2
=42

89 Prachi V. Kale
Huffman Algorithm
Suppose a Tree T is given with n external nodes and suppose each of the external node is assigned a non – negative
weight,
The External weighted path length P of the tree T is defined to be the sum of the Weighted Path Length
P= W1L1+ W2L2
W is the weight and L is the Path length

For tree T path is


P= 2.2+3.2+5.2+11.2
=42

90 Prachi V. Kale
Example
Suppose A , B, C, D , E ,F G & H are 8 data items and suppose they are assigned weights as follows

Data Item : A B C D E F G H
Weight: 22 5 11 19 2 11 25 5

Consider a Tree with minimum weighted path length using above data and Huffman’s algorithm.

A B C D E F G H
22 5 11 19 2 11 25 5

91 Prachi V. Kale
Example
Each data item belongs to its own subtree . Two subtrees with the smallest possible combination of weight , the one
weighted 2 and the one of those weighted 5.

7
A C D F G H
B E
22 11 19 11 25 5
5 2

92 Prachi V. Kale
Example
The subtree that were shaded are joined together to form a subtree with weight 7.
Again the current two subtrees of the lowest weight are shaded

7
A C D F G H
22 7 11 19 11 25 5

B E
5 2

93 Prachi V. Kale
Example
Again the current two subtrees of the lowest weight are shaded

12
7
A C D F G H
22 11 19 11 25
7 5

B E
5 2

94 Prachi V. Kale
Example
Again the current two subtrees of the lowest weight are shaded

A C D 12 F G
22 11 19 7 11 25

H
7 5

B E
5 2

95 Prachi V. Kale
Example
Again the current two subtrees of the lowest weight are shaded

A C D 12 F G
22 11 19 7 11 25

H
7 5

B E
5 2

96 Prachi V. Kale
Example
Again the current two subtrees of the lowest weight are shaded

22

A D 12 G
C F
22 19 7 25
11 11
H
7 5

B E
5 2

97 Prachi V. Kale
Example
The subtree that were shaded are joined together to form a subtree with weight 31.
Again the current two subtrees of the lowest weight are shaded

22
A 31 G
22 25
D 12 C F

19 7 11 11

H
7 5

B E
5 2

98 Prachi V. Kale
Example
The subtree that were shaded are joined together to form a subtree with weight 31.
Again the current two subtrees of the lowest weight are shaded

22
A 31 G
22 25
D 12 C F

19 7 11 11

H
7 5

B E
5 2

99 Prachi V. Kale
Example
The subtree that were shaded are joined together to form a subtree with weight 44.
Again the current two subtrees of the lowest weight are shaded

44
G
31
25 A 22
22
D 12
19 7 C F
11 11
H
7 5

B E
5 2

100 Prachi V. Kale


Example
The subtree that were shaded are joined together to form a subtree with weight 44.
Again the current two subtrees of the lowest weight are shaded

56

44 31 G
25
A 22 D 12
22 19 7

C F H
11 11
7 5

B E
5 2

101 Prachi V. Kale


Example
100

44
56

A 22 31 G
22
25
C F D 12

11 11 19 7
H
7 5

B E
5 2
102 Prachi V. Kale
Threaded Binary tree
• Consider the binary tree linked representation where half the entries in the
pointer fields LEFT & RIGHT will contain NULL elements.
• This space may be more effectively used by replacing the null entries by
some other type of information.
• So replace certain entries by special pointers which points to nodes higher
in the tree .
• These special pointers are called Threads and
• The binary tree with such pointers is called Threaded Trees.

10 Prachi V. Kale
Example

D R

A
G M T

H
Two Way Threading

D R

A
G M T

H
One Way Threading
10 Prachi V. Kale
Example
A
A
D R
D R

A
A G M T
G M T

H
H Two Way Threading
One Way Threading

10 Prachi V. Kale

You might also like