lectures 6.2 (1).pptx

Upload: kerhun123

Post on 14-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Lectures 6.2 (1).pptx

    1/39

    Lecture 6

    C S 1 1 3 2 D A T A S T R U C T U R E S A N D S O F T W A R ED E S I G N

  • 7/27/2019 Lectures 6.2 (1).pptx

    2/39

    2

    Introduction to Graphs

    A graph is a finite set of nodes with edges between nodes

    Formally, a graph G is a structure (V,E) consisting of

    a finite set V called the set of nodes, and a set E that is a subset of VxV. That is, E is a set of pairs of the form (x,y) where x

    and y are nodes in V

  • 7/27/2019 Lectures 6.2 (1).pptx

    3/39

    3

    Examples of GraphsV={1,2,3,4,5}E={(1,2), (2,3), (1,4), (4,1), (3,3), (5,4)}

    12

    5

    3

    4

    When (x,y) is an edge,

    we say that x is adjacent

    to y.

    1 is adjacent to 2.

    2 is not adjacent to 1.

    4 is not adjacent to 3.

  • 7/27/2019 Lectures 6.2 (1).pptx

    4/39

    4

    Trees

    A tree is a connected acyclic undirected graph. The following are threetrees:

    12

    5

    311

    12

    10

    98

    7

    64

  • 7/27/2019 Lectures 6.2 (1).pptx

    5/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    6/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    7/39

    Make binary tree by sorted

    ArrayArray A[15] = { 1, 4,8,11,14,19,23,29,40,89,91,105,110,124,200}

    8

    29

    11

    4 19

    14 23

    105

    124

    91 2001

    89

    40 110

    A[15] :

    The First is A[0]

    And last is A[14]

    So (0+14)/2 is

    A[7] which is equal

    To 29

    And Now we have two array one of the is bigger than 29 that is in the right side

    And the other one has 7 int that all of the are smaller than 29

  • 7/27/2019 Lectures 6.2 (1).pptx

    8/39

    Illustration of Insert

    6

    15

    8

    2

    3 7

    11

    10

    14

    12

    20

    27

    22 30

    Before inserting 25

    6

    158

    2

    3 7

    11

    10

    14

    12

    20

    27

    22 30

    25

    After inserting 25

  • 7/27/2019 Lectures 6.2 (1).pptx

    9/39

    10 min for Array To Binary

    Search Tree function

  • 7/27/2019 Lectures 6.2 (1).pptx

    10/39

    De-allocating binary treesThree things to do

    Free current node

    Recursively free left subtree

    Recursively free right subtree

  • 7/27/2019 Lectures 6.2 (1).pptx

    11/39

    C Binary Tree node

    struct btnode

    {int data;struct btnode *left_p;struct btnode *right_p;

    } ;

  • 7/27/2019 Lectures 6.2 (1).pptx

    12/39

    Creating a treestruct btnode *root;

    struct btnode *mynode =

    (struct btnode *) malloc (sizeof(struct btnode));

    root = mynode;

    // use root to access the tree, like head for a linked list

  • 7/27/2019 Lectures 6.2 (1).pptx

    13/39

    Creating nodesstruct node * NewNode(int data)

    {struct node *mynode = (struct node *) malloc (sizeof(struct node));

    mynode->data = data;mynode->left_p = NULL;mynode->right_p = NULL;

    return(node);}

  • 7/27/2019 Lectures 6.2 (1).pptx

    14/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    15/39

    10 min for t_insert and Search

    function

  • 7/27/2019 Lectures 6.2 (1).pptx

    16/39

    Insert function

  • 7/27/2019 Lectures 6.2 (1).pptx

    17/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    18/39

    Binary Tree TraversalTraversal is the process of visiting everynode once

    Visiting a node entails doing someprocessing at that node, but whendescribing a traversal strategy, we need

    not concern ourselves with what thatprocessing is

  • 7/27/2019 Lectures 6.2 (1).pptx

    19/39

    Binary Tree Traversal

    TechniquesThree recursive techniques for binary tree traversal

    In each technique,

    the left subtree is traversed recursively,

    the right subtree is traversed recursively,

    and the root is visited

    What distinguishes the techniques from one anotheris the order of those 3 tasks

  • 7/27/2019 Lectures 6.2 (1).pptx

    20/39

    Preoder, Inorder, Postorder

    In Preorder, the rootis visited before (pre)

    the subtrees traversals

    In Inorder, the root isvisited in-between left

    and right subtree traversal

    In Preorder, the root

    is visited after (pre)

    the subtrees traversals

    Preorder Traversal:

    1. Visit the root

    2. Traverse left subtree

    3. Traverse right subtree

    Inorder Traversal:

    1. Traverse left subtree

    2. Visit the root

    3. Traverse right subtree

    Postorder Traversal:

    1. Traverse left subtree

    2. Traverse right subtree

    3. Visit the root

  • 7/27/2019 Lectures 6.2 (1).pptx

    21/39

    Illustrations for Traversals

    Assume: visiting a node

    is printing its label

    Preorder:

    Inorder:

    Postorder:

    1

    3

    11

    98

    4 6

    5

    7

    12

    10

  • 7/27/2019 Lectures 6.2 (1).pptx

    22/39

    Illustrations for TraversalsAssume: visiting a node

    is printing its label

    Preorder:

    1 3 5 4 6 7 8 9 10 11 12

    Inorder:

    4 5 6 3 1 8 7 9 11 10 12

    Postorder:

    4 6 5 3 8 11 12 10 9 7 1

    1

    3

    11

    98

    4 6

    5

    7

    12

    10

  • 7/27/2019 Lectures 6.2 (1).pptx

    23/39

    Illustrations for Traversals (Contd.)

    Assume: visiting a node

    is printing its data

    Preorder: 15 8 2 6 3 7

    11 10 12 14 20 27 22 30

    Inorder: 2 3 6 7 8 10 11

    12 14 15 20 22 27 30

    Postorder: 3 7 6 2 10 1412 11 8 22 30 27 20 15

    6

    15

    8

    2

    3 7

    11

    10

    14

    12

    20

    27

    22 30

  • 7/27/2019 Lectures 6.2 (1).pptx

    24/39

    Code for the Traversal Techniques

    The code for visit

    is up to you toprovide, depending

    on the application

    A typical example

    for visit() is to

    print out the data

    part of its input

    node

    void inOrder(Tree *tree){

    if(tree->isEmpty( )) return;inOrder(tree->getLeftSubtree( ));

    visit(tree->getRoot( ));

    inOrder(tree->getRightSubtree( ));

    }

    void preOrder(Tree *tree){

    if(tree->isEmpty( )) return;

    visit(tree->getRoot( ));preOrder(tree->getLeftSubtree());

    preOrder(tree->getRightSubtree());

    }

    void postOrder(Tree *tree){

    if(tree->isEmpty( )) return;

    postOrder(tree->getLeftSubtree( ));

    postOrder(tree->getRightSubtree( ));

    visit(tree->getRoot( ));

    }

  • 7/27/2019 Lectures 6.2 (1).pptx

    25/39

    Application of TraversalSorting a BST

    Observe the output of the inorder traversal ofthe BST example two slides earlier

    It is sortedThis is no coincidence

    As a general rule, if you output the keys (data)

    of the nodes of a BST using inorder traversal,the data comes out sorted in increasing order

  • 7/27/2019 Lectures 6.2 (1).pptx

    26/39

    Depth/Height of Full Trees and

    Almost Complete TreesThe height (or depth ) h of such trees is O(log n)

    Proof: In the case of full trees,

    The number of nodes n is: n=1+2+22

    +23

    ++2h

    =2h+1

    -1 Therefore, 2h+1 = n+1, and thus, h=log(n+1)-1

    Hence, h=O(log n)

    For almost complete trees, the proof is left as an

    exercise.

  • 7/27/2019 Lectures 6.2 (1).pptx

    27/39

    Canonical Labeling ofAlmost Complete Binary Trees

    Same labeling inherited from full binary trees

    Same relationship holding between the labels ofchildren and parents:

    Relationships between labels

    of children and parent:

    2i 2i+1

    i

  • 7/27/2019 Lectures 6.2 (1).pptx

    28/39

    Trees and arrays

    Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compact-binary-heaps/

    Map current node, left child, and right child to array positions

    t[i] , t[2i+1] , t[2i+2]

    Order in the array

    by level in the tree

  • 7/27/2019 Lectures 6.2 (1).pptx

    29/39

    Trees and arrays

    Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compact-binary-heaps/

    Map current node, left child, and right child to array positions

    Order in the array

    by level in the tree

  • 7/27/2019 Lectures 6.2 (1).pptx

    30/39

    Application of Almost Complete

    Binary Trees: HeapsA heap (or min-heap to be precise) is an almost complete binary treewhere

    Every node holds a data value (or key)

    The key of every node is the keys of the children

    Note:

    A max-heap has the same definition except that the

    Key of every node is >= the keys of the children

  • 7/27/2019 Lectures 6.2 (1).pptx

    31/39

    Search function

  • 7/27/2019 Lectures 6.2 (1).pptx

    32/39

    Delete a Complete TreeTo delete a tree we must traverse all the nodes ofthe tree and delete them one by one. So whichtraversal we should use Inorder or Preorder or

    Postorder. Answer is simple Postorder, becausebefore deleting the parent node we should deleteits children nodes first

  • 7/27/2019 Lectures 6.2 (1).pptx

    33/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    34/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    35/39

    10 min for t_insert function

  • 7/27/2019 Lectures 6.2 (1).pptx

    36/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    37/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    38/39

  • 7/27/2019 Lectures 6.2 (1).pptx

    39/39