Computer notes - Binary Tree
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Computer notes - Binary Tree

on

  • 3,200 views

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.

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.

Statistics

Views

Total Views
3,200
Views on SlideShare
3,200
Embed Views
0

Actions

Likes
3
Downloads
164
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • End of lecture 10
  • End of lecture 11

Computer notes - Binary Tree Presentation Transcript

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