Computer Notes - Data Structures - 12

Embed Size (px)

Citation preview

  • 8/3/2019 Computer Notes - Data Structures - 12

    1/47

    Class No.12

    Data Structures

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    2/47

    Operations on Binary Tree

    There are a number of operations that canbe defined for a binary tree.

    If p is pointing to a node in an existing treethen

    left(p) returns pointer to the left subtree

    right(p) returns pointer to right subtree

    parent(p) returns the father of p

    brother(p) returns brother of p.

    info(p) returns content of the node.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    3/47

    Operations on Binary Tree

    In order to construct a binary tree, thefollowing can be useful:

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

    setRight(p,x) creates the right child nodeof p. The child node contains the info x.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    4/47

    Applications of Binary 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

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    5/47

    Applications of Binary Trees

    One way of finding duplicates is tocompare each number with all those thatprecede 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

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    6/47

    Searching for Duplicates

    If the list of numbers is large and isgrowing, this procedure involves a largenumber of comparisons.

    A linked list could handle the growth butthe comparisons 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.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    7/47

    Searching for Duplicates

    The binary tree is built in a special way.

    The first number in the list is placed in anode that is designated as the root of abinary tree.

    Initially, both left and right subtrees of theroot are empty.

    We take the next number and compare itwith the number placed in the root.

    If it is the same then we have a duplicate.http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    8/47

    Searching for Duplicates

    Otherwise, we create a new tree node andput the new number in it.

    The new node is made the left child of theroot node if the second number is lessthan the one in the root.

    The new node is made the right child if thenumber is greater than the one in the root.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    9/47

    Searching for Duplicates

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

    14

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    10/47

    Searching for Duplicates

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

    1415

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    11/47

    Searching for Duplicates

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

    14

    15

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    12/47

    Searching for Duplicates

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

    14

    15

    4

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    13/47

    Searching for Duplicates

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

    14

    154

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    14/47

    Searching for Duplicates

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

    14

    154

    9

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    15/47

    Searching for Duplicates

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

    14

    154

    9

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    16/47

    Searching for Duplicates

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

    14

    154

    9

    7

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    17/47

    Searching for Duplicates

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

    14

    154

    9

    7

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    18/47

    Searching for Duplicates

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

    14

    154

    9

    7

    18

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    19/47

    Searching for Duplicates

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

    14

    154

    9

    7

    18

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    20/47

    Searching for Duplicates

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

    14

    154

    9

    7

    18

    3

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    21/47

    Searching for Duplicates

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

    14

    154

    9

    7

    183

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    22/47

    Searching for Duplicates

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

    14

    154

    9

    7

    183

    5

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    23/47

    Searching for Duplicates

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

    14

    154

    9

    7

    183

    5

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    24/47

    Searching for Duplicates

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

    14

    154

    9

    7

    183

    5

    16

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    25/47

    Searching for Duplicates

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

    14

    154

    9

    7

    183

    5

    16

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    26/47

    Searching for Duplicates

    4, 20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

    4

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    27/47

    Searching for Duplicates

    20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16

    20

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    28/47

    Searching for Duplicates

    20, 17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    29/47

    Searching for Duplicates

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    30/47

    Searching for Duplicates

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    31/47

    Searching for Duplicates

    9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    32/47

    C++ Implementation

    #include

    template

    class TreeNode {

    public:

    // constructors

    TreeNode(){

    this->object = NULL;

    this->left = this->right = NULL;

    };

    TreeNode( Object* object ){

    this->object = object;

    this->left = this->right = NULL;

    };

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    33/47

    C++ Implementation

    Object* getInfo(){

    return this->object;

    };

    void setInfo(Object* object)

    {this->object = object;

    };

    TreeNode* getLeft()

    {

    return left;

    };

    void setLeft(TreeNode *left)

    {

    this->left = left;

    };

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    34/47

    C++ Implementation

    TreeNode *getRight()

    {

    return right;

    };

    void setRight(TreeNode *right)

    {this->right = right;

    };

    int isLeaf( )

    {if( this->left == NULL && this->right == NULL )

    return 1;

    return 0;

    };

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    35/47

    C++ Implementation

    private:

    Object* object;

    TreeNode* left;

    TreeNode* right;

    }; // end class TreeNode

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    36/47

    C++ Implementation

    #include

    #include

    #include "TreeNode.cpp"

    int main(int argc, char *argv[])

    {int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17,

    9, 14,5, -1};

    TreeNode* root = new TreeNode();

    root->setInfo( &x[0] );

    for(int i=1; x[i] > 0; i++ ){

    insert(root, &x[i] );

    }

    }

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    37/47

    C++ Implementation

    void insert(TreeNode* root, int* info)

    {

    TreeNode* node = new TreeNode(info);

    TreeNode *p, *q;

    p = q = root;

    while( *info != *(p->getInfo()) && q != NULL )

    {

    p = q;

    if( *info < *(p->getInfo()) )

    q = p->getLeft();else

    q = p->getRight();

    }

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    38/47

    C++ Implementation

    if( *info == *(p->getInfo()) ){

    cout setRight( node );

    } // end of insert

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    39/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17pq

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    40/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17p

    q

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    41/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    pq

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    42/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    43/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    pq

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    44/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    45/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    pq

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    46/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    q

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 12

    47/47

    Trace of insert

    17, 9, 14, 5

    14

    154

    9

    7

    183

    5

    16 20

    17

    p

    p->setRight( node );

    node

    http://ecomputernotes.com/