Professional Documents
Culture Documents
SPANNING OF TREE
Department of
Information Technology
Institute of Computing
Bahauddin Zakariya
University
Multan Pakistan
Prepared by
Mr. Ali Asad Sahu
Mr. Shuja Ahmad
Mr. Samiullah
Binary tree
Tree with 0–2 children per node
Interior ⇒ non-leaf
Root node
Leaf nodes
Binary Tree
A binary tree is either empty, or it consists of
a node called the root together with two binary
trees called the left sub-tree and the right sub-
tree of the root.
A node cannot have more than two sub-trees
Example
X >Y X
X < Z
Y Z
Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary search Not a binary
trees search tree
Importance of Trees
Representing Algebraic formulas
Searching large and dynamic lists
Supports Very effective searching
technique using binary search.
Implemented using linked lists.
Trees are used to organize databases and
file systems.
Unix or DOS/Windows file system
Spanning of Trees
The process of inserting new data elements
in tree data structure is called spanning of
tree.
How to build & maintain binary trees?
Insertion
Deletion
20
28,8,4,36,85,9,23,41,30 3
2
3
6
9
2 8
4
8 8
4 5
1
3
0
Binary Search Tree –
Deletion
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else
a) Replace with largest value Y on left subtree
OR smallest value Z on right
subtree
b) Delete replacement value (Y or Z) from
subtree
Example Deletion (Leaf)
Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
Example Deletion (Internal
Node)
Delete ( 10 ) Delete ( 5 ) Delete ( 5 )
10 5 5
5 30 5 30 2 30
2 25 45 2 25 45 2 25 45
1. Preorder NLR
2. Inorder LNR
3. Postorder LRN
Very Easy by using recursion. We have to
Preorder
PreOrder Traversal
A A
B E B E
C D F C D F
Walking order
A B C D E F
Processing order
InOrder Traversal
The algorithm of InOrder traversal is
following:
Recursive Definition
A A
B E B E
C D F C D F
Walking order
C B D A E F
Processing order
PostOrder Traversal
The algorithm of PostOrder traversal is following:
Recursive Definition
A A
B E B E
C D F C D F
Walking order
C D B F E A
Processing order
Example
Whatever the mind can conceive
and believe, the mind can
achieve.