Lect 22 Zaheer Abbas

1,313 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,313
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lect 22 Zaheer Abbas

  1. 1. Created by Zaheer Abbas Aghani 2k9-152
  2. 2. Non-Linear Data Structure Tree
  3. 3. <ul><li>As list, Stack, & Queue, binary tree can also be implement in two ways. </li></ul><ul><li>Linked Representation </li></ul><ul><li>Array Representation. </li></ul><ul><li>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. </li></ul>Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  4. 4. <ul><li>Tree Structure : </li></ul><ul><li>Linked Representation of tree: </li></ul>A B C D E F Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) A B C D E F
  5. 5. <ul><li>Tree Structure: </li></ul><ul><li>Array Representation of tree:- I f 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.. </li></ul>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 null Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) A B C D E F
  6. 6. <ul><li>Four Basic Operations </li></ul><ul><ul><li>Traversing </li></ul></ul><ul><ul><li>Searching </li></ul></ul><ul><ul><li>Inserting </li></ul></ul><ul><ul><li>Deleting </li></ul></ul>
  7. 7. <ul><li>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. </li></ul><ul><li>There are three standard ways to traversing a binary tree. These three algorithms are </li></ul><ul><li>Preorder Traversal </li></ul><ul><li>Inorder Traversal </li></ul><ul><li>Postorder Traversal </li></ul>
  8. 8. <ul><li>Visit the root. </li></ul><ul><li>Traverse the left subtree of root in preorder. </li></ul><ul><li>Traverse the right subtree of root in preorder. </li></ul><ul><li>If root is denoted as N, left subtree as L & right subtree as R then Preorder traversal is also called NLR Traversal. </li></ul>
  9. 9. <ul><li>Preorder Traversal: ABDECFG </li></ul>A B C D E F G
  10. 10. <ul><li>The nodes are visited in preorder as: ABDHECFIG </li></ul>I A B C D E F G H
  11. 11. <ul><li>Traverse the left subtree of root in Inorder. </li></ul><ul><li>Visit the root. </li></ul><ul><li>Traverse the right subtree of root in Inorder. </li></ul><ul><li>Inorder traversal is also called LNR Traversal. </li></ul>
  12. 12. <ul><li>Inorder Traversal: DBEAFCG </li></ul>A B C D E F G
  13. 13. <ul><li>The nodes are visited in inorder as: DHBEAFCG </li></ul>A B C D E F G H
  14. 14. <ul><li>Traverse the left subtree in postorder. </li></ul><ul><li>Traverse the right subtree in postorder. </li></ul><ul><li>Visit the Root. </li></ul><ul><li>Postorder traversal is also called LRN Traversal. </li></ul>
  15. 15. <ul><li>Postorder Traversal: DEBFGCA </li></ul>A B C D E F G
  16. 16. <ul><li>The nodes are visited in postorder as: HDEBFGCA </li></ul>A B C D E F G H
  17. 17. <ul><li>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. </li></ul><ul><li>We traverse the nodes of a particular level from left to right. </li></ul>LEVEL 2 C K G <ul><li>The nodes are traversing in level-order as: ABECKG </li></ul>A E B LEVEL 0 LEVEL 1

×