SlideShare une entreprise Scribd logo
1  sur  24
Télécharger pour lire hors ligne
Lecture 7




                                     Tree
                                  (Data Structure)




                        Abdisalam Issa-Salwe

                         Taibah University
            College of Computer Science & Engineering
                  Computer Science Department




Outline

 Binary trees
 Binary search trees
 AVL trees
 B trees
 Applications




                                                        2




                                                            1
Tree ADT
  A tree is a collection (may be empty) of nodes,
  containing:
    a distinguished node called the root r,
    zero or more non-empty subtrees T1, T2, …, Tk,
    A directed edge from the root r to the root of each
    subtree.
                          root




                           T2   …
                  T1                  Tk
                                                          3




Terminologies


                  parent          root


       children                              siblings

subtrees
                                grandchildren


                                                          4




                                                              2
Terminologies
          ancestor
         path

length of path
                      depth
                     length of path from the root
                       descendant



                                              5




Terminologies




height




                                              6




                                                    3
Tree: Implementation
class TreeNode
{
    Object       element;
    TreeNode     firstChild;
    TreeNode     nextSibling;
}

                  nextSibling=null



                             nextSibling=null
                 nextsibling firstChild=null
                 firstChild=null


                nextSibling=null
 firstChild=null firstChild=null
                                                7




Binary Trees
 A tree in which no node can have more
 than 2 children.




                                                8




                                                    4
Binary Tree: Implementation
Class     BinaryNode
{
                                        3
  Object       element;
  BinaryNode   left;
  BinaryNode   right;
}
                        5     node.element=3
                              node.left=node
                              node.right=node
     9
node.element=9 node.element=5
node.left=null node.left=node
node.right=null node.right=null
                                                     9




Binary Tree: Implementation
class BinaryNode
{ // Constructors
BinaryNode ( Comparable theElement )
{ this ( theElement, null, null);           }

BinaryNode
(Comparable theElement, BinaryNode lt, BinaryNode
  rt)
{ element = theElement;
  left     = lt;
  right    = rt;                                }

// Friendly data; accessible by other package
  routines
  Comparable element;      // The data in the node
  BinaryNode left;         // Left child
  BinaryNode right;        // Right child            10
}




                                                          5
Binary Search Trees

Properties of a binary search tree T

1.        T is a binary tree.
2.       For each node n in T, whose left subtree
         is Tl and right subtree is Tr,
     •     the item in each node in Tl is smaller than
           the item in n.
     •     the item in each node in Tr is larger than the
           item in n.
                                                        11




Example
                                 8

                        3                    11


                    2       4            9        12


                1                    6


                             5               7
                                                        12




                                                             6
Binary Search Trees
public class BinarySearchTree
{ public BinarySearchTree( )
  { root = null;                                         }
  public void insert( Comparable x )
  { root = insert( x, root );                            }
  public void remove( Comparable x )
  { root = remove( x, root );                            }

  public Comparable find( Comparable x )
  { return elementAt( find( x, root ) );             }
  public void makeEmpty( )
  { root = null;                                         }
  ...
  private BinaryNode root;                               }

                                                             13




FIND
                               8
Find 6
                       <
                   3                       11
                           >

               2           4           9        12
                               >
           1                       6


                           5               7
                                                             14




                                                                  7
Method find
private BinaryNode find
  ( Comparable x, BinaryNode t )
{
  if( t == null )   return null;
  if( x.compareTo( t.element ) < 0 )
     return find( x, t.left );
  else if( x.compareTo( t.element ) > 0 )
     return find( x, t.right );
  else return t;              // Match
}


                                                  15




INSERT
                                10
                            8
Insert 10                           >
                    3                   11
                                    <

                2       4           9        12


            1                   6           10


                        5               7
                                                  16




                                                       8
Method insert
private BinaryNode insert
          ( Comparable x, BinaryNode t )
{ if( t == null )
     t = new BinaryNode( x, null, null );
  else if( x.compareTo( t.element ) < 0 )
     t.left = insert( x, t.left );
  else if( x.compareTo( t.element ) > 0 )
     t.right = insert( x, t.right );
  else    ; // Duplicate; do nothing
  return t;
}
                                                    17




FindMax, FindMin
                          8

                  3                   11


              2       4           9        12 max


    min   1                   6


                      5               7
                                                    18




                                                         9
Methods findMin & findMax
private BinaryNode findMin( BinaryNode t )
{ if( t == null )
      return null;
  else if( t.left == null )
      return t;
  return findMin( t.left );
}
 private BinaryNode findMax( BinaryNode t )
{ if( t != null )
      while( t.right != null )
            t = t.right;
  return t;
}
                                                   19




REMOVE
Remove 7                       11

                   3                     13


               2           7        12        14


           1       5                9

               4       6                10
                                                   20




                                                        10
REMOVE
Remove 7                       11

                   3                     13


               2           7        12        14


           1                        9

                               8        10
                                                   21




REMOVE
Remove 7                       11

                   3                     13


               2           7        12        14


           1       5

               4       6
                                                   22




                                                        11
Method Remove
private BinaryNode remove(Comparable x,BinaryNode t)
{ if(t == null) return t; // Item not found;do nothing
  if( x.compareTo(t.element) < 0 )
     t.left = remove(x, t.left);
  else if ( x.compareTo(t.element) > 0 )
     t.right = remove(x, t.right);
  else if (t.left!=null && t.right!=null) // 2 child
  { t.element = findMin(t.right).element;
     t.right = remove(t.element, t.right);
  }
  else
     t = (t.left != null) ? t.left : t.right;
  return t;
}
                                                  23




 AVL Trees

    An AVL tree is a binary search tree with
    a balance condition. A self-balancing
    binary search tree.
    AVL tree is named after G.M. Adelson-
    Velskii and E.M. Landis.
 Balance condition
    For every node in the tree, the height of
    the left & right subtrees can differ by at
    most 1.
                                                  24




                                                         12
AVL Trees (cont…)
    The balance factor of a node is the height of
    its left subtree minus the height of its right
    subtree (sometimes opposite) and a node with
    balance factor 1, 0, or −1 is considered
    balanced.
    A node with any other balance factor is
    considered unbalanced and requires
    rebalancing the tree.
    The balance factor is either stored directly at
    each node or computed from the heights of the
    subtrees.

                                                                     25




AVL Trees
                  11                                  11

          3                 13            3                     13

      2       7        12        14   2           7        12        14


1         5        8              1       5
                                                      not AVL tree
          AVL tree
                                      4       6
                                                                     26




                                                                          13
Single Right Rotation

                k2
                                        k1
          k1          Zh
                                                  k2

                Yh          Xh+1         Yh            Zh
   Xh+1



                                                              27




Single Left Rotation


          k2
                                             k1
                k1
   Xh                              k2
           Yh                                          Zh+1
                     Zh+1   Xh               Yh




                                                              28




                                                                   14
Height of AVL Tree




If N is the number of nodes in an AVL tree, the height
of the tree, h(N), is approximately 1.44 log(N+2)-.328.

                                                                    29




 Inorder Traversal
                                            +
(a – (b * (c / d))) + (e – f)
                                    -                       -


                                a       *           e           f


                                    b           /


                                        c               d
                                                                    30




                                                                         15
Method inorder


public static void inorder
  (BinaryNode t)
{ if ( t!=null )
  { inorder(t.left);
    System.out.println(t.element);
    inorder(t.right);
  }
}
                                                         31




Preorder Traversal
                                 +
+ – a*b/cd–ef
                         -                       -


                     a       *           e           f


                         b           /


                             c               d
                                                         32




                                                              16
Method preorder

public static void preorder
  (BinaryNode t)
{ if ( t!=null )
  { System.out.println(t.element);
    inorder(t.left);
    inorder(t.right);
  }
}
                                                          33




Postorder Traversal
                                  +
abcd/*–ef–+
                          -                       -


                      a       *           e           f


                          b           /


                              c               d
                                                          34




                                                               17
Method postorder


public static void postorder (BinaryNode
  t)
{ if ( t!=null )
  { inorder(t.left);
     inorder(t.right);
     System.out.println(t.element);
  }
}

                                                 35




B tree
N-ary tree
Increase the breadth of trees to decrease the height
Used for indexing of large amount of data (stored in
disk)




                                                 36




                                                       18
B tree (cont…)
 Unlike a binary-tree, each node of a b-tree may
 have a variable number of keys and children.
 The keys are stored in non-decreasing order.
 Each key has an associated child that is the root
 of a subtree containing all nodes with keys less
 than or equal to the key but greater than the
 preceeding key.
 A node also has an additional rightmost child
 that is the root for a subtree containing all keys
 greater than any keys in the node.
                                                      37




Example

                   12   52   78

4    8
                                       83   91

05      19    26   37   46   60   69
168
279                                         79   85   93
   11 13 20    27 38 49 54        61   70   80   86   95
   12 14 21    28 44 50 56        62   71   81   90   97
      17 22    31 45    57        66   76   82        98
      19 26    35       59        67   77   83        99
                        60

                                                      38




                                                           19
B tree (cont…)
 A b-tree has a minumum number of
 allowable children for each node known as
 the minimization factor.
 If t is this minimization factor, every node
 must have at least t - 1 keys.
 Under certain circumstances, the root
 node is allowed to violate this property by
 having fewer than t - 1 keys.
 Every node may have at most 2t - 1 keys
 or, equivalently, 2t children.
                                            39




Height of B-Tree

 For n greater than or equal to one, the
 height of an n-key b-tree T of height h with
 a minimum degree t greater than or equal
 to 2,




                                            40




                                                 20
Properties of B Trees

 For an M-ary B tree:
  The root has up to M children.
  Non-leaf nodes store up to M-1 keys, and
  have between M/2 and M children, except
  the root.
  All data items are stored at leaves.
  All leaves have to same depth, and store
  between L/2 and L data items.

                                                        41




 Search
Search for 66
                     12   52   78

  4     8
                                         83   91

  05      19    26   37   46   60   69
  168
  279                                         79   85   93
     11 13 20    27 38 49 54        61   70   80   86   95
     12 14 21    28 44 50 56        62   71   81   90   97
        17 22    31 45    57        66   76   82        98
        19 26    35       59        67   77   83        99
                          60

                                                        42




                                                             21
Insert
Insert 55
                                                    Split leave
                   12       52    78

4       8
                                               83    91

05      19 26 37 46 60                   69
16 8
27 9                                                 79   85   93
                     54                  61    70    80   86   95
   11 13 20 27 38 49
   12 14 21 28 44 50 56                  62    71    81   90   97
      17 22 31 45    57                  66    76    82        98
      19 26 35       59                  67    77    83        99
                     60

                                                               43




 Insert
    Insert 32                Insert key 31
                                                    Split leave
                       12    52     78

    4       8           Insert key 31
                                               83    91

    05      19    26    37       46 60    69
    168
    279                                              79   85 93
       11 13 20   27 38 49 54            61    70    80   86 95
       12 14 21   28 44 50 56            62    71    81   90 97
          17 22   31 45    57            66    76    82      98
          19 26            59            67    77    83      99
                  35
                  36       60
                                                               44




                                                                    22
B+ tree
 B+ tree or B plus tree is a type of tree which
 represents sorted data in a way that allows for
 efficient insertion, retrieval and removal of
 records, each of which is identified by a key.
 It is a dynamic, multilevel index, with maximum
 and minimum bounds on the number of keys in
 each index segment (usually called a "block" or
 "node").
 In a B+ tree, in contrast to a B-tree, all records
 are stored at the leaf level of the tree; only keys
 are stored in interior nodes.

                                                       45




B+ tree (cont…)
 The primary value of a B+ tree is in storing
 data for efficient retrieval in a block-
 oriented storage context — in particular,
 file systems.
 This is primarily because unlike binary
 search trees, B+ trees have very high
 fanout (typically on the order of 100 or
 more), which reduces the number of I/O
 operations required to find an element in
 the tree.
                                                       46




                                                            23
A simple B+ tree example linking the keys 1–7
 to data values d1-d7. The linked list (red)
 allows rapid in-order traversal.




                                                 47




References
 Abdisalam Issa-Salwe, Taibah University,
 Madinah, Saudi Arabia.




                                                 48




                                                      24

Contenu connexe

Tendances

Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosGenevaJUG
 
XSLT 1 and XPath Quick Reference (from mulberrytech.com)
XSLT 1 and XPath Quick Reference (from mulberrytech.com)XSLT 1 and XPath Quick Reference (from mulberrytech.com)
XSLT 1 and XPath Quick Reference (from mulberrytech.com)FrescatiStory
 
JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...
JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...
JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...JSFestUA
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfilesmrecedu
 
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...Frank Nielsen
 
Ян Малаховски. Введение в Agda
Ян Малаховски. Введение в AgdaЯн Малаховски. Введение в Agda
Ян Малаховски. Введение в AgdaFProg
 
Doctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 ParisDoctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 Parispablodip
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 

Tendances (20)

JavaYDL7
JavaYDL7JavaYDL7
JavaYDL7
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
Chtp412
Chtp412Chtp412
Chtp412
 
C Language Unit-7
C Language Unit-7C Language Unit-7
C Language Unit-7
 
Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian Dragos
 
XSLT 1 and XPath Quick Reference (from mulberrytech.com)
XSLT 1 and XPath Quick Reference (from mulberrytech.com)XSLT 1 and XPath Quick Reference (from mulberrytech.com)
XSLT 1 and XPath Quick Reference (from mulberrytech.com)
 
JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...
JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...
JS Fest 2019. Max Koretskiy. A sneak peek into super optimized code in JS fra...
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
 
About Array
About ArrayAbout Array
About Array
 
Lecture19
Lecture19Lecture19
Lecture19
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfiles
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
E2
E2E2
E2
 
Array
ArrayArray
Array
 
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
 
binary_trees3
binary_trees3binary_trees3
binary_trees3
 
Ян Малаховски. Введение в Agda
Ян Малаховски. Введение в AgdaЯн Малаховски. Введение в Agda
Ян Малаховски. Введение в Agda
 
Doctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 ParisDoctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 Paris
 
Learn How to Master Solr1 4
Learn How to Master Solr1 4Learn How to Master Solr1 4
Learn How to Master Solr1 4
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 

En vedette (10)

SIP, CWE Presentation
SIP, CWE PresentationSIP, CWE Presentation
SIP, CWE Presentation
 
Lecture9 (cs212)(referencing)
Lecture9 (cs212)(referencing)Lecture9 (cs212)(referencing)
Lecture9 (cs212)(referencing)
 
Social Media
Social MediaSocial Media
Social Media
 
Lesson 2 - Blogs And Commenting
Lesson 2 - Blogs And CommentingLesson 2 - Blogs And Commenting
Lesson 2 - Blogs And Commenting
 
Lecture1 is322 data&amp;infomanag(introduction)(old curr)
Lecture1 is322 data&amp;infomanag(introduction)(old curr)Lecture1 is322 data&amp;infomanag(introduction)(old curr)
Lecture1 is322 data&amp;infomanag(introduction)(old curr)
 
Lecture8 (cs212)(survey)
Lecture8 (cs212)(survey)Lecture8 (cs212)(survey)
Lecture8 (cs212)(survey)
 
Lecture6 is353(ea&amp;data viewpoint )
Lecture6 is353(ea&amp;data viewpoint )Lecture6 is353(ea&amp;data viewpoint )
Lecture6 is353(ea&amp;data viewpoint )
 
The 10 key things i've learnt about IWBs - Interactive White Boards
The 10 key things i've learnt about IWBs - Interactive White BoardsThe 10 key things i've learnt about IWBs - Interactive White Boards
The 10 key things i've learnt about IWBs - Interactive White Boards
 
Lecture2 is331 data&amp;infomanag(databaseenv)
Lecture2 is331 data&amp;infomanag(databaseenv)Lecture2 is331 data&amp;infomanag(databaseenv)
Lecture2 is331 data&amp;infomanag(databaseenv)
 
Lecture1 data structure(introduction)
Lecture1 data structure(introduction)Lecture1 data structure(introduction)
Lecture1 data structure(introduction)
 

Similaire à Lecture7 data structure(tree)

Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVLKatang Isip
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bstSSE_AndyLi
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxfarrahkur54
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphsmaznabili
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfajaycosmeticslg
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxVeerannaKotagi1
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6Teksify
 
1.2 operations of tree representations
1.2 operations of tree representations 1.2 operations of tree representations
1.2 operations of tree representations Krish_ver2
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in indiaEdhole.com
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfssuser034ce1
 

Similaire à Lecture7 data structure(tree) (20)

Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
1.2 operations of tree representations
1.2 operations of tree representations 1.2 operations of tree representations
1.2 operations of tree representations
 
Tree & bst
Tree & bstTree & bst
Tree & bst
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdf
 

Plus de Taibah University, College of Computer Science & Engineering

Plus de Taibah University, College of Computer Science & Engineering (20)

Lecture 1- Computer Organization and Architecture.pdf
Lecture 1- Computer Organization and Architecture.pdfLecture 1- Computer Organization and Architecture.pdf
Lecture 1- Computer Organization and Architecture.pdf
 
The paper the welfare state of the somali nation - a possible solution to t...
The paper   the welfare state of the somali nation - a possible solution to t...The paper   the welfare state of the somali nation - a possible solution to t...
The paper the welfare state of the somali nation - a possible solution to t...
 
Colonial intrusion and_the_somali_resistance
Colonial intrusion and_the_somali_resistanceColonial intrusion and_the_somali_resistance
Colonial intrusion and_the_somali_resistance
 
Lecture 3 (Contemporary approaches to Information Systems)
Lecture 3 (Contemporary approaches to Information Systems)Lecture 3 (Contemporary approaches to Information Systems)
Lecture 3 (Contemporary approaches to Information Systems)
 
Lecture 7 (business-level strategy and the value chain model)
Lecture 7  (business-level strategy and the value chain model)Lecture 7  (business-level strategy and the value chain model)
Lecture 7 (business-level strategy and the value chain model)
 
Lecture 4 (using information technology for competitive advantage)
Lecture 4 (using information technology for competitive advantage)Lecture 4 (using information technology for competitive advantage)
Lecture 4 (using information technology for competitive advantage)
 
Lecture 2 (major types of information systems in organizations)
Lecture 2 (major types of information systems in organizations)Lecture 2 (major types of information systems in organizations)
Lecture 2 (major types of information systems in organizations)
 
Practical session 1 (critical path analaysis)
Practical session 1 (critical path analaysis)Practical session 1 (critical path analaysis)
Practical session 1 (critical path analaysis)
 
Chapter 2 modeling the process and life-cycle
Chapter 2  modeling the process and life-cycleChapter 2  modeling the process and life-cycle
Chapter 2 modeling the process and life-cycle
 
Historical Perspective on the Challenge Facing the Somali Sacral Unity
Historical Perspective on the Challenge Facing the Somali Sacral UnityHistorical Perspective on the Challenge Facing the Somali Sacral Unity
Historical Perspective on the Challenge Facing the Somali Sacral Unity
 
Colonial intrusion and the Somali Resistance
Colonial intrusion and the Somali ResistanceColonial intrusion and the Somali Resistance
Colonial intrusion and the Somali Resistance
 
Lecture 8 (information systems and strategy planning)
Lecture 8  (information systems and strategy planning)Lecture 8  (information systems and strategy planning)
Lecture 8 (information systems and strategy planning)
 
Lecture 4 (using information technology for competitive advantage)
Lecture 4 (using information technology for competitive advantage)Lecture 4 (using information technology for competitive advantage)
Lecture 4 (using information technology for competitive advantage)
 
Lecture4 is353-ea(fea)
Lecture4 is353-ea(fea)Lecture4 is353-ea(fea)
Lecture4 is353-ea(fea)
 
Lecture3 is353-ea(togaf)
Lecture3 is353-ea(togaf)Lecture3 is353-ea(togaf)
Lecture3 is353-ea(togaf)
 
Lecture2 is353-ea(the zachma framework)
Lecture2 is353-ea(the zachma framework)Lecture2 is353-ea(the zachma framework)
Lecture2 is353-ea(the zachma framework)
 
Lecture1 is353-enterprise architectureconcept)
Lecture1 is353-enterprise architectureconcept)Lecture1 is353-enterprise architectureconcept)
Lecture1 is353-enterprise architectureconcept)
 
Lecture1 is313-(is-innovation&amp;tech)
Lecture1 is313-(is-innovation&amp;tech)Lecture1 is313-(is-innovation&amp;tech)
Lecture1 is313-(is-innovation&amp;tech)
 
Lab 1 (cs351) (computer tech &amp; c++)
Lab 1 (cs351) (computer tech &amp; c++)Lab 1 (cs351) (computer tech &amp; c++)
Lab 1 (cs351) (computer tech &amp; c++)
 
Lecture1 is441-(intro toe-commerce)
Lecture1 is441-(intro toe-commerce)Lecture1 is441-(intro toe-commerce)
Lecture1 is441-(intro toe-commerce)
 

Dernier

Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 

Dernier (20)

Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 

Lecture7 data structure(tree)

  • 1. Lecture 7 Tree (Data Structure) Abdisalam Issa-Salwe Taibah University College of Computer Science & Engineering Computer Science Department Outline Binary trees Binary search trees AVL trees B trees Applications 2 1
  • 2. Tree ADT A tree is a collection (may be empty) of nodes, containing: a distinguished node called the root r, zero or more non-empty subtrees T1, T2, …, Tk, A directed edge from the root r to the root of each subtree. root T2 … T1 Tk 3 Terminologies parent root children siblings subtrees grandchildren 4 2
  • 3. Terminologies ancestor path length of path depth length of path from the root descendant 5 Terminologies height 6 3
  • 4. Tree: Implementation class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; } nextSibling=null nextSibling=null nextsibling firstChild=null firstChild=null nextSibling=null firstChild=null firstChild=null 7 Binary Trees A tree in which no node can have more than 2 children. 8 4
  • 5. Binary Tree: Implementation Class BinaryNode { 3 Object element; BinaryNode left; BinaryNode right; } 5 node.element=3 node.left=node node.right=node 9 node.element=9 node.element=5 node.left=null node.left=node node.right=null node.right=null 9 Binary Tree: Implementation class BinaryNode { // Constructors BinaryNode ( Comparable theElement ) { this ( theElement, null, null); } BinaryNode (Comparable theElement, BinaryNode lt, BinaryNode rt) { element = theElement; left = lt; right = rt; } // Friendly data; accessible by other package routines Comparable element; // The data in the node BinaryNode left; // Left child BinaryNode right; // Right child 10 } 5
  • 6. Binary Search Trees Properties of a binary search tree T 1. T is a binary tree. 2. For each node n in T, whose left subtree is Tl and right subtree is Tr, • the item in each node in Tl is smaller than the item in n. • the item in each node in Tr is larger than the item in n. 11 Example 8 3 11 2 4 9 12 1 6 5 7 12 6
  • 7. Binary Search Trees public class BinarySearchTree { public BinarySearchTree( ) { root = null; } public void insert( Comparable x ) { root = insert( x, root ); } public void remove( Comparable x ) { root = remove( x, root ); } public Comparable find( Comparable x ) { return elementAt( find( x, root ) ); } public void makeEmpty( ) { root = null; } ... private BinaryNode root; } 13 FIND 8 Find 6 < 3 11 > 2 4 9 12 > 1 6 5 7 14 7
  • 8. Method find private BinaryNode find ( Comparable x, BinaryNode t ) { if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return find( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return find( x, t.right ); else return t; // Match } 15 INSERT 10 8 Insert 10 > 3 11 < 2 4 9 12 1 6 10 5 7 16 8
  • 9. Method insert private BinaryNode insert ( Comparable x, BinaryNode t ) { if( t == null ) t = new BinaryNode( x, null, null ); else if( x.compareTo( t.element ) < 0 ) t.left = insert( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = insert( x, t.right ); else ; // Duplicate; do nothing return t; } 17 FindMax, FindMin 8 3 11 2 4 9 12 max min 1 6 5 7 18 9
  • 10. Methods findMin & findMax private BinaryNode findMin( BinaryNode t ) { if( t == null ) return null; else if( t.left == null ) return t; return findMin( t.left ); } private BinaryNode findMax( BinaryNode t ) { if( t != null ) while( t.right != null ) t = t.right; return t; } 19 REMOVE Remove 7 11 3 13 2 7 12 14 1 5 9 4 6 10 20 10
  • 11. REMOVE Remove 7 11 3 13 2 7 12 14 1 9 8 10 21 REMOVE Remove 7 11 3 13 2 7 12 14 1 5 4 6 22 11
  • 12. Method Remove private BinaryNode remove(Comparable x,BinaryNode t) { if(t == null) return t; // Item not found;do nothing if( x.compareTo(t.element) < 0 ) t.left = remove(x, t.left); else if ( x.compareTo(t.element) > 0 ) t.right = remove(x, t.right); else if (t.left!=null && t.right!=null) // 2 child { t.element = findMin(t.right).element; t.right = remove(t.element, t.right); } else t = (t.left != null) ? t.left : t.right; return t; } 23 AVL Trees An AVL tree is a binary search tree with a balance condition. A self-balancing binary search tree. AVL tree is named after G.M. Adelson- Velskii and E.M. Landis. Balance condition For every node in the tree, the height of the left & right subtrees can differ by at most 1. 24 12
  • 13. AVL Trees (cont…) The balance factor of a node is the height of its left subtree minus the height of its right subtree (sometimes opposite) and a node with balance factor 1, 0, or −1 is considered balanced. A node with any other balance factor is considered unbalanced and requires rebalancing the tree. The balance factor is either stored directly at each node or computed from the heights of the subtrees. 25 AVL Trees 11 11 3 13 3 13 2 7 12 14 2 7 12 14 1 5 8 1 5 not AVL tree AVL tree 4 6 26 13
  • 14. Single Right Rotation k2 k1 k1 Zh k2 Yh Xh+1 Yh Zh Xh+1 27 Single Left Rotation k2 k1 k1 Xh k2 Yh Zh+1 Zh+1 Xh Yh 28 14
  • 15. Height of AVL Tree If N is the number of nodes in an AVL tree, the height of the tree, h(N), is approximately 1.44 log(N+2)-.328. 29 Inorder Traversal + (a – (b * (c / d))) + (e – f) - - a * e f b / c d 30 15
  • 16. Method inorder public static void inorder (BinaryNode t) { if ( t!=null ) { inorder(t.left); System.out.println(t.element); inorder(t.right); } } 31 Preorder Traversal + + – a*b/cd–ef - - a * e f b / c d 32 16
  • 17. Method preorder public static void preorder (BinaryNode t) { if ( t!=null ) { System.out.println(t.element); inorder(t.left); inorder(t.right); } } 33 Postorder Traversal + abcd/*–ef–+ - - a * e f b / c d 34 17
  • 18. Method postorder public static void postorder (BinaryNode t) { if ( t!=null ) { inorder(t.left); inorder(t.right); System.out.println(t.element); } } 35 B tree N-ary tree Increase the breadth of trees to decrease the height Used for indexing of large amount of data (stored in disk) 36 18
  • 19. B tree (cont…) Unlike a binary-tree, each node of a b-tree may have a variable number of keys and children. The keys are stored in non-decreasing order. Each key has an associated child that is the root of a subtree containing all nodes with keys less than or equal to the key but greater than the preceeding key. A node also has an additional rightmost child that is the root for a subtree containing all keys greater than any keys in the node. 37 Example 12 52 78 4 8 83 91 05 19 26 37 46 60 69 168 279 79 85 93 11 13 20 27 38 49 54 61 70 80 86 95 12 14 21 28 44 50 56 62 71 81 90 97 17 22 31 45 57 66 76 82 98 19 26 35 59 67 77 83 99 60 38 19
  • 20. B tree (cont…) A b-tree has a minumum number of allowable children for each node known as the minimization factor. If t is this minimization factor, every node must have at least t - 1 keys. Under certain circumstances, the root node is allowed to violate this property by having fewer than t - 1 keys. Every node may have at most 2t - 1 keys or, equivalently, 2t children. 39 Height of B-Tree For n greater than or equal to one, the height of an n-key b-tree T of height h with a minimum degree t greater than or equal to 2, 40 20
  • 21. Properties of B Trees For an M-ary B tree: The root has up to M children. Non-leaf nodes store up to M-1 keys, and have between M/2 and M children, except the root. All data items are stored at leaves. All leaves have to same depth, and store between L/2 and L data items. 41 Search Search for 66 12 52 78 4 8 83 91 05 19 26 37 46 60 69 168 279 79 85 93 11 13 20 27 38 49 54 61 70 80 86 95 12 14 21 28 44 50 56 62 71 81 90 97 17 22 31 45 57 66 76 82 98 19 26 35 59 67 77 83 99 60 42 21
  • 22. Insert Insert 55 Split leave 12 52 78 4 8 83 91 05 19 26 37 46 60 69 16 8 27 9 79 85 93 54 61 70 80 86 95 11 13 20 27 38 49 12 14 21 28 44 50 56 62 71 81 90 97 17 22 31 45 57 66 76 82 98 19 26 35 59 67 77 83 99 60 43 Insert Insert 32 Insert key 31 Split leave 12 52 78 4 8 Insert key 31 83 91 05 19 26 37 46 60 69 168 279 79 85 93 11 13 20 27 38 49 54 61 70 80 86 95 12 14 21 28 44 50 56 62 71 81 90 97 17 22 31 45 57 66 76 82 98 19 26 59 67 77 83 99 35 36 60 44 22
  • 23. B+ tree B+ tree or B plus tree is a type of tree which represents sorted data in a way that allows for efficient insertion, retrieval and removal of records, each of which is identified by a key. It is a dynamic, multilevel index, with maximum and minimum bounds on the number of keys in each index segment (usually called a "block" or "node"). In a B+ tree, in contrast to a B-tree, all records are stored at the leaf level of the tree; only keys are stored in interior nodes. 45 B+ tree (cont…) The primary value of a B+ tree is in storing data for efficient retrieval in a block- oriented storage context — in particular, file systems. This is primarily because unlike binary search trees, B+ trees have very high fanout (typically on the order of 100 or more), which reduces the number of I/O operations required to find an element in the tree. 46 23
  • 24. A simple B+ tree example linking the keys 1–7 to data values d1-d7. The linked list (red) allows rapid in-order traversal. 47 References Abdisalam Issa-Salwe, Taibah University, Madinah, Saudi Arabia. 48 24