discrete maths trees

Upload: ocira-oyaro

Post on 08-Aug-2018

236 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/22/2019 Discrete Maths Trees

    1/12

    18/1/2013

    MATH 224 Discrete Mathematics

    A rooted tree is a tree that has a distinguished node called the root. Just as with all

    trees a rooted tree is acyclic (no cycles) and connected. Rooted trees have many

    applications in computer science, for example the binary search tree.

    Rooted Trees

    The root is typically drawn at the top.

    Children of the root.

    Grandchildren of the root and leaf nodes.

  • 8/22/2019 Discrete Maths Trees

    2/12

    28/1/2013

    MATH 224 Discrete Mathematics

    A Binary tree is a rooted tree where no node has more than two children. As shown in

    the previous slide all nodes, except for leaf nodes, have children and some have

    grandchildren. All nodes except the root have a parent and some have a grandparent

    and ancestors. A node has at most one parent and at most one grandparent.

    Rooted Binary Trees

    The root of a binary tree at level 4 also height 4 and depth 0.

    Nodes with only 1 child at level 2 and depth 2.

    Nodes a level 1, and depth 3.

    Nodes at level 3 and depth 1.

    Nodes with no childrenare called leaf nodes.

    Nodes a level 0, and depth 4.

  • 8/22/2019 Discrete Maths Trees

    3/12

    38/1/2013

    MATH 224 Discrete Mathematics

    A Balanced tree is a rooted tree where the leaf nodes have depths that vary by no

    more than one. In other words, the depth of a leaf node is either equal to the height of

    the tree or one less than the height. All of the trees below are balanced.

    Balanced Trees

    What are the heights of each of these trees?

  • 8/22/2019 Discrete Maths Trees

    4/12

    48/1/2013

    MATH 224 Discrete Mathematics

    A Binary Search Tree

    15

    3010

    535

    32 458

  • 8/22/2019 Discrete Maths Trees

    5/12

    58/1/2013

    MATH 224 Discrete Mathematics

    Some Tree Applications

    Binary Search Trees to store items for easy retrieval, insertion and deletion. For

    balanced trees, each of these steps takes lg(N) time for an Nnode tree.

    Variations of the binary search tree include, the AVL tree, Red-Blacktree and the

    B-tree. These are all designed to keep the tree nearly balanced. The first two are

    binary trees while the B-tree has more than two children for each internal node.Game trees are used extensively in AI. The text has a simple example of a tic-tac-

    toe tree on Page 654.

    Huffman trees are used to compress data. They are most commonly used to

    compress faxes before transmission.

    Spanning trees are subgraphs that have applications to computer and telephonenetworks. Minimum spanning trees are of special interest.

    Steiner trees are a generalization of the spanning tree with in multicast

    communication.

  • 8/22/2019 Discrete Maths Trees

    6/12

    68/1/2013

    MATH 224 Discrete Mathematics

    Binary Tree Traversal Functions

    void inorder(node T)

    if (T null) {

    inorder(T>left)

    cout data right)

    } fiend inorder

    a

    e

    c

    d

    b

    f g

    h

    i joutput will be something like

    d, b, e, a, f, c, g, i, h, j

  • 8/22/2019 Discrete Maths Trees

    7/12

    78/1/2013

    MATH 224 Discrete Mathematics

    Binary Tree Traversal Functions

    void inorder(node T)

    if (T null) {

    inorder(T>left)

    cout data right)

    } fiend inorder

    a

    e

    c

    d

    b

    f g

    h

    i j

    T is NULL

    T is d

    prints d

    T is b

    T is a

    main calls

    inorder

  • 8/22/2019 Discrete Maths Trees

    8/12

    88/1/2013

    MATH 224 Discrete Mathematics

    Binary Tree Traversal Functions

    void pre_order(node T)

    if (T null) {

    cout data left)

    pre_order(T >right)

    }end pre_order

    a

    e

    c

    d

    b

    f g

    h

    i joutput will be something like

    a, b, d, e, c, f, g, h, i, j

  • 8/22/2019 Discrete Maths Trees

    9/12

    98/1/2013

    MATH 224 Discrete Mathematics

    Binary Tree Traversal Functions

    void post_order(node T)

    if (T null) {

    post_order(T>left)

    post_order(T >right)

    cout data

  • 8/22/2019 Discrete Maths Trees

    10/12

    108/1/2013

    MATH 224 Discrete Mathematics

    Counting the nodes in a Binary Tree

    int count(node T)

    int num = 0

    if (T null)

    num = 1 + count(T>left) + count(T >right)

    fi

    return numend count

    a

    e

    c

    d

    b

    f g

    h

    i j

  • 8/22/2019 Discrete Maths Trees

    11/12

    118/1/2013

    MATH 224 Discrete Mathematics

    The Height of a Binary Tree

    int height(node T)int ht = 1

    if (T null)

    ht = 1 + max(height(T>left), height(T>right))

    fi

    return ht

    end height

    a

    e

    c

    d

    b

    f g

    h

    i j

    Called initially at a

    Then at b

    Then a d

    Then at NULL

    Returns 1 to dfrom left and right

    Returns 0 to bfrom dand from e

    Returns 1 to afrom left.

    Returns 3 to afrom right

    Returns 4 from ato original caller

  • 8/22/2019 Discrete Maths Trees

    12/12

    128/1/2013

    MATH 224 Discrete Mathematics

    Inserting into a Binary Search Tree

    // Note that T is passed by referenceinsert(node & T, int X)

    if (T = = null)

    T = node(X)

    else if X < T>value

    insert(T >left, X)

    elseinsert(T >right, X)

    fi

    end height

    30

    24

    45

    17

    22

    32 70

    84

    75 98

    Where would 60 be inserted?

    If the original value for T is the

    node containing 30, where is the

    first recursive call, the second etc?

    Where would 10 be inserted?