C-Programming-Class 14

Embed Size (px)

Citation preview

  • 8/14/2019 C-Programming-Class 14

    1/41

    1

    Session 14

    Trees

  • 8/14/2019 C-Programming-Class 14

    2/41

    2

    Session Objectives

    To understand the concepts of trees.

    To know different parts of trees.

  • 8/14/2019 C-Programming-Class 14

    3/41

    3

    Session Topics

    Introduction to trees

    Parts of trees

    Binary trees

    In-order Transversal

    Post-order Transversal Threaded Binary Tree

  • 8/14/2019 C-Programming-Class 14

    4/41

    4

    Definition of a Tree

    A directed tree is an acyclic graph, which has only one

    node with indegree 0 (root) and all other nodes haveindegree 1.

    Indegree the number of edges incidenton a node .Outdegree the number of edges leavinga node.

  • 8/14/2019 C-Programming-Class 14

    5/41

    5

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    6/41

    6

    nodes

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    7/41

    7

    parent

    node

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    8/41

    8

    child

    nodesparent

    node

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    9/41

    9

    child

    nodesparent

    node

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    10/41

    10

    root node

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    11/41

    11

    leaf

    nodes

    A node that has an outdegree of 0, is

    called a Leaf node or Terminal node.

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    12/41

    12

    sub-tree

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    13/41

    13

    sub-tree

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    14/41

    14

    sub-tree

    Parts of a Tree

  • 8/14/2019 C-Programming-Class 14

    15/41

  • 8/14/2019 C-Programming-Class 14

    16/41

    16

    10

    20 15

    2530

    Level It is number of edges in the path from the root node

    Ex: Level of 25 is 2, Level of 10 is 0

    Height It is one more than the maximum level in the tree.

    Ex: Height of tree shown below is 3.

    Level & Height of a Tree

  • 8/14/2019 C-Programming-Class 14

    17/41

    17

    Binary Tree

    A tree in which outdegree of each node is less than or equal to

    two is called a Binary tree.

    If the outdegree of every node is either 0 or 2, it is called Strictly

    Binary Tree.

  • 8/14/2019 C-Programming-Class 14

    18/41

    18

    Storage representation of a node

    In a linked allocation technique, a node in a tree has threefields.

    infowhich contains the actual information

    llinkwhich contains address of the left subtree

    rlinkwhich contains address of the right subtree

    A node can be represented as a structure as follows

    struct node{

    int info;struct node *llink;struct node *rlink;

    };

  • 8/14/2019 C-Programming-Class 14

    19/41

    19

    Various operations performed on Trees

    Insertion Insert a given item into a tree

    Traversal Visiting the nodes of the tree

    one by one Search Search for the specified item

    CopyingTo obtain exact copy of the

    given tree

    Deletion To delete a node from the tree

  • 8/14/2019 C-Programming-Class 14

    20/41

    20

    Traversals

    Traversal is the most common operation that can be

    performed on trees.

    There are three types of traversals:-

    Preorder

    Inorder

    Postorder

    Preorder Traversal

    Recursive definition of Preorder traversal

    Process the root Node[N]

    Traverse the Left subtree in preorder[L]

    Traverse the Right subtree in preorder[R]

  • 8/14/2019 C-Programming-Class 14

    21/41

    21

    Example: Preorder

    FIECHGDBA

    A

    B C

    D E

    HG

    F

    I

  • 8/14/2019 C-Programming-Class 14

    22/41

    22

    Function for preorder traversal

    void preorder(NODE root)

    {

    if(root != NULL)

    {

    printf(%d,root->info);

    preorder(root->llink);

    preorder(root->rlink);}

    }

  • 8/14/2019 C-Programming-Class 14

    23/41

    23

    Inorder Traversal

    Traverse the Left subtree in inorder[L]

    Process the root Node[N]

    Traverse the Right subtree in inorder[R]

  • 8/14/2019 C-Programming-Class 14

    24/41

    24

    Example: Inorder

    FCIEABHDG

    A

    B C

    D E

    HG

    F

    I

  • 8/14/2019 C-Programming-Class 14

    25/41

    25

    Function for inorder traversal

    void inorder(NODE root)

    {

    if(root != NULL){

    inorder(root->llink);

    printf(%d,root->info);

    inorder(root->rlink);}

    }

  • 8/14/2019 C-Programming-Class 14

    26/41

    26

    Postorder Traversal

    Traverse the Left subtree in postorder[L]

    Traverse the Right subtree in postorder[R]

    Process the root Node[N]

  • 8/14/2019 C-Programming-Class 14

    27/41

    27

    ACFEIBDHG

    Example:Postorder

    A

    B C

    D E

    HG

    F

    I

  • 8/14/2019 C-Programming-Class 14

    28/41

  • 8/14/2019 C-Programming-Class 14

    29/41

    29

    Insertion

    Algorithm to insert an item into a tree

    Create new node for the item.

    Find a parent node according to thedirectionsgiven.

    Find the appropriate position to insert the node

    Attach the new node as a leaf.

  • 8/14/2019 C-Programming-Class 14

    30/41

    30

    43

    31 64

    20 40 56

    28 33 47 59

    89

    57Example:

    Insert

  • 8/14/2019 C-Programming-Class 14

    31/41

    31

    Insert

    Example: 5743

    31 64

    20 40 56

    28 33 47 59

    89

    57

    43

    64

    56

    59

    57

  • 8/14/2019 C-Programming-Class 14

    32/41

    32

    Binary Search Tree

    A binary search tree is a binary tree in which foreach node x in the tree, elements in the left subtreeare less than info(x) and elements in the rightsubtree are greater than or equal to info(x).

    Various operations performed

    Insertion

    Searching

    Deleting

  • 8/14/2019 C-Programming-Class 14

    33/41

    33

    45

    35 64

    25 40 56

    28 32 47 59

    89

    Binary Search Tree

  • 8/14/2019 C-Programming-Class 14

    34/41

    34

    Insertion

    25

    140

    100

    50

    90

    80

    200

    300150

    180140

  • 8/14/2019 C-Programming-Class 14

    35/41

    35

    Function to insert an itemNODE insert(int item, NODE root){

    NODE temp,cur,prev;temp = get_node(); /*Obtain a new node*/

    temp->info = item;

    temp->llink = NULL;

    temp->rlink = NULL;

    if(root == NULL)

    return temp;

    prev = NULL; /*find the appropriate position*/cur = root;

    while(cur != NULL)

    {

    prev = cur;/*Obtain parent and left or right child*/

    cur = (item < cur->info) ? Cur->llink :cur->rlink;

    }

    if(item < prev->info) /*new node is less than parent */prev->llink = temp; /*Insert towards left */

    else

    prev->rlink = temp; /*Insert towards right */

    return root; /*Return the root*/

    }

  • 8/14/2019 C-Programming-Class 14

    36/41

  • 8/14/2019 C-Programming-Class 14

    37/41

    37

    Deletion

    When we delete a node, we need to consider how we

    take care of the children of the deleted node.

    This has to be done such that the property of the

    search tree is maintained.

  • 8/14/2019 C-Programming-Class 14

    38/41

    38

    Deletion

    Two cases:

    (1) the node is a leaf

    Delete it immediately

    (2) the node has one child

    Adjust a pointer from the parent to bypass that node

  • 8/14/2019 C-Programming-Class 14

    39/41

    39

    Threaded Binary Tree

    Inorder traversal of a threaded binary tree By using threads we can perform an inorder traversal

    without making use of a stack (simplifying the task)

    Now, we can follow the thread of any node,ptr, to thenext node of inorder traversal

    Ifptr->right_thread= TRUE, the inorder successor ofptrisptr->right_childby definition of the threads

    Otherwise we obtain the inorder successor of ptr byfollowing a path of left-child links from the right-child ofptruntil we reach a node with left_thread= TRUE

  • 8/14/2019 C-Programming-Class 14

    40/41

    40

    Summary

    A directed tree is an acyclic graph, which has onlyone node with indegree 0 (root) and all othernodes have indegree 1.

    A node that has an outdegree of 0, is called a Leafnode or Terminal node.

    A tree in which outdegree of each node is less thanor equal to two is called a Binary tree.

    If the outdegree of every node is either 0 or 2, it iscalled Strictly Binary Tree.

  • 8/14/2019 C-Programming-Class 14

    41/41

    41

    Thank You!