Computer notes - Binary Tree

3,719 views
3,567 views

Published on

If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is termed a strictly binary tree. There are a number of operations that can be defined for a binary tree.

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

No Downloads
Views
Total views
3,719
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
231
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • End of lecture 10
  • End of lecture 11
  • Computer notes - Binary Tree

    1. 1. Class No.11 Data Structures http://ecomputernotes.com
    2. 2. Code for Simulation <ul><li>// print the final avaerage wait time. </li></ul><ul><li>double avgWait = (totalTime*1.0)/count; </li></ul><ul><li>cout << &quot;Total time: &quot; << totalTime << endl; </li></ul><ul><li>cout << “Customer: &quot; << count << endl; </li></ul><ul><li>cout << &quot;Average wait: &quot; << avgWait << endl; </li></ul>http://ecomputernotes.com
    3. 3. Priority Queue <ul><li>#include &quot;Event.cpp&quot; </li></ul><ul><li>#define PQMAX 30 </li></ul><ul><li>class PriorityQueue { </li></ul><ul><li>public: </li></ul><ul><li>PriorityQueue() { </li></ul><ul><li>size = 0; rear = -1; </li></ul><ul><li>}; </li></ul><ul><li>~PriorityQueue() {}; </li></ul><ul><li>int full(void) </li></ul><ul><li>{ </li></ul><ul><li>return ( size == PQMAX ) ? 1 : 0; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    4. 4. Priority Queue <ul><li>Event* remove() </li></ul><ul><li>{ </li></ul><ul><li>if( size > 0 ) { </li></ul><ul><li>Event* e = nodes[0]; </li></ul><ul><li>for(int j=0; j < size-2; j++ ) </li></ul><ul><li>nodes[j] = nodes[j+1]; </li></ul><ul><li>size = size-1; rear=rear-1; </li></ul><ul><li>if( size == 0 ) rear = -1; </li></ul><ul><li>return e; </li></ul><ul><li>} </li></ul><ul><li>return (Event*)NULL; </li></ul><ul><li>cout << &quot;remove - queue is empty.&quot; << endl; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    5. 5. Priority Queue <ul><li>int insert(Event* e) </li></ul><ul><li>{ </li></ul><ul><li>if( !full() ) { </li></ul><ul><li>rear = rear+1; </li></ul><ul><li>nodes[rear] = e; </li></ul><ul><li>size = size + 1; </li></ul><ul><li> sortElements(); // in ascending order </li></ul><ul><li> return 1; </li></ul><ul><li>} </li></ul><ul><li>cout << &quot;insert queue is full.&quot; << endl; </li></ul><ul><li>return 0; </li></ul><ul><li>}; </li></ul><ul><li>int length() { return size; }; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    6. 6. Tree Data Structures <ul><li>There are a number of applications where linear data structures are not appropriate. </li></ul><ul><li>Consider a genealogy tree of a family. </li></ul>http://ecomputernotes.com Mohammad Aslam Khan Sohail Aslam Javed Aslam Yasmeen Aslam Saad Haaris Qasim Asim Fahd Ahmad Sara Omer
    7. 7. Tree Data Structure <ul><li>A linear linked list will not be able to capture the tree-like relationship with ease. </li></ul><ul><li>Shortly, we will see that for applications that require searching, linear data structures are not suitable. </li></ul><ul><li>We will focus our attention on binary trees . </li></ul>http://ecomputernotes.com
    8. 8. Binary Tree <ul><li>A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. </li></ul><ul><li>The first subset contains a single element called the root of the tree. </li></ul><ul><li>The other two subsets are themselves binary trees called the left and right subtrees . </li></ul><ul><li>Each element of a binary tree is called a node of the tree. </li></ul>http://ecomputernotes.com
    9. 9. Binary Tree <ul><li>Binary tree with 9 nodes. </li></ul>D H F G I http://ecomputernotes.com A B C E
    10. 10. Binary Tree D H F G I Left subtree root Right subtree http://ecomputernotes.com A B C E
    11. 11. Binary Tree <ul><li>Recursive definition </li></ul>D H F G I Left subtree root Right subtree http://ecomputernotes.com A B C E
    12. 12. Binary Tree <ul><li>Recursive definition </li></ul>D H F G I Left subtree root http://ecomputernotes.com A B C E
    13. 13. Binary Tree <ul><li>Recursive definition </li></ul>D H F G I root http://ecomputernotes.com A B C E
    14. 14. Binary Tree <ul><li>Recursive definition </li></ul>D H F G I root Right subtree http://ecomputernotes.com A B C E
    15. 15. Binary Tree <ul><li>Recursive definition </li></ul>D H F G I root Right subtree Left subtree http://ecomputernotes.com A B C E
    16. 16. Not a Tree <ul><li>Structures that are not trees. </li></ul>D H F G I http://ecomputernotes.com A B C E
    17. 17. Not a Tree <ul><li>Structures that are not trees. </li></ul>D H F G I http://ecomputernotes.com A B C E
    18. 18. Not a Tree <ul><li>Structures that are not trees. </li></ul>D H F G I http://ecomputernotes.com A B C E
    19. 19. Binary Tree: Terminology D H F G I parent Left descendant Right descendant Leaf nodes Leaf nodes http://ecomputernotes.com A B C E
    20. 20. Binary Tree <ul><li>If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is termed a strictly binary tree . </li></ul>D H F G I http://ecomputernotes.com A B C E J K
    21. 21. Level of a Binary Tree Node <ul><li>The level of a node in a binary tree is defined as follows: </li></ul><ul><ul><li>Root has level 0, </li></ul></ul><ul><ul><li>Level of any other node is one more than the level its parent (father). </li></ul></ul><ul><li>The depth of a binary tree is the maximum level of any leaf in the tree. </li></ul>http://ecomputernotes.com
    22. 22. Level of a Binary Tree Node D H F G I 1 0 1 2 2 2 3 3 3 Level 0 Level 1 Level 2 Level 3 http://ecomputernotes.com A B C E
    23. 23. Complete Binary Tree <ul><li>A complete binary tree of depth d is the strictly binary all of whose leaves are at level d . </li></ul>N G O 1 0 1 2 3 3 L F M 2 3 3 H D I 2 3 J E K 2 3 http://ecomputernotes.com A B C
    24. 24. Complete Binary Tree Level 0: 2 0 nodes H D I E J K L F M G N O Level 1: 2 1 nodes Level 2: 2 2 nodes Level 3: 2 3 nodes http://ecomputernotes.com A B C
    25. 25. Complete Binary Tree <ul><li>At level k , there are 2 k nodes. </li></ul><ul><li>Total number of nodes in the tree of depth d : 2 0 + 2 1 + 2 2 + ………. + 2 d =  2 j = 2 d+1 – 1 </li></ul><ul><li>In a complete binary tree, there are 2 d leaf nodes and (2 d - 1) non-leaf (inner) nodes. </li></ul>j=0 d http://ecomputernotes.com
    26. 26. Complete Binary Tree <ul><li>If the tree is built out of ‘n’ nodes then n = 2 d+1 – 1 or log 2 (n+1) = d+1 or d = log 2 (n+1) – 1 </li></ul><ul><li>I.e., the depth of the complete binary tree built using ‘n’ nodes will be log 2 (n+1) – 1. </li></ul><ul><li>For example, for n=100,000, log 2 (100001) is less than 20; the tree would be 20 levels deep. </li></ul><ul><li>The significance of this shallowness will become evident later. </li></ul>http://ecomputernotes.com
    27. 27. Operations on Binary Tree <ul><li>There are a number of operations that can be defined for a binary tree. </li></ul><ul><li>If p is pointing to a node in an existing tree then </li></ul><ul><ul><li>left( p ) returns pointer to the left subtree </li></ul></ul><ul><ul><li>right( p ) returns pointer to right subtree </li></ul></ul><ul><ul><li>parent( p ) returns the father of p </li></ul></ul><ul><ul><li>brother( p ) returns brother of p . </li></ul></ul><ul><ul><li>info(p) returns content of the node. </li></ul></ul>http://ecomputernotes.com

    ×