Lect 22 Zaheer Abbas
-
Upload
information-technology-center -
Category
Technology
-
view
1.095 -
download
3
Transcript of Lect 22 Zaheer Abbas
![Page 1: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/1.jpg)
Created by Zaheer Abbas Aghani 2k9-152
![Page 2: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/2.jpg)
Non-Linear Data StructureTree
![Page 3: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/3.jpg)
As list, Stack, & Queue, binary tree can also be implement in two ways.
Linked Representation Array Representation.
LINKED REPRESENTATION OF BINARY TREE: if we implement tree data in linked list then every node of linked list has three member/parts. First member is for data, second and third member for left & right child . Second & third members are structure pointers which point to the same structure as for tree node.
3Prepared by: Shumaila Bashir
Sheikh(Lecturer, ITC)
![Page 4: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/4.jpg)
Tree Structure: Linked Representation of tree:
A
B C
D E F
A
B C
D E F
4Prepared by: Shumaila Bashir
Sheikh(Lecturer, ITC)
![Page 5: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/5.jpg)
Tree Structure: Array Representation of tree:- If we implement tree data in array then we need a 2 dimension array to store that data in memory & a pointer variable that store the address of root node..
A
B C
D E F
Data LN
RN
Root 3 1
2
3
4
5
6
A
B
D
C
F
E
5 2
1
4 6
null null
null
null null
null null5
Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
![Page 6: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/6.jpg)
Four Basic Operations
•Traversing•Searching•Inserting•Deleting
![Page 7: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/7.jpg)
In tree creation we take three parameters node, left child & right child, so traversing of binary tree means traversing of node, left subtree and right subtree.
There are three standard ways to traversing a binary tree. These three algorithms are
1) Preorder Traversal2) Inorder Traversal3) Postorder Traversal
![Page 8: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/8.jpg)
1.Visit the root.2.Traverse the left subtree of root in
preorder.3.Traverse the right subtree of root in
preorder.
If root is denoted as N, left subtree as L & right subtree as R then Preorder traversal is also called NLR Traversal.
![Page 9: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/9.jpg)
Preorder Traversal: ABDECFG
A
BC
D E F G
![Page 10: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/10.jpg)
The nodes are visited in preorder as: ABDHECFIG
A
BC
D E F G
H I
![Page 11: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/11.jpg)
1. Traverse the left subtree of root in Inorder.
2. Visit the root.3. Traverse the right subtree of root in
Inorder.
Inorder traversal is also called LNR Traversal.
![Page 12: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/12.jpg)
Inorder Traversal: DBEAFCG
A
BC
D E F G
![Page 13: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/13.jpg)
The nodes are visited in inorder as: DHBEAFCG
A
BC
D E F G
H
![Page 14: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/14.jpg)
1. Traverse the left subtree in postorder.2. Traverse the right subtree in postorder.3. Visit the Root.
Postorder traversal is also called LRN Traversal.
![Page 15: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/15.jpg)
Postorder Traversal: DEBFGCA
A
BC
D E F G
![Page 16: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/16.jpg)
The nodes are visited in postorder as: HDEBFGCA
A
BC
D E F G
H
![Page 17: Lect 22 Zaheer Abbas](https://reader036.fdocuments.us/reader036/viewer/2022062307/555a5fffd8b42a47748b52b2/html5/thumbnails/17.jpg)
In level order traversal, we traverse the nodes according to their levels. We start traversing with the level 0, then level traverse all the nodes of level 1, & then traverse all the nodes of level 2 & so on.
We traverse the nodes of a particular level from left to right.
A
EB
LEVEL 0
LEVEL 1
LEVEL 2
C K G
The nodes are traversing in level-order as: ABECKG