Lect 19 Zaheer Abbas

672 views
596 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
672
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lect 19 Zaheer Abbas

  1. 1. Created by Zaheer Abbas Aghani 2K9-152
  2. 2. Non-Linear Data Structure TREE Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  3. 3. <ul><li>Definition: </li></ul><ul><li>Tree is a type of non-linear data structure that is mainly used to represent data containing a hierarchical relationship between elements. </li></ul><ul><li>e.g. records, family tree and table of contents. </li></ul>Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  4. 4. <ul><li>There are several ways to represent tree structure. We will use graph to represent tree in which every node will be represented by circle and a line from one node to other is called edge. </li></ul><ul><li>In tree every node has three fields, first represent the data while second & third keep the information of left & right child of node. </li></ul>Node Edge Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) A B D E C F
  5. 5. <ul><li>A tree can be consists of following Parts: </li></ul><ul><li>Node </li></ul><ul><li>Root Node </li></ul><ul><li>Parent Node </li></ul><ul><li>Child Node </li></ul><ul><li>Leaf Node </li></ul><ul><li>Sub-tree </li></ul>Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  6. 6. <ul><li>A node may contain a value or a condition or represent a separate data structure or a tree of its own. Each node in a tree has zero or more child nodes , which are below it in the tree. </li></ul>nodes Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  7. 7. <ul><li>The topmost node in a tree is called the root node . </li></ul><ul><li>Being the topmost node, the root node will not have parents. It is the node at which operations on the tree commonly begin. </li></ul>root node Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  8. 8. <ul><li>A node that has a child is called the parent node (or ancestor node , or superior ). Each node in tree has at most one parent except root node. </li></ul>parent node Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  9. 9. <ul><li>A node that has a parent node is called the child node. Each node in tree has is child node except leaf node. </li></ul>parent node Child node Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  10. 10. <ul><li>A node with no children is called a leaf node. </li></ul><ul><li>Most leaf nodes are at the bottom most level of tree. Since they are at the bottom most level, they do not have any children. </li></ul><ul><li>They are also referred to as terminal node. </li></ul>Leaf node Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  11. 11. <ul><li>A subtree is a portion of a tree data structure that can be viewed as a complete tree in itself. </li></ul><ul><li>Any node in a tree , together with all the nodes below it, comprise a subtree of . The subtree corresponding to the root node is the entire tree; </li></ul><ul><li>the subtree corresponding to any other node is called is a proper subtree </li></ul>Sub-tree Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  12. 12. <ul><li>An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf node. </li></ul><ul><li>An intermediate node between the root and the leaf nodes is called an internal node. </li></ul>Internal nodes Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  13. 13. <ul><li>First we study a special kind of tree called Binary tree, which can easily maintained in the computer. </li></ul><ul><li>Binary tree can be define as a finite set of elements, called nodes. </li></ul><ul><li>A binary tree can be either empty or Each node has at most two children. </li></ul><ul><li>Example:- </li></ul>Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) A B D E C F
  14. 14. <ul><li>Each node in binary tree is assigned a level number. </li></ul><ul><li>The level of each node in binary tree is defined as: </li></ul><ul><ul><li>Root of tree is defined as level 0 </li></ul></ul><ul><ul><li>Level number of other nodes is 1 more than level number of its father node or parent node. </li></ul></ul><ul><ul><li>Example: </li></ul></ul>A C B LEVEL 0 LEVEL 1 Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  15. 15. <ul><li>The depth or height of tree is one more than the largest level number of tree. </li></ul><ul><li>For example if the largest level number of tree is 2 than its depth is calculate by 2+1=3. </li></ul><ul><li>Example: </li></ul>LEVEL 0 LEVEL 1 A B C <ul><li>In this example the largest level number is 1 therefore depth is 1+1=2 </li></ul><ul><li>Depth=2 </li></ul>Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  16. 16. <ul><li>Strictly Binary Tree: If every internal node (non terminal node) has its non empty left & right children then it is called strictly binary tree. </li></ul><ul><li>Here every internal node A, B, E has two non empty left and right child hence it is strictly binary tree. </li></ul>A B C D E F G Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  17. 17. <ul><li>Complete Binary Tree: A complete binary tree is a binary tree where all internal nodes have both children except the last node. </li></ul><ul><li>In this tree the last node () has only one child. </li></ul>3 1 2 4 5 6 Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  18. 18. <ul><li>Extended Binary Tree: A binary tree is called extended binary tree if every node of tree has zero or two children. It is also said to be 2-tree. </li></ul><ul><li>In binary tree, most of the nodes have one child then we can add one more child and can extend it. So it can be converted to extended binary tree. </li></ul>Extended binary tree Binary Tree Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC)
  19. 19. <ul><li>Binary Search Tree: A binary search tree is a binary tree in which each node has value greater than every node of left sub-tree and less than every node of right sub-tree. </li></ul>Prepared by: Shumaila Bashir Sheikh(Lecturer, ITC) 50 45 22 48 75 85

×