unit 1 - trees

Upload: vaibhav-makkar

Post on 04-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Unit 1 - Trees

    1/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 1

    TREES

  • 7/30/2019 Unit 1 - Trees

    2/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 2

    Introduction to Trees Flexible, versatile, and powerful data

    structures.

    Used to represent hierarchical relationship

    among them. Example: Geneology

    The relationship between the grandfather and

    his descendants and in turn their descendantsand so on

  • 7/30/2019 Unit 1 - Trees

    3/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 3

    Tree definition Non-linear data structure in which items

    are arranged in a sorted sequence.

    Represents hierarchical relationshipexisting amongst several data items.

  • 7/30/2019 Unit 1 - Trees

    4/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 4

    Graph theoretic definitionIt is finite set of one or more data items

    (nodes) such that

    1. There is special data item called rootof the

    tree2. Remaining data items are partitioned into

    number of mutually exclusive (i.e, disjoint)

    subsets. Each of which is itself istree called to

    be subtrees

  • 7/30/2019 Unit 1 - Trees

    5/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 5

    A Tree

    A

    B C D

    E F G H I

    J K L M

    Level 0

    Level 1

    Level 2

    Level 3

    Root

    Trees in data structures grows from top to bottom.

  • 7/30/2019 Unit 1 - Trees

    6/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 6

    Tree TerminologyTerm associated with trees1. Root

    2. Node

    3. Degree of a node

    4. Degree of a tree

    5. Terminal node(s)6. Non-terminal node(s)

    7. Siblings

    8. Level

    9. Edge10. Path

    11. Depth

    12. Forest

  • 7/30/2019 Unit 1 - Trees

    7/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 7

    Root

    1. Specially designated data item.

    2. First in the hierarchical arrangements ofdata items.

    Node

    1. Each data item in a tree is called tree.2. It specifies the data information and links

    (branches) to other data items.

  • 7/30/2019 Unit 1 - Trees

    8/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 8

    Degree of a node

    It is the number of sub trees of a node in agiven tree.

    a. The degree of nodeA is 3

    b. The degree of node C is 1

    c. The degree of node D is 2

    d. The degree of node H is 3

    e. The degree of node I is 0

    Degree of treeIt is maximum degree of the nodes in agiven tree.

    In the tree shown , the node A has degree 3 and another nodeH is also having its degree 3. in all the value is the maximum.So, the degree of the tree is 3

  • 7/30/2019 Unit 1 - Trees

    9/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 9

    Terminal Node(s)

    A node with degree zero.

    Also called leaf node. Ex. Nodes E,G,I,J,K,L and M are Terminal nodes

    Non-Terminal Node(s)

    Any node (except the root node) whose degree is not

    zero.

    Intermediate nodes in traversing the given tree from

    its root to the terminal nodes.

    Ex nodes B, C, D, F & H are Non terminal nodes

  • 7/30/2019 Unit 1 - Trees

    10/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 10

    Siblings

    The children nodes of a given node.

    Also called brothers.

    E and F are siblings of parent node B.

    K, L and M are siblings of parent node H.

    Level Entire tree structure is leveled as such that the root is

    always at level 0.

    Its intermediate at level 1. and their intermediate atlevel 2 and so on upto the terminal nodes.

    Tree shown has level 4.

  • 7/30/2019 Unit 1 - Trees

    11/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 11

    Edge

    Connecting line of two nodes. line drawn from one node to another node.

    Path

    Sequence of consecutive edges from source

    node to the destination node.

    Eg: Path betweenA and J is given by

    (A,B), (B,F), (F,J)

  • 7/30/2019 Unit 1 - Trees

    12/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 12

    Depth/height

    Maximum level of any node in a given tree. Number of levels one can descend the tree from its

    root to the terminal nodes

    In tree shown Root node has the maximum level. Forest

    Set of disjoint trees.

    Ifroot is removed from tree it becomes forest. In tree shown there is forest with three trees

  • 7/30/2019 Unit 1 - Trees

    13/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 13

    Binary Trees Finite set of data items which is either

    empty or consists of a single item calledroot and two disjoint binary trees called left

    sub tree and right sub tree

    Maximum degree of any node is at most

    two.

  • 7/30/2019 Unit 1 - Trees

    14/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 14

    A binary tree

    A

    B C

    D E F G

    J

    Root

    I K

  • 7/30/2019 Unit 1 - Trees

    15/59

  • 7/30/2019 Unit 1 - Trees

    16/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 16

    Complete Binary Tree There is exactly one node at level 0,two

    nodes at level 1, four nodes at level 2 andso on.

    If there are m nodes at level L then a

    binary tree contains 2m nodes at level

    L+1.

  • 7/30/2019 Unit 1 - Trees

    17/59

    Trees

    MPSTME, SHIRPUR

    Preeti S. Patil 17

    Properties of binary trees

    1. The maximum number of nodes at level l of a binary

    tree is 2l, where l>=0

    2. The maximum number of nodes in a binary tree of

    depth d is 2d-1 where d>=1.

    3. The total number of edges in a complete binary tree

    with n terminal nodes is 2(n-1).

    4. A binary tree with n nodes has exactly N+1 null

    branches.

  • 7/30/2019 Unit 1 - Trees

    18/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 18

    Binary Tree Representation Arrays representation

    Linked list representation

    1. Basic component of a tree is a node2. Node consist of three fields.

    Data

    Left Child

    Right Child

  • 7/30/2019 Unit 1 - Trees

    19/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 19

    Data field hold the value to be given. The left child is a link field which contains

    the address of its left node.

    The right child is a link field which contains

    the address of its right node.

    The Structure of a node

    Lchild Data Rchild

  • 7/30/2019 Unit 1 - Trees

    20/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 20

    Logical Representationstruct node

    {

    char data;

    struct node lchild;struct node rchild;

    };typedef struct node BTNODE;

  • 7/30/2019 Unit 1 - Trees

    21/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 21

    Consider the following Binary tree.

    A

    B C

    D E

    Root

    F

  • 7/30/2019 Unit 1 - Trees

    22/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 22

    Linked List representation

    A

    B C

    D E F

    Root

  • 7/30/2019 Unit 1 - Trees

    23/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 23

    Array Representation The nodes stored in an array are

    accessible sequentially. Maximum nu ber of nodes is specified by

    MaxSize.

    Root node is always at index 0.

    Successive memory locations the left child

    and right child are stored.

  • 7/30/2019 Unit 1 - Trees

    24/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 24

    The array representation of the above tree

    A

    B C

    0

    1 2

    A

    B

    C

    BT

    BT [0]

    BT [1]

    BT [2]

  • 7/30/2019 Unit 1 - Trees

    25/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 25

    A

    B C

    D E F G

    0

    2

    5 6

    1

    3 4

    A

    B

    C

    DE

    F

    G

    BT

    BT [0]

    BT [1]

    BT [2]

    BT [3]BT [4]

    BT [5]

    BT [6]

  • 7/30/2019 Unit 1 - Trees

    26/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 26

    Father(n):

    The Father of node having index n is at floor((n-1)/2) If n=0, then it is the root node and has no father.

    Eg: father of node numbered 3(i.e, D)

    =floor ((3-1) / 2)=floor (2/2)

    =1(i.e B)

    lchild(n): The left child of node numbered n is at (2n+1).

    Eg: lchild(A)=lchild(0)

    =2*0+1

    =1(i.e B).

  • 7/30/2019 Unit 1 - Trees

    27/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 27

    rchild(n):

    The right child of node numbered n is atindex(2n+2)

    Eg: rchild(A)=rchild(0)

    =2*0+2=2(i.e C).

    Siblings: If left child at index n is given then right sibling

    is at (n+1).

    If right child at index n is given then left siblingis at (n-1).

    Right node of node 4 is at index 5.

  • 7/30/2019 Unit 1 - Trees

    28/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 28

    Skewed binary tree

    Since left sub tree is present, this type of tree is called left skewedbinary tree. we can even have right skewed binary tree. This result

    in wastage of memory in case of array representation.

    A

    B

    C

    0

    1

    3

    A

    B

    -

    C

    -

    -

    -

    BT

    0

    1

    2

    3

    4

    5

    6

  • 7/30/2019 Unit 1 - Trees

    29/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 29

    Disadvantages of array

    representation

    Not suitable for normal binary trees.

    Ideal for complete binary trees

    More memory is unnecessarily wasted.

    Insertion and deletion at the middle of thetree is cumbersome.

    O ti bi t

  • 7/30/2019 Unit 1 - Trees

    30/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 30

    Operations on binary trees

    Operation Description1. Create It creates an empty binary tree.

    2. Make BT It creates a new Binary Tree having single node with data

    field set to some value.

    3. Empty BT It returns true if the binary tree is empty. Otherwise it returns

    false.

    4. lchild It returns a pointer to the left child of the node. if the node has

    no left child it returns the null pointer

    5. rchild It returns a pointer to the right child of the node. if the node

    has no right child it returns the null pointer

    6. father It returns the pointer to father of the node.otherwise returns

    null pointer

    7. Brother It returns the pointer to brother of the node.otherwise returns

    null pointer

    8. Data It returns the content of the node.

  • 7/30/2019 Unit 1 - Trees

    31/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 31

    Operations Other than primitive operation other

    operations are1. Tree traversal

    2. Insertion of nodes

    3. Deletion of nodes

    4. Searching for the node

    5. Copying the binary tree.

  • 7/30/2019 Unit 1 - Trees

    32/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 32

    Traversal of a Binary Tree It is a way in which each node in the tree

    is visited exactly once in systematicmanner.

    Popular ways of Binary tree traversal1. Preorder Traversal

    2. Postorder Traversal

    3. Inorder Traversal

  • 7/30/2019 Unit 1 - Trees

    33/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 33

    Preorder TraversalThe preorder of a non-empty binary tree is

    defined as

    1. Visit the root node.2. Traverse the left sub tree in preorder.

    3. Traverse the right sub tree in preorder.

  • 7/30/2019 Unit 1 - Trees

    34/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 34

    Preorder traversal of the above binary tree

    A B D E I C F G JRoot

    A

    B C

    D E F G

    J

    Root

    I

    left Right

  • 7/30/2019 Unit 1 - Trees

    35/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 35

    Algorithmpreorder(BTNODE *tree)

    {if(tree != NULL)

    {

    printf(%c,tree->data); /* step 1*/preorder(tree->lchild); /*step 2*/

    preorder(tree->rchild); /*step 3*/

    }return;

    }

  • 7/30/2019 Unit 1 - Trees

    36/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 36

    Inorder TraversalThe Inorder of a non-empty binary tree is

    defined as

    1. Traverse the left sub tree in inorder.2. Visit the root node.

    3. Traverse the right sub tree in inorder.

  • 7/30/2019 Unit 1 - Trees

    37/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 37

    A

    B C

    D E F G

    J

    Root

    I

    left Right

    Inorder traversal of the above binary treeD B I E A F C G J

  • 7/30/2019 Unit 1 - Trees

    38/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 38

    Algorithminorder(BTNODE *tree)

    {if(tree != NULL)

    {

    inorder(tree->lchild); /*step 1*/printf(%c,tree->data); /* step 2*/

    inorder(tree->rchild); /*step 3*/

    }

    return;

    }

  • 7/30/2019 Unit 1 - Trees

    39/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 39

    Postorder TraversalThe postorder of a non-empty binary tree is

    defined as

    1. Traverse the left sub tree in postorder.2. Traverse the right sub tree in postorder.

    3. Visit the root node.

  • 7/30/2019 Unit 1 - Trees

    40/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 40

    Post order traversal of the above binary tree

    D I E B F J G C A

    A

    B C

    D E F G

    J

    Root

    I

    left Right

  • 7/30/2019 Unit 1 - Trees

    41/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 41

    Algorithmpostorder(BTNODE *tree)

    {if(tree != NULL)

    {

    postorder(tree->lchild); /*step 1*/

    postorder(tree->rchild); /*step 2*/

    printf(%c,tree->data); /* step 3*/

    }

    return;

    }

  • 7/30/2019 Unit 1 - Trees

    42/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 42

    Binary Search Tree (BST)

    Tree in which each node has left and rightchild.

    If traversed in inorder, we get a file is

    ascending order.

  • 7/30/2019 Unit 1 - Trees

    43/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 43

    Traverse the tree in inorder, we get

    9 12 21 25 42 43 48

    25

    12 43

    9 21 42 48

  • 7/30/2019 Unit 1 - Trees

    44/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 44

    SEARCHING Start with the root.

    Eg; search 21 in the given tree Compare 2112. so take right branch.

    On third comparison, since 21=21, the search

    is announced successful.

  • 7/30/2019 Unit 1 - Trees

    45/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 45

    Advantages Advantage over an array is that all the

    insertion and deletions and searching areperformed efficiently.

  • 7/30/2019 Unit 1 - Trees

    46/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 46

    Height Balanced Tree We attempt to place each terminal node at

    equal distance from the root node.

    A

    B C

    D E F G

  • 7/30/2019 Unit 1 - Trees

    47/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 47

    Insertion in a BST To insert an element 23 in an tree shown below

    Search for element 23 in that tree.

    Search causes to reach at the node 21 with no where to go.

    Since 23>21, we simply insert node 23 as right child to node 21.

    25

    12 43

    9 21 42 48

    25

    12 43

    9 21 42 48

    23

    23>21

    Algorithm for SearchingS h(i t k l i t &f d BTNODE *(& t))

  • 7/30/2019 Unit 1 - Trees

    48/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 48

    Search(int key_val, int &found, BTNODE *(&parent))

    {

    BTNODE *p;

    found=0;

    parent=NULL;

    if(tree==NULL)

    {

    return;

    }

    p=tree;while(p!=null)

    {

    if(p->data==key_val)

    {found=1;

    return;

    }if(p->data > key_val)

    {parent=p;

    p=p->lchild;

    }

    else

    if(p->data < key_val){parent=p;

    p=p->rchild;

    }

    }}

    Algorithm for insertion

  • 7/30/2019 Unit 1 - Trees

    49/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 49

    ginsert BST(int num)

    {int found;

    BTNODE *temp,*parent;

    search_node(num,found,parent);

    if(found==1)

    printf(Node already exists);else

    {

    temp=(BTNODE*)malloc(sizeof(BTNODE));

    temp->data=num;

    temp->lchild=temp->rchild=NULL;if(parent==NULL)

    tree=temp;

    else

    { if(parent->data > num)

    parent->lchild=temp;else

    parent-> rchild=temp;

    }}}

  • 7/30/2019 Unit 1 - Trees

    50/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 50

    Deletion in a BST

    Three cases:

    1. Node to be deleted has no children.

    2. Node to be deleted has exactly one

    subtree.

    3. Node to be deleted has two subtrees.

  • 7/30/2019 Unit 1 - Trees

    51/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 51

    In first situation ,

    As there is no children for the node,so that node is deleted with out any

    further adjustments to the tree.

  • 7/30/2019 Unit 1 - Trees

    52/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 52

    In second case,

    The node to be deleted has exactly one child, so this child is

    moved up the tree to occupy its place.25

    12 43

    9 21 42 48

    23Node to

    be

    deleted

    25

    12 43

    9 23 42 48

    After Deletion of the node 21

    Before Deletion of the node 21

  • 7/30/2019 Unit 1 - Trees

    53/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 53

    Third case,The node to be deleted has two sub trees. The in order

    successor of the node should be moved up to occupy its

    place.

    25

    12 43

    9 21 42 48

    23

    Node to

    be

    deleted

    42

    12 43

    9 21 48

    23

    After Deletion of

    the Root node 25

    Before Deletion of

    the Root node 25

    C code for deletiond l BST(i b BTNODE * )

  • 7/30/2019 Unit 1 - Trees

    54/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 54

    delete_BST(int number, BTNODE *tree)

    {int found;

    BTNODE *temp, *parent, *temp_succ;

    if(tree==NULL)

    {

    printf( Tree is empty);

    return;

    }

    parent=temp=Null;

    search_BST(number,parent,temp,tree,found)

    if(found==0)

    { printf( node to be deleted is not found);return;

    }

  • 7/30/2019 Unit 1 - Trees

    55/59

    TreesMPSTME, SHIRPUR

    Preeti S. Patil 55

    /* first case : The node to be deleted has no children*/If((temp->lchild == NULL) && (temp->rchild ==NULL))

    {

    if(parent-.rchild == temp){

    parent->rchild = NULL;

    }

    else{

    parent->lchild= NULL;

    }

    free(temp);return;

    }

  • 7/30/2019 Unit 1 - Trees

    56/59

    TreesMPSTME, SHIRPURPreeti S. Patil 56

    /* second case: if node to be deleted has only left child */If(temp->lchild != NULL)&&(temp->rchild == NULL)

    {

    if(parent->lchild ==temp)

    {parent->lchild =temp->lchild;

    }

    else

    {

    parent->rchild = temp->lchild;

    }

    free(temp);return;

    }

  • 7/30/2019 Unit 1 - Trees

    57/59

    TreesMPSTME, SHIRPURPreeti S. Patil 57

    /* Third case: if node to be deleted has two children */If((temp->lchild != NULL) && (temp->rchild != NULL))

    {

    parent = temp;

    temp_succ = temp->rchild;

    while(temp_succ != NULL)

    {

    parent=temp_succ;temp_succ=temp_succ->lchild;

    }

    temp->data = temp_succ->data;

    temp=temp_succ;

    }

    }

    Efficiency of Binary Search Tree

  • 7/30/2019 Unit 1 - Trees

    58/59

    TreesMPSTME, SHIRPURPreeti S. Patil 58

    Efficiency of Binary Search Tree

    Operations The timing analysis of searching a binary search

    tree, for randomly inserted records is O(log n).

    Worst case behavior of a binary search tree

    would occur when records are inserted in sorted

    order. In this case the search time is O(n), as the

    resulting tree consists all NULL left children.

  • 7/30/2019 Unit 1 - Trees

    59/59

    TreesMPSTME, SHIRPURPreeti S. Patil 59

    End of Trees