presentation of data structure

Upload: softway096517

Post on 08-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 presentation of data structure

    1/112

    Bismillah

  • 8/7/2019 presentation of data structure

    2/112

    Presentation

    Muhammad Irfan Khan (S-31021)Sheraz Khan (S-31030)

    BE(SE) -16

    Data structure

  • 8/7/2019 presentation of data structure

    3/112

  • 8/7/2019 presentation of data structure

    4/112

    represented via a tree data

    structure

    leaf nodes

    root nodepresident

    VP VP VP VP

    mgr mgr

    mgr

    mgr

  • 8/7/2019 presentation of data structure

    5/112

    Binary Trees

    A binary tree is defined recursively. Eitherit is empty (the base case) or it consists of

    a rootand two subtrees, each of which is a

    binary tree

    Binary trees and trees typically are

    represented using references as dynamic

    links, though it is possible to use fixedrepresentations like arrays

    leaf nodes

    root node

  • 8/7/2019 presentation of data structure

    6/112

    Tree Data Structures

    There are a number of applications where linear

    data structures are not appropriate.

    Consider a genealogy tree of a family.Mohammad Aslam Khan

    Sohail Aslam Javed Aslam Yasmeen Aslam

    SaadHaaris Qasim Asim Fahd Ahmad Sara Omer

  • 8/7/2019 presentation of data structure

    7/112

    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 thatrequire searching, linear data structures are not

    suitable.

    We will focus our attention on binary trees.

  • 8/7/2019 presentation of data structure

    8/112

    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 thetree.

  • 8/7/2019 presentation of data structure

    9/112

    Binary Tree

    Binary tree with 9 nodes.

    A

    B

    D

    H

    C

    E F

    G I

  • 8/7/2019 presentation of data structure

    10/112

    Binary Tree

    A

    B

    D

    H

    C

    E F

    G I

    Left subtree

    root

    Right subtree

  • 8/7/2019 presentation of data structure

    11/112

    Binary Tree

    Recursive definition

    A

    B

    D

    H

    C

    E F

    G ILeft subtree

    root

    Right subtree

  • 8/7/2019 presentation of data structure

    12/112

  • 8/7/2019 presentation of data structure

    13/112

    Binary Tree

    Recursive definition

    A

    B

    D

    H

    C

    E F

    G Iroot

  • 8/7/2019 presentation of data structure

    14/112

    Binary Tree

    Recursive definition

    A

    B

    D

    H

    C

    E F

    G I

    root

    Right subtree

  • 8/7/2019 presentation of data structure

    15/112

    Binary Tree

    Recursive definition

    A

    B

    D

    H

    C

    E F

    G I

    root

    Right subtreeLeft subtree

  • 8/7/2019 presentation of data structure

    16/112

    Not a Tree

    Structures that are not trees.

    A

    B

    D

    H

    C

    E F

    G I

  • 8/7/2019 presentation of data structure

    17/112

    Not a Tree

    Structures that are not trees.

    A

    B

    D

    H

    C

    E F

    G I

  • 8/7/2019 presentation of data structure

    18/112

    Not a Tree

    Structures that are not trees.

    A

    B

    D

    H

    C

    E F

    G I

  • 8/7/2019 presentation of data structure

    19/112

    Binary Tree: Terminology

    A

    B

    D

    H

    C

    E F

    G I

    parent

    Left descendant Right descendant

    Leaf nodes Leaf nodes

  • 8/7/2019 presentation of data structure

    20/112

    Binary Tree

    If every non-leaf node in a binary tree has non-emptyleft and right subtrees, the tree is termed a strictlybinary tree.

    A

    B

    D

    H

    C

    E F

    G I

    J

    K

  • 8/7/2019 presentation of data structure

    21/112

    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.

  • 8/7/2019 presentation of data structure

    22/112

    Level of a Binary Tree Node

    A

    B

    D

    H

    C

    E F

    G I

    1

    0

    1

    2 2 2

    3 3 3

    Level 0

    Level 1

    Level 2

    Level 3

  • 8/7/2019 presentation of data structure

    23/112

    Complete Binary Tree

    A complete binary tree of depth d is the strictly binaryall of whose leaves are at level d.

    A

    B

    N

    C

    G

    O

    1

    0

    1

    2

    3 3L

    F

    M

    2

    3 3H

    D

    I

    2

    3 J

    E

    K

    2

    3

  • 8/7/2019 presentation of data structure

    24/112

    Complete Binary Tree

    A

    B

    Level 0: 20 nodes

    H

    D

    I

    E

    J K

    C

    L

    F

    M

    G

    N O

    Level 1: 21 nodes

    Level 2: 22 nodes

    Level 3: 23 nodes

  • 8/7/2019 presentation of data structure

    25/112

    Complete Binary Tree

    At level k, there are 2k nodes.

    Total number of nodes in the tree of depth d:

    20+ 21+ 22 + . + 2d = 2j = 2d+1 1

    In a complete binary tree, there are 2d leaf

    nodes and (2d - 1) non-leaf (inner) nodes.

    j=0

    d

  • 8/7/2019 presentation of data structure

    26/112

    Complete Binary Tree

    If the tree is built out of n nodes then

    n = 2d+1 1

    or log2(n+1) = d+1or d = log2(n+1) 1

    I.e., the depth of the complete binary tree built using nnodes will be log2(n+1) 1.

    For example, for n=100,000, log2(100001) is less than20; the tree would be 20 levels deep.

    The significance of this shallowness will becomeevident later.

  • 8/7/2019 presentation of data structure

    27/112

    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.

  • 8/7/2019 presentation of data structure

    28/112

    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.

  • 8/7/2019 presentation of data structure

    29/112

    Operations on Binary Tree

    In order to construct a binary tree, the following

    can be useful:

    setLeft(p,x) creates the left child node of p. Thechild node contains the info x.

    setRight(p,x) creates the right child node of p.

    The child node contains the info x.

  • 8/7/2019 presentation of data structure

    30/112

    Applications ofBinary Trees

    A binary tree is a useful data structure when

    two-way decisions must be made at each point

    in a process. For example, suppose we wanted to find all

    duplicates in a list of numbers:

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  • 8/7/2019 presentation of data structure

    31/112

    Applications ofBinary Trees

    One way of finding duplicates is to compare

    each number with all those that precede it.

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  • 8/7/2019 presentation of data structure

    32/112

    Searching for Duplicates

    If the list of numbers is large and is growing,this procedure involves a large number ofcomparisons.

    A linked list could handle the growth but thecomparisons would still be large.

    The number of comparisons can be drastically

    reduced by using a binary tree. The tree grows dynamically like the linked list.

  • 8/7/2019 presentation of data structure

    33/112

    Searching for Duplicates

    The binary tree is built in a special way.

    The first number in the list is placed in a node

    that is designated as the root of a binary tree.

    Initially, both left and right subtrees of the root

    are empty.

    We take the next number and compare it with

    the number placed in the root.

    If it is the same then we have a duplicate.

  • 8/7/2019 presentation of data structure

    34/112

    Searching for Duplicates

    Otherwise, we create a new tree node and put

    the new number in it.

    The new node is made the left child of the rootnode if the second number is less than the one

    in the root.

    The new node is made the right child if the

    number is greater than the one in the root.

  • 8/7/2019 presentation of data structure

    35/112

    Searching for Duplicates

    14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

  • 8/7/2019 presentation of data structure

    36/112

    Searching for Duplicates

    15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    1415

  • 8/7/2019 presentation of data structure

    37/112

    Searching for Duplicates

    15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    15

  • 8/7/2019 presentation of data structure

    38/112

    Searching for Duplicates

    4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    15

    4

  • 8/7/2019 presentation of data structure

    39/112

    Searching for Duplicates

    4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

  • 8/7/2019 presentation of data structure

    40/112

    Searching for Duplicates

    9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

  • 8/7/2019 presentation of data structure

    41/112

    Searching for Duplicates

    9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

  • 8/7/2019 presentation of data structure

    42/112

    Searching for Duplicates

    7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

  • 8/7/2019 presentation of data structure

    43/112

    Searching for Duplicates

    7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

  • 8/7/2019 presentation of data structure

    44/112

  • 8/7/2019 presentation of data structure

    45/112

    Searching for Duplicates

    18, 3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    18

  • 8/7/2019 presentation of data structure

    46/112

  • 8/7/2019 presentation of data structure

    47/112

    Searching for Duplicates

    3, 5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

  • 8/7/2019 presentation of data structure

    48/112

    Searching for Duplicates

    5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

  • 8/7/2019 presentation of data structure

    49/112

    Searching for Duplicates

    5, 16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

  • 8/7/2019 presentation of data structure

    50/112

    Searching for Duplicates

    16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

  • 8/7/2019 presentation of data structure

    51/112

    Searching for Duplicates

    16, 4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

  • 8/7/2019 presentation of data structure

    52/112

    Searching for Duplicates

    4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

    4

  • 8/7/2019 presentation of data structure

    53/112

    Searching for Duplicates

    20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

    20

  • 8/7/2019 presentation of data structure

    54/112

    Searching for Duplicates

    20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

  • 8/7/2019 presentation of data structure

    55/112

    Searching for Duplicates

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

  • 8/7/2019 presentation of data structure

    56/112

    Searching for Duplicates

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

  • 8/7/2019 presentation of data structure

    57/112

    Searching for Duplicates

    9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

  • 8/7/2019 presentation of data structure

    58/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17pq

  • 8/7/2019 presentation of data structure

    59/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17p

    q

  • 8/7/2019 presentation of data structure

    60/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/7/2019 presentation of data structure

    61/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/7/2019 presentation of data structure

    62/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    pq

  • 8/7/2019 presentation of data structure

    63/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/7/2019 presentation of data structure

    64/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    pq

  • 8/7/2019 presentation of data structure

    65/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

  • 8/7/2019 presentation of data structure

    66/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    p->setRight( node );

    node

  • 8/7/2019 presentation of data structure

    67/112

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    p->setRight( node );

    node

  • 8/7/2019 presentation of data structure

    68/112

    Binary Search Tree

    A binary tree with the property that items in theleft subtree are smaller than the root and itemsare larger or equal in the right subtree is called

    a binary search tree (BST). The tree we built for searching for duplicate

    numbers was a binary search tree.

    BST and its variations play an important role insearching algorithms.

  • 8/7/2019 presentation of data structure

    69/112

  • 8/7/2019 presentation of data structure

    70/112

    Traversing a Binary Tree

    Ways to print a 3 node tree:

    14

    154

    (4, 14, 15), (4,15,14)

    (14,4,15), (14,15,4)

    (15,4,14), (15,14,4)

  • 8/7/2019 presentation of data structure

    71/112

    Traversing a Binary Tree

    In case of the general binary tree:

    node

    (L,N,R), (L,R,N)

    (N,L,R), (N,R,L)

    (R,L,N), (R,N,L)

    leftsubtree

    rightsubtree

    L

    N

    R

  • 8/7/2019 presentation of data structure

    72/112

    Traversing a Binary Tree

    Three common ways

    node

    Preorder: (N,L,R)

    Inorder: (L,N,R)

    Postorder: (L,R,N)

    leftsubtree

    rightsubtreeL

    N

    R

  • 8/7/2019 presentation of data structure

    73/112

    Traversing a Binary Tree

    Preorder: 14 4 3 9 7 5 15 18 16 17 20

    14

    154

    9

    7

    183

    5

    16 20

    17

  • 8/7/2019 presentation of data structure

    74/112

    Traversing a Binary Tree

    Inorder: 3 4 5 7 9 14 15 16 17 18 20

    14

    154

    9

    7

    183

    5

    16 20

    17

  • 8/7/2019 presentation of data structure

    75/112

    Traversing a Binary Tree

    Postorder: 3 5 7 9 4 17 16 20 18 15 14

    14

    154

    9

    7

    183

    5

    16 20

    17

  • 8/7/2019 presentation of data structure

    76/112

    Level-order Traversal

    There is yet another way of traversing a binary

    tree that is not related to recursive traversal

    procedures discussed previously.

    In level-order traversal, we visit the nodes at

    each level before proceeding to the next level.

    At each level, we visit the nodes in a left-to-right

    order.

  • 8/7/2019 presentation of data structure

    77/112

  • 8/7/2019 presentation of data structure

    78/112

    Level-order Traversal

    There is yet another way of traversing a binary

    tree that is not related to recursive traversal

    procedures discussed previously.

    In level-order traversal, we visit the nodes at

    each level before proceeding to the next level.

    At each level, we visit the nodes in a left-to-right

    order.

  • 8/7/2019 presentation of data structure

    79/112

  • 8/7/2019 presentation of data structure

    80/112

    Level-order Traversal

    How do we do level-order traversal?

    Surprisingly, if we use a queue instead of a

    stack, we can visit the nodes in level-order.

    Here is the code for level-order traversal:

  • 8/7/2019 presentation of data structure

    81/112

    Level-order Traversal

    Queue: 14

    Output:

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    82/112

    Level-order Traversal

    Queue: 4 15

    Output: 14

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    83/112

    Level-order Traversal

    Queue: 15 3 9

    Output: 14 4

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    84/112

    Level-order Traversal

    Queue: 3 9 18

    Output: 14 4 15

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    85/112

    Level-order Traversal

    Queue: 9 18

    Output: 14 4 15 3

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    86/112

  • 8/7/2019 presentation of data structure

    87/112

    Level-order Traversal

    Queue: 7 16 20

    Output: 14 4 15 3 9 18

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    88/112

    Level-order Traversal

    Queue: 16 20 5

    Output: 14 4 15 3 9 18 7

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    89/112

    Level-order Traversal

    Queue: 20 5 17

    Output: 14 4 15 3 9 18 7 16

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    90/112

    Level-order Traversal

    Queue: 5 17

    Output: 14 4 15 3 9 18 7 16 20

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    91/112

  • 8/7/2019 presentation of data structure

    92/112

    Level-order Traversal

    Queue:

    Output: 14 4 15 3 9 18 7 16 20 5 17

    14

    4

    9

    7

    3

    5

    15

    18

    16 20

    17

  • 8/7/2019 presentation of data structure

    93/112

    Deleting a node in BST

    As is common with many data structures, the

    hardest operation is deletion.

    Once we have found the node to be deleted, we

    need to consider several possibilities.

    If the node is a leaf, it can be deleted

    immediately.

  • 8/7/2019 presentation of data structure

    94/112

    Deleting a node in BST

    If the node has one child, the node can be deleted afterits parent adjusts a pointer to bypass the node andconnect to inorder successor.

    6

    2

    4

    3

    1

    8

  • 8/7/2019 presentation of data structure

    95/112

    Deleting a node in BST

    The inorder traversal order has to be maintained

    after the delete.

    6

    2

    4

    3

    1

    8

    6

    2

    4

    3

    1

    8

  • 8/7/2019 presentation of data structure

    96/112

    Deleting a node in BST

    The inorder traversal order has to be maintained

    after the delete.

    6

    2

    4

    3

    1

    8

    6

    2

    4

    3

    1

    8

    6

    2

    31

    8

  • 8/7/2019 presentation of data structure

    97/112

    Deleting a node in BST

    The complicated case is when the node to be

    deleted has both left and right subtrees.

    The strategy is to replace the data of this node

    with the smallest data of the right subtree and

    recursively delete that node.

  • 8/7/2019 presentation of data structure

    98/112

    Deleting a node in BST

    Delete(2): locate inorder successor

    6

    2

    5

    3

    1

    8

    4Inorder

    successor

    S

  • 8/7/2019 presentation of data structure

    99/112

    Deleting a node in BST

    Delete(2): locate inorder successor

    6

    2

    5

    3

    1

    8

    4Inorder

    successor

    Inorder successor will be the left-most node in

    the right subtree of 2.

    The inorder successor will not have a left child

    because if it did, that child would be the left-

    most node.

    D l i d i BST

  • 8/7/2019 presentation of data structure

    100/112

    Deleting a node in BST

    Delete(2): copy data from inorder successor

    6

    2

    5

    3

    1

    8

    4

    6

    3

    5

    3

    1

    8

    4

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    101/112

    Deleting a node in BST

    Delete(2): remove the inorder successor

    6

    2

    5

    3

    1

    8

    4

    6

    3

    5

    3

    1

    8

    4

    6

    3

    5

    3

    1

    8

    4

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    102/112

    Deleting a node in BST

    Delete(2)

    6

    3

    5

    4

    1

    8

    6

    3

    5

    3

    1

    8

    4

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    103/112

    Deleting a node in BST

    As is common with many data structures, the

    hardest operation is deletion.

    Once we have found the node to be deleted, we

    need to consider several possibilities.

    If the node is a leaf, it can be deleted

    immediately.

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    104/112

    Deleting a node in BST

    If the node has one child, the node can be deleted afterits parent adjusts a pointer to bypass the node andconnect to inorder successor.

    6

    2

    4

    3

    1

    8

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    105/112

    Deleting a node in BST

    The inorder traversal order has to be maintained

    after the delete.

    6

    2

    4

    3

    1

    8

    6

    2

    4

    3

    1

    8

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    106/112

    Deleting a node in BST

    The inorder traversal order has to be maintained

    after the delete.

    6

    2

    4

    3

    1

    8

    6

    2

    4

    3

    1

    8

    6

    2

    31

    8

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    107/112

    Deleting a node in BST

    The complicated case is when the node to be

    deleted has both left and right subtrees.

    The strategy is to replace the data of this node

    with the smallest data of the right subtree and

    recursively delete that node.

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    108/112

    Deleting a node in BST

    Delete(2): locate inorder successor

    6

    2

    5

    3

    1

    8

    4Inorder

    successor

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    109/112

    Deleting a node in BST

    Delete(2): locate inorder successor

    6

    2

    5

    3

    1

    8

    4Inorder

    successor

    Inorder successor will be the left-most node in

    the right subtree of 2.

    The inorder successor will not have a left child

    because if it did, that child would be the left-

    most node.

    D l ti d i BST

  • 8/7/2019 presentation of data structure

    110/112

    Deleting a node in BST

    Delete(2): copy data from inorder successor

    6

    2

    5

    3

    1

    8

    4

    6

    3

    5

    3

    1

    8

    4

    Deleting a node in BST

  • 8/7/2019 presentation of data structure

    111/112

    Deleting a node in BST

    Delete(2): remove the inorder successor

    6

    2

    5

    3

    1

    8

    4

    6

    3

    5

    3

    1

    8

    4

    6

    3

    5

    3

    1

    8

    4

  • 8/7/2019 presentation of data structure

    112/112