SlideShare una empresa de Scribd logo
1 de 29
Tree: A Non-linear Data Structure




January 5, 2013   Programming and   1
Background

• Linked lists use dynamic memory allocation,
  and hence enjoys some advantages over static
  representations using arrays.
• A problem
   – Searching an ordinary linked list for a given
     element.
       • Time complexity O(n).
   – Can the nodes in a linked list be rearranged so that
     we can search in O(nlog2n) time?



January 5, 2013     Programming and          2
Illustration
                                    50                            Level 1

                    30                        75                  Level 2


          12                  45         53             80        Level 3


               20        35        48              78        85   Level 4




January 5, 2013          Programming and                      3
Binary Tree

• A new data structure
   – Binary means two.
• Definition
   – A binary tree is either empty, or it consists of a node
     called the root, together with two binary trees called
     the left subtree and the right subtree.
                                 Root




              Left                       Right
            subtree                     subtree


January 5, 2013       Programming and             4
Examples of Binary Trees




January 5, 2013   Programming and   5
Not Binary Trees




January 5, 2013   Programming and    6
Binary Search Trees

• A particular form of binary tree suitable for
  searching.
• Definition
   – A binary search tree is a binary tree that is either
     empty or in which each node contains a key that
     satisfies the following conditions:
       • All keys (if any) in the left subtree of the root precede the
         key in the root.
       • The key in the root precedes all keys (if any) in its right
         subtree.
       • The left and right subtrees of the root are again binary
         search trees.

January 5, 2013      Programming and                   7
Examples

     10                                             50



5          15                       30                            75


      12        20
                          12                  45         53                 80



                               20        35        48                  78        85




January 5, 2013      Programming and                          8
How to Implement a Binary Tree?

• Two pointers in every node (left and right).
       struct nd {
                     int element;
                     struct nd *lptr;
                     struct nd *rptr;
                   };
       typedef nd node;
       node *root;        /* Will point to the root of the tree */




January 5, 2013      Programming and                9
An Example
                                      a
• Create the tree                10

                          b 5         20       c

                                 15        d

        a = (node *) malloc (sizeof (node));
        b = (node *) malloc (sizeof (node));
        c = (node *) malloc (sizeof (node));
        d = (node *) malloc (sizeof (node));
        a->element = 10; a->lptr = b;              a->rptr = c;
        b->element = 5; b->lptr = NULL;            b->rptr = NULL;
        c->element = 20; c->lptr = d;              c->rptr = NULL;
        d->element = 15; d->lptr = NULL;           d->rptr = NULL;
        root = a;



January 5, 2013        Programming and                        10
Traversal of Binary Trees

• In many applications, it is required to move
  through all the nodes of a binary tree, visiting
  each node in turn.
   – For n nodes, there exists n! different orders.
   – Three traversal orders are most common:
       • Inorder traversal
       • Preorder traversal
       • Postorder traversal




January 5, 2013     Programming and           11
Inorder Traversal

• Recursively, perform the following three steps:
   – Visit the left subtree.
   – Visit the root.
   – Visit the right subtree.

   LEFT-ROOT-RIGHT




January 5, 2013    Programming and    12
Example:: inorder traversal
                                          10



        10                      20                  30



   20         30          40         25        50        60




   20 10 30                 40 20 25 10 50 30 60




January 5, 2013    Programming and             13
a



                  b                           c



          d                       e                   f



              g           h               i


                                      j           k



         . d g b . a h e j i k c f


January 5, 2013       Programming and                     14
Preorder Traversal

• Recursively, perform the following three steps:
   – Visit the root.
   – Visit the left subtree.
   – Visit the right subtree.

   ROOT-LEFT-RIGHT




January 5, 2013    Programming and     15
Example:: preorder traversal
                                         10



        10                     20                  30



   20        30          40         25        50        60




   10 20 30                10 20 40 25 30 50 60




January 5, 2013   Programming and             16
a



                  b                           c



          d                       e                   f



              g           h               i


                                      j           k



         a b d . g . c e h i j k f


January 5, 2013       Programming and                     17
Postorder Traversal

• Recursively, perform the following three steps:
   – Visit the left subtree.
   – Visit the right subtree.
   – Visit the root.

   LEFT-RIGHT-ROOT




January 5, 2013     Programming and     18
Example:: postorder traversal
                                         10



        10                     20                  30



   20        30          40         25        50        60




   20 30 10                40 25 20 50 60 30 10




January 5, 2013   Programming and             19
a



                  b                           c



          d                       e                   f



              g           h               i


                                      j           k



         . g d . b h j k i e f c a


January 5, 2013       Programming and                     20
Implementations
 void inorder (node *root)                 void preorder (node *root)
{                                         {
   if (root != NULL)                         if (root != NULL)
   {                                         {
       inorder (root->left);                     printf (“%d “, root->element);
       printf (“%d “, root->element);            inorder (root->left);
       inorder (root->right);                    inorder (root->right);
    }                                         }
}                                         }

                        void postorder (node *root)
                       {
                          if (root != NULL)
                          {
                              inorder (root->left);
                              inorder (root->right);
                              printf (“%d “, root->element);
                           }
                       }

   January 5, 2013          Programming and                    21
A case study :: Expression Tree
                  *


        +                     -               Represents the expression:
                                                (a + b) * (c – (d + e))


a            b        c               +


                                  d       e


    Preorder traversal :: * + a b - c + d e
    Postorder traversal :: a b + c d e + - *

    January 5, 2013       Programming and                 22
Binary Search Tree (Revisited)
• Given a binary search tree, how to write a program to
  search for a given element?
• Easy to write a recursive program.
       int search (node *root, int key)
       {
          if (root != NULL)
          {
             if (root->element == key) return (1);
             else if (root->element > key)
                     search (root->lptr, key);
                  else
                     search (root->rptr, key);
          }
       }


January 5, 2013     Programming and                  23
Some Points to Note

• The following operations are a little complex
  and are not discussed here.
   – Inserting a node into a binary search tree.
   – Deleting a node from a binary search tree.




January 5, 2013     Programming and          24
Graph :: another important data structure

• Definition
   – A graph G={V,E} consists of a set of vertices V, and
     a set of edges E which connect pairs of vertices.
   – If the edges are undirected, G is called an
     undirected graph.
   – If at least one edge in G is directed, it is called a
     directed graph.




January 5, 2013   Programming and            25
How to represent a graph in a program?

• Many ways
   – Adjacency matrix
   – Incidence matrix, etc.




January 5, 2013   Programming and   26
Adjacency Matrix

                     e1                          e5
         1                         2                       6

    e2                                 e4             e6       e7
                     e3                     e8
         3                         4                       5


             .   1        2    3       4    5    6
             1   0        1    1       0    0    0
             2   1        0    0       1    1    1
             3   1        0    0       1    0    0
             4   0        1    1       0    1    0
             5   0        1    0       1    0    1
             6   0        1    0       0    1    0

January 5, 2013                Programming and                      27
Incidence Matrix

                  e1                           e5
         1                      2                             6

    e2                              e4              e6            e7
                  e3                      e8
         3                      4                             5


             .   e1   e2   e3   e4       e5 e6      e7   e8
             1   1    1    0    0         0 0       0    0
             2   1    0    0    1         1 1       0    0
             3   0    1    1    0         0 0       0    0
             4   0    0    1    1         0 0       0    1
             5   0    0    0    0         0 1       1    1
             6   0    0    0    0         1 0       1    0

January 5, 2013            Programming and                             28
January 5, 2013   Programming and   29

Más contenido relacionado

Más de mondalakash2012 (11)

Sn reaction
Sn reactionSn reaction
Sn reaction
 
Part i
Part iPart i
Part i
 
Elimination
EliminationElimination
Elimination
 
L12 complexity
L12 complexityL12 complexity
L12 complexity
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
L8 file
L8 fileL8 file
L8 file
 
L6 structure
L6 structureL6 structure
L6 structure
 
L4 functions
L4 functionsL4 functions
L4 functions
 
L3 control
L3 controlL3 control
L3 control
 
L2 number
L2 numberL2 number
L2 number
 
Struct examples
Struct examplesStruct examples
Struct examples
 

L11 tree

  • 1. Tree: A Non-linear Data Structure January 5, 2013 Programming and 1
  • 2. Background • Linked lists use dynamic memory allocation, and hence enjoys some advantages over static representations using arrays. • A problem – Searching an ordinary linked list for a given element. • Time complexity O(n). – Can the nodes in a linked list be rearranged so that we can search in O(nlog2n) time? January 5, 2013 Programming and 2
  • 3. Illustration 50 Level 1 30 75 Level 2 12 45 53 80 Level 3 20 35 48 78 85 Level 4 January 5, 2013 Programming and 3
  • 4. Binary Tree • A new data structure – Binary means two. • Definition – A binary tree is either empty, or it consists of a node called the root, together with two binary trees called the left subtree and the right subtree. Root Left Right subtree subtree January 5, 2013 Programming and 4
  • 5. Examples of Binary Trees January 5, 2013 Programming and 5
  • 6. Not Binary Trees January 5, 2013 Programming and 6
  • 7. Binary Search Trees • A particular form of binary tree suitable for searching. • Definition – A binary search tree is a binary tree that is either empty or in which each node contains a key that satisfies the following conditions: • All keys (if any) in the left subtree of the root precede the key in the root. • The key in the root precedes all keys (if any) in its right subtree. • The left and right subtrees of the root are again binary search trees. January 5, 2013 Programming and 7
  • 8. Examples 10 50 5 15 30 75 12 20 12 45 53 80 20 35 48 78 85 January 5, 2013 Programming and 8
  • 9. How to Implement a Binary Tree? • Two pointers in every node (left and right). struct nd { int element; struct nd *lptr; struct nd *rptr; }; typedef nd node; node *root; /* Will point to the root of the tree */ January 5, 2013 Programming and 9
  • 10. An Example a • Create the tree 10 b 5 20 c 15 d a = (node *) malloc (sizeof (node)); b = (node *) malloc (sizeof (node)); c = (node *) malloc (sizeof (node)); d = (node *) malloc (sizeof (node)); a->element = 10; a->lptr = b; a->rptr = c; b->element = 5; b->lptr = NULL; b->rptr = NULL; c->element = 20; c->lptr = d; c->rptr = NULL; d->element = 15; d->lptr = NULL; d->rptr = NULL; root = a; January 5, 2013 Programming and 10
  • 11. Traversal of Binary Trees • In many applications, it is required to move through all the nodes of a binary tree, visiting each node in turn. – For n nodes, there exists n! different orders. – Three traversal orders are most common: • Inorder traversal • Preorder traversal • Postorder traversal January 5, 2013 Programming and 11
  • 12. Inorder Traversal • Recursively, perform the following three steps: – Visit the left subtree. – Visit the root. – Visit the right subtree. LEFT-ROOT-RIGHT January 5, 2013 Programming and 12
  • 13. Example:: inorder traversal 10 10 20 30 20 30 40 25 50 60 20 10 30 40 20 25 10 50 30 60 January 5, 2013 Programming and 13
  • 14. a b c d e f g h i j k . d g b . a h e j i k c f January 5, 2013 Programming and 14
  • 15. Preorder Traversal • Recursively, perform the following three steps: – Visit the root. – Visit the left subtree. – Visit the right subtree. ROOT-LEFT-RIGHT January 5, 2013 Programming and 15
  • 16. Example:: preorder traversal 10 10 20 30 20 30 40 25 50 60 10 20 30 10 20 40 25 30 50 60 January 5, 2013 Programming and 16
  • 17. a b c d e f g h i j k a b d . g . c e h i j k f January 5, 2013 Programming and 17
  • 18. Postorder Traversal • Recursively, perform the following three steps: – Visit the left subtree. – Visit the right subtree. – Visit the root. LEFT-RIGHT-ROOT January 5, 2013 Programming and 18
  • 19. Example:: postorder traversal 10 10 20 30 20 30 40 25 50 60 20 30 10 40 25 20 50 60 30 10 January 5, 2013 Programming and 19
  • 20. a b c d e f g h i j k . g d . b h j k i e f c a January 5, 2013 Programming and 20
  • 21. Implementations void inorder (node *root) void preorder (node *root) { { if (root != NULL) if (root != NULL) { { inorder (root->left); printf (“%d “, root->element); printf (“%d “, root->element); inorder (root->left); inorder (root->right); inorder (root->right); } } } } void postorder (node *root) { if (root != NULL) { inorder (root->left); inorder (root->right); printf (“%d “, root->element); } } January 5, 2013 Programming and 21
  • 22. A case study :: Expression Tree * + - Represents the expression: (a + b) * (c – (d + e)) a b c + d e Preorder traversal :: * + a b - c + d e Postorder traversal :: a b + c d e + - * January 5, 2013 Programming and 22
  • 23. Binary Search Tree (Revisited) • Given a binary search tree, how to write a program to search for a given element? • Easy to write a recursive program. int search (node *root, int key) { if (root != NULL) { if (root->element == key) return (1); else if (root->element > key) search (root->lptr, key); else search (root->rptr, key); } } January 5, 2013 Programming and 23
  • 24. Some Points to Note • The following operations are a little complex and are not discussed here. – Inserting a node into a binary search tree. – Deleting a node from a binary search tree. January 5, 2013 Programming and 24
  • 25. Graph :: another important data structure • Definition – A graph G={V,E} consists of a set of vertices V, and a set of edges E which connect pairs of vertices. – If the edges are undirected, G is called an undirected graph. – If at least one edge in G is directed, it is called a directed graph. January 5, 2013 Programming and 25
  • 26. How to represent a graph in a program? • Many ways – Adjacency matrix – Incidence matrix, etc. January 5, 2013 Programming and 26
  • 27. Adjacency Matrix e1 e5 1 2 6 e2 e4 e6 e7 e3 e8 3 4 5 . 1 2 3 4 5 6 1 0 1 1 0 0 0 2 1 0 0 1 1 1 3 1 0 0 1 0 0 4 0 1 1 0 1 0 5 0 1 0 1 0 1 6 0 1 0 0 1 0 January 5, 2013 Programming and 27
  • 28. Incidence Matrix e1 e5 1 2 6 e2 e4 e6 e7 e3 e8 3 4 5 . e1 e2 e3 e4 e5 e6 e7 e8 1 1 1 0 0 0 0 0 0 2 1 0 0 1 1 1 0 0 3 0 1 1 0 0 0 0 0 4 0 0 1 1 0 0 0 1 5 0 0 0 0 0 1 1 1 6 0 0 0 0 1 0 1 0 January 5, 2013 Programming and 28
  • 29. January 5, 2013 Programming and 29