CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

download CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

of 46

Transcript of CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    1/46

    CSCE 3110Data Structures &Algorithm Analysis

    Rada Mihalcea

    http://www.cs.unt.edu/~rada/CSCE3110

    Trees. Binary Trees.

    Reading: Chap.4 (4.1-4.2) Weiss

    http://www.cs.unt.edu/~rada/CSCE3110http://www.cs.unt.edu/~rada/CSCE3110
  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    2/46

    The British Constitution

    Crown

    Church of

    England Cabinet

    House of

    Commons

    House of

    Lords

    Supreme

    Court

    Ministers

    County

    CouncilMetropolitan

    police

    County Borough

    Council

    Rural District

    Council

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    3/46

    More Trees Examples

    Unix / Windows file structure

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    4/46

    Definition of Tree

    A tree is a finite set of one or more nodessuch that:

    There is a specially designated node calledthe root.

    The remaining nodes are partitioned into n>=0disjoint sets T1, ..., Tn, where each of these sets is

    a tree.We call T1, ..., Tn the subtrees of the root.

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    5/46

    Level and Depth

    K L

    E F

    B

    G

    C

    M

    H I J

    D

    A

    Level

    1

    2

    3

    4

    node (13)

    degree of a node

    leaf (terminal)

    nonterminal

    parent

    children

    sibling

    degree of a tree (3)

    ancestorlevel of a node

    height of a tree (4)

    3

    2 1 3

    2 0 0 1 0 0

    0 0 0

    1

    2 2 2

    3 3 3 3 3 3

    4 4 4

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    6/46

    Terminology

    The degree of a node is the number of subtreesof the node

    The degree of A is 3; the degree of C is 1.

    The node with degree 0 is a leaf or terminalnode.

    A node that has subtrees is theparentof theroots of the subtrees.

    The roots of these subtrees are the children ofthe node.

    Children of the same parent are siblings.

    The ancestors of a node are all the nodesalong the path from the root to the node.

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    7/46

    Tree Properties

    A

    B C

    D

    G

    E F

    IH

    Property Value

    Number of nodes

    Height

    Root Node

    Leaves

    Interior nodes

    Number of levels

    Ancestors of H

    Descendants of B

    Siblings of E

    Right subtree

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    8/46

    Representation of Trees

    List Representation

    ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )

    The root comes first, followed by a list of sub-trees

    data link 1 link 2 ... link n

    How many link fields are

    needed in such a representation?

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    9/46

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    10/46

    Left Child - Right Sibling

    A

    B C D

    E F G H I J

    K L M

    data

    left child right sibling

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    11/46

    Tree ADT

    Objects: any type of objects can be stored in a treeMethods:

    accessor methods

    root() return the root of the tree

    parent(p) return the parent of a nodechildren(p) returns the children of a node

    query methods

    size() returns the number of nodes in the tree

    isEmpty() - returns true if the tree is emptyelements() returns all elements

    isRoot(p), isInternal(p), isExternal(p)

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    12/46

    Tree Implementation

    typedef struct tnode {int key;

    struct tnode* lchild;

    struct tnode* sibling;

    } *ptnode;

    - Create a tree with three nodes (one root & two children)

    - Insert a new node (in tree with root R, as a new child at level L)

    - Delete a node (in tree with root R, the first child at level L)

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    13/46

    Tree Traversal

    Two main methods:Preorder

    Postorder

    Recursive definition

    PREorder:visit the root

    traverse in preorder the children (subtrees)

    POSTordertraverse in postorder the children (subtrees)

    visit the root

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    14/46

    Preorder

    preordertraversalAlgorithmpreOrder(v)

    visit nodev

    for eachchildwofvdo

    recursively performpreOrder(w)

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    15/46

    Postorder

    postorder traversal

    AlgorithmpostOrder(v)

    for eachchildwofvdo

    recursively performpostOrder(w)

    visit nodev

    du (disk usage) command in Unix

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    16/46

    Preorder Implementation

    public void preorder(ptnode t) {ptnode ptr;

    display(t->key);

    for(ptr = t->lchild; NULL != ptr; ptr = ptr->sibling) {

    preorder(ptr);

    }

    }

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    17/46

    Postorder Implementation

    public void postorder(ptnode t) {ptnode ptr;

    for(ptr = t->lchild; NULL != ptr; ptr = ptr->sibling) {

    postorder(ptr);

    }

    display(t->key);

    }

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    18/46

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    19/46

    Example

    J

    IM

    HL

    A

    B

    C

    D

    E

    F GK

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    20/46

    ADT Binary Tree

    objects: a finite set of nodes either empty orconsisting of a root node, leftBinaryTree,

    and rightBinaryTree.method:

    for all bt, bt1, bt2BinTree, itemelement

    Bintree create()::= creates an empty binary treeBoolean isEmpty(bt)::= if(bt==empty binary

    tree) return TRUEelse returnFALSE

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    21/46

    BinTree makeBT(bt1, item, bt2)::= return a binary tree

    whose left subtree is bt1, whose right subtree is bt2,

    and whose root node contains the data itemBintree leftChild(bt)::= if (IsEmpty(bt)) return error

    else return the left subtree ofbt

    elementdata(bt)::= if (IsEmpty(bt)) return error

    else return the data in the root node ofbtBintree rightChild(bt)::= if (IsEmpty(bt)) return error

    else return the right subtree ofbt

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    22/46

    Samples of Trees

    A

    B

    A

    B

    A

    B C

    GE

    I

    D

    H

    F

    Complete Binary Tree

    Skewed Binary Tree

    E

    C

    D

    1

    2

    3

    4

    5

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    23/46

    Maximum Number of Nodes in B

    The maximum number of nodes on level i of abinary tree is 2i-1, i>=1.

    The maximum nubmer of nodes in a binary treeof depth kis 2k-1, k>=1.

    Prove by induction.2 2 1

    1

    1

    i

    i

    kk

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    24/46

    Full BT vs. Complete BT

    A full binary tree of depth k is a binary tree ofdepth k having 2 -1 nodes, k>=0.

    A binary tree with n nodes and depth k is

    complete iffits nodes correspond to the nodes numberedfrom 1 to n in the full binary tree of depth k.

    k

    A

    B C

    GE

    I

    D

    H

    F

    A

    B C

    GE

    K

    D

    J

    F

    IH ONML

    Full binary tree of depth 4Complete binary tree

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    25/46

    Binary Tree Representations

    If a complete binary tree with n nodes (depth =logn + 1) is represented sequentially, then for

    any node with index i, 1

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    26/46

    SequentialRepresentation

    AB

    --

    C

    --

    --

    --

    D

    --

    .

    E

    [1][2]

    [3]

    [4]

    [5]

    [6]

    [7]

    [8]

    [9]

    .

    [16]

    [1]

    [2]

    [3]

    [4]

    [5]

    [6]

    [7]

    [8]

    [9]

    A

    B

    C

    DE

    F

    G

    HI

    A

    B

    E

    C

    D

    A

    B C

    GE

    I

    D

    H

    F

    (1) waste space(2) insertion/deletion

    problem

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    27/46

    Linked Representation

    typedef struct tnode *ptnode;typedef struct tnode {

    int data;

    ptnode left, right;

    };

    dataleft right

    data

    left right

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    28/46

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    29/46

    Arithmetic Expression Using BT

    +

    *

    A

    *

    /

    E

    D

    C

    B

    inorder traversal

    A / B * C * D + E

    infix expression

    preorder traversal+ * * / A B C D E

    prefix expression

    postorder traversal

    A B / C * D * E +

    postfix expression

    level order traversal

    + * E * D / C A B

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    30/46

    Inorder Traversal(recursive version)

    void inorder(ptnode ptr)

    /* inorder tree traversal */

    {if (ptr) {

    inorder(ptr->left);

    printf(%d, ptr->data);

    indorder(ptr->right);}

    }

    A / B * C * D + E

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    31/46

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    32/46

    L l O d T l

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    33/46

    Level Order Traversal(using queue)

    void levelOrder(ptnode ptr)

    /* level order tree traversal */

    {

    int front = rear = 0;

    ptnode queue[MAX_QUEUE_SIZE];

    if (!ptr) return; /* empty queue */

    enqueue(front, &rear, ptr);

    for (;;) {

    ptr = dequeue(&front, rear);

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    34/46

    if (ptr) {

    printf(%d, ptr->data);if (ptr->left)enqueue(front, &rear,

    ptr->left);if (ptr->right)enqueue(front, &rear,

    ptr->right);

    }else break;

    }}

    + * E * D / C A B

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    35/46

    Euler Tour Traversal

    generic traversal of a binary treethe preorder, inorder, and postorder traversals arespecial cases of the Euler tour traversal

    walk around the

    tree and visit eachnode three times:

    on the left

    from below

    on the right

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    36/46

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    37/46

    Application: Evaluation of

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    38/46

    Application: Evaluation ofExpressions

    +

    *

    A

    *

    /

    E

    D

    C

    B

    inorder traversal

    A / B * C * D + E

    infix expression

    preorder traversal+ * * / A B C D E

    prefix expression

    postorder traversal

    A B / C * D * E +

    postfix expression

    level order traversal

    + * E * D / C A B

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    39/46

    Inorder Traversal(recursive version)

    voidinorder(ptnode ptr)

    /* inorder tree traversal */

    { if (ptr) {

    inorder(ptr->left);

    printf(%d, ptr->data);

    inorder(ptr->right);}

    }

    A / B * C * D + E

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    40/46

    Preorder Traversal(recursive version)

    voidpreorder(ptnode ptr)

    /* preorder tree traversal */

    {

    if (ptr) {

    printf(%d, ptr->data);

    preorder(ptr->left);

    preorder(ptr->right);}

    }

    + * * / A B C D E

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    41/46

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    42/46

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    43/46

    Propositional Calculus Expression

    X3X1

    X2 X1

    X3

    (x1 x

    2) ( x

    1 x

    3) x

    3

    postorder traversal (postfix evaluation)

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    44/46

    Node Structure

    lef t data value right

    typedef emun {not, and, or, true, false } logical;

    typedef struct tnode *ptnode;

    typedef struct node {

    logical data;

    short int value;

    ptnode right, left;

    } ;

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    45/46

    Postorder Eval

    void post_order_eval(ptnode node){/* modifiedpost order traversal to evaluate a propositionalcalculus tree */

    if (node) {post_order_eval(node->left);post_order_eval(node->right);switch(node->data) {

    case not: node->value =!node->right->value;break;

  • 7/27/2019 CSCE 3110 Data Structures & Algorithm Analysis: Binary Trees

    46/46

    Postorder Eval (contd)

    case and: node->value =node->right->value &&node->left->value;break;

    case or: node->value =

    node->right->value | |node->left->value;break;

    case true: node->value = TRUE;

    break;case false: node->value = FALSE;}

    }}