Lecture 7 data structures

download Lecture 7 data structures

of 34

Transcript of Lecture 7 data structures

  • 8/13/2019 Lecture 7 data structures

    1/34

    Trees: basic definitions and terminology

    Contrary to arrays, stacks, queues and sequences all of which are one-

    dimensional data structures, trees are two-dimensional data structures with

    hierarchical relationship between data items.

    Definition 1 A tree is a non-empty collection of vertices (nodes) and edges

    that satisfy certain requirements.

    Definition 2 A path in a tree is a list of distinct vertices in which successive

    vertices are connected by edges in the tree.

    One node in the tree is designated as the root. Each tree has exactly one

    path between the root and each of the other nodes. If there is more than one

    path between the root and some node, or no path at all, we have a graph.

    Definition 3 A set of trees is called a forest.

    Definition 4 (Recursive definition) A tree is either a single node or a root

    node connected to a forest.

  • 8/13/2019 Lecture 7 data structures

    2/34

    Example of a tree:

    root

    siblings

    subtree

    internal nodes

    external nodes, or leaves

  • 8/13/2019 Lecture 7 data structures

    3/34

    More definitions

    Definition 5 An ord ered tree is a tree in which the order of children is

    specified.

    Definition 6 A level (depth) of a node in the number of nodes on the path

    from that node to the root.

    Definition 7 The height (max imum dis tance) of a tree is the maximum

    level among all of the nodes in the tree.

    Definition 8 The path length of a tree is the sum of the levels of all the

    nodes in the tree.

    Definition 9 A tree where each node has a specific number of children

    appearing in a specific order is call a mu lt iway tree. The simplest type of

    a multiway tree is the bin ary tree. Each node in a binary tree has exactlytwo children one of which is designated as a left child, and the other is

    designated as a right child.

    Definition 10 (Recursive definition) A binary tree is either an external node,

    or an internal node and two binary trees.

  • 8/13/2019 Lecture 7 data structures

    4/34

    Example of a binary tree

    root

    left child right child

    one or both childrenmight be external nodes

    special external nodes with

    no name and no data associated

    with them

  • 8/13/2019 Lecture 7 data structures

    5/34

    More binary trees examples

    1. Binary tree for representing arithmetic expressions. The underlying hierarchical

    relationship is that of an arithmetic operator and its two operands.

    Arithmetic expression in an infix form: (A - B) + C * (E / F)

    +

    - *

    A B C /

    E F

    Note that a post-order traversal of this tree (i.e. visiting the left subtree first, right

    subtree next, and finally the root) returns the postfix form of the arithmetic

    expression, while the pre-order traversal (root is visited first, then the left subtree,

    then the right subtree) returns the prefix form of the arithmetic expression.

  • 8/13/2019 Lecture 7 data structures

    6/34

    2. Binary tree with a heap property. The underlying hierarchical relationship

    suggests that the datum in each node is greater than or equal to the data in

    its left and right subtrees.

    87

    84 63

    68 79 12

    32 67 6 10

    8 9

  • 8/13/2019 Lecture 7 data structures

    7/34

    3. Binary tree with an ordering property. The underlying hierarchical

    relationship suggests that the datum in each node is greater than the data in

    its left subtree, and less than or equal to the data in its right subtrees.

    87

    84 103

    68 86 90 109

    32 74 88 97

    70 80

  • 8/13/2019 Lecture 7 data structures

    8/34

    4. Decision trees. The underlying hierarchical relationship depends on the nature of

    the domain represented by the binary tree. For example, consider a domain that

    consists of the following statements (from J.Ignizio Intro to ES):

    If the planes engine is propeller, then the plane is C130.

    If the planes engine is jet and the wing position is low, then the plane is B747.

    If the planes engine is jet and the wing position is high and no bulges are seen, then

    the plane is C5A

    If the planes engine is jet and the wing position is high and bulges are aft of wing, then

    plane is C141 .

    The following decision tree can be generated from these rules:

    Engine type

    Jet Propeller

    Wing Posit ion C130

    Low High

    B747 Bulges

    None Aft Wing

    C5A C141

  • 8/13/2019 Lecture 7 data structures

    9/34

    Properties of binary trees

    1. The number of external nodes is 1 more than the number of internal nodes.

    It is easy to see this if we start removing external nodes with their internalparent, one pair at a time (assume that a method removeAboveExternal(n)

    does this). At the end of this process, only the root with its two external

    children will remain.

    2. The number of external nodes is at least h + 1, where his the height of the

    tree, and at most 2h. The later holds for a fu l l bin ary tree, which is a treewhere internal nodes completely fill every level.

    3. The number of internal nodes is at least hand at most 2h

    - 1.

    4. The total number of nodes in a binary tree is at least 2*h + 1 and at most

    2

    h+1

    - 1.5. The height, h, of a binary tree with nnodes is at least log n+1 and at most

    n.

    6. A binary tree with nnodes has exactly n - 1 edges.

  • 8/13/2019 Lecture 7 data structures

    10/34

    Full binary trees and complete binary trees

    Here is an example of a full binary tree:

    1

    2 3

    4 5 6 7

    8 9 10 11 12 13 14 15

    A complete binary tree is a full binary tree where the internal nodes on the

    bottom level all appear to the left of the external nodes on that level. Here is

    an example of a complete binary tree:

    12 3

    4 5 6

  • 8/13/2019 Lecture 7 data structures

    11/34

    Properties of binary trees (cont.)

    The following property holds for a complete binary tree.

    Let ibe a number assigned to a node in a complete binary tree. Then:

    1. If i = 1, then this node is the root of the tree. If i > 1, then the parent of this

    node is assigned the number (i / 2).

    2. If 2*i > n, then the corresponding node has no left child. Otherwise, the left

    child of that node is assigned the number 2*i.3. If 2*i + 1 > n, then the corresponding node has no right child. Otherwise, the

    right child of that node is assigned the number 2*i + 1.

    This property suggests a trivial array-based representation of a complete binary

    tree, where iis the index of the node in the array. We will see that a slightmodification in this representation allows us to represent any binary tree in a

    linear fashion.

  • 8/13/2019 Lecture 7 data structures

    12/34

    The generic Binary Tree ADT

    We cannot provide a complete specification of the Binary Tree ADT, as we did

    with other ADTs so far, because the hierarchical relationship in the binary tree

    cannot be uniquely defined. We define here only a set of basic operations on

    binary trees, and more specific binary tree ADTs will be introduced as the

    need arrives.

    Operations (methods) on binary trees:

    empty () Returns t rueif the binary tree is empty

    getRoot () Returns the root node of the tree

    leftChild (node) Returns the left child of node.

    rightChild (node) Returns the right child of node.

    expandExternal(node) Makes nodeinternal by creating its left and right children

    removeAboveExternal(node) Removes an external node together with its parent

    insert (node) Inserts nodein the appropriate position in the tree

    delete (node) Deletes node

    preOrder() Visit the root, then the left subtree, then the right subtree

    postOrder () Visit the left subtree, then the right subtree, then the root

    inOrder() Visit the left subtree, then the root, then the right subtree

    levelOrder () Starting from the root, visit tree nodes level by level

  • 8/13/2019 Lecture 7 data structures

    13/34

    Linear (or sequence-based) representation of a

    binary tree

    Linear representation of a binary tree utilizes one-dimensional array of size

    2h+1 - 1. Consider the following tree:

    + level 0 (d = 0)

    - * level 1 (d = 1)

    A B C / level 2 (d = 2)

    E F level 3 (d = 3)

    To represent this tree, we need an array of size 23+1

    - 1 = 15

    The tree is represented as follows:

    1. The root is stored in BinaryTree[1].

    2. For node BinaryTree[n], the left child is stored in BinaryTree[2*n], and the

    right child is stored in BinaryTree[2*n+1]

    i: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    BinaryTree[i]: + - * A B C / E F

  • 8/13/2019 Lecture 7 data structures

    14/34

    Linear representation of a binary tree (cont.)

    Advantages of linear representation:

    1. Simplicity.

    2. Given the location of the child (say, k), the location of the parent is easy to

    determine (k / 2).

    Disadvantages of linear representation:

    1. Additions and deletions of nodes are inefficient, because of the datamovements in the array.

    2. Space is wasted if the binary tree is not complete. That is, the linear

    representation is useful if the number of missing nodes is small.

    Note that linear representation of a binary tree can be implemented by means

    of a linked list instead of an array. For example, we can use the Positional

    Sequence ADT to implement a binary tree in a linear fashion. This way the

    above mentioned disadvantages of the linear representation will be resolved.

  • 8/13/2019 Lecture 7 data structures

    15/34

    Linked representation of a binary tree

    Linked representation uses explicit links to connect the nodes. Example:

    1

    2 5

    3 4 6 7

    8 9

    Nodes in this tree can be viewed as positions in a sequence (numbered 1

    through 9).

    +

    BA

    - *

    C /

    E F

  • 8/13/2019 Lecture 7 data structures

    16/34

    Binary tree nodes (linked representation)

    class BTNode {

    char data;BTNode leftChild;

    BTNode rightChild;

    BTNode parent;

    int pos;

    public BTNode () {

    }

    public BTNode (char newData) {

    data = newData;

    }

    public BTNode (char newData, BTNode newLeftChild, BTNode newRightChild) {

    data = newData;

    leftChild = newLeftChild;

    rightChild = newRightChild;

    }

    ... methods setData, setLeftChild, setRightChild, getData, getLeftChild, getRightChild,

    displayBTNode follow next ...

    }

  • 8/13/2019 Lecture 7 data structures

    17/34

    Binary tree (linked representation)

    We can use a positional sequence ADT to implement a binary tree. Ourexample tree, in this case, we be represented as follows:

    position 1 2 3 4 5 6 7 8 9

    data + - A B * C / E F

    leftChild 2 3 null null 6 null 8 null null

    rightChild 5 4 null null 7 null 9 null null

    parent null 1 2 2 1 5 5 7 7

    class BTLRPS implements PSDLL {

    private BTNode header;

    private BTNode trailer;

    private int size;

    int position;

    ... class methods follow ... }

  • 8/13/2019 Lecture 7 data structures

    18/34

    Traversals of a binary tree

    Preorder traversal

    public void preOrder (BTNode localRoot) {

    if (localRoot != null) {

    localRoot.displayBTNode();

    preOrder(localRoot.leftChild);

    preOrder(localRoot.rightChild); } }

    Example Consider a tree with an ordering property, where nodes are inserted

    in the following order b i n a r y t r e e, i.e.

    b

    a i

    e ne r

    y

    t

    r

    The preorder traversal is: b a i e e n r y t r

  • 8/13/2019 Lecture 7 data structures

    19/34

    Traversals of a binary tree (cont.)

    Post-order traversal

    public void postOrder (BTNode localRoot) {

    if (localRoot != null) {

    postOrder(localRoot.leftChild);

    postOrder(localRoot.rightChild);

    localRoot.displayBTNode(); } }

    The nodes in the example tree are traversed in post-order as follows:

    a e e r t y r n i b

    In-order traversal

    public void inOrder (BTNode localRoot) {

    if (localRoot != null) {inOrder(localRoot.leftChild);

    localRoot.displayBTNode();

    inOrder(localRoot.rightChild); } }

    The nodes in the example tree are traversed in in-order as follows:

    a b e e i n r r t y

  • 8/13/2019 Lecture 7 data structures

    20/34

    Traversals of a binary tree (cont.)

    Level-order traversal

    public void levelOrder (BTNode localRoot) {

    BTNode[] queue = new BTNode[20];

    int front = 0;

    int rear = -1;

    while (localRoot != null) {

    localRoot.displayBTNode();if (localRoot.leftChild != null) {

    rear++;

    queue[rear] = localRoot.leftChild; }

    if (localRoot.rightChild != null) {

    rear++;

    queue[rear] = localRoot.rightChild; }localRoot = queue[front];

    front++; } }

    The nodes in the example tree are traversed in level-order as follows:

    b a i e n e r y t r

  • 8/13/2019 Lecture 7 data structures

    21/34

    Example applications of binary tree traversals

    3. Application of an in-order traversal: binary search trees

    A binary search tree is a tree with an ordering relationship between data in the

    nodes (i.e. all nodes with smaller data are in the left subtree and all nodes with

    greater or equal data are in the right subtree). See slide 7 for an example

    In-order traversal of a binary search tree produces an ordered list:

    32 68 70 74 80 84 86 87 88 90 97 103 109

    Binary search trees allow for a very efficient search (in log N time). The idea

    of the binary tree search is the following: to find a node with a given datum

    (the target), compare the target to the root; if it is smaller, go to the left subtree;

    if it is larger, go to the right subtree; if it is equal, stop.

  • 8/13/2019 Lecture 7 data structures

    22/34

  • 8/13/2019 Lecture 7 data structures

    23/34

    Binary Tree with an ordering property: the insert method

    class BTLRADT {

    BTNode root;

    public BTLRADT () { }

    public BTNode getRoot () {

    return root; }

    public void insert (char newData) {

    BTNode newNode = new BTNode ();newNode.data = newData;

    if (root == null)

    root = newNode;

    else {

    BTNode temp = root;

    BTNode parent;

    while (true) {

    parent = temp;

    if (newData < temp.data) { //go left

    temp = temp.leftChild;

    if (temp == null) {

    parent.leftChild = newNode;

    return; }

    }

    else { // go right

    temp = temp.rightChild;if (temp == null) {

    parent.rightChild = newNode;

    return;

    }

    }

    }

    }

    }

  • 8/13/2019 Lecture 7 data structures

    24/34

    Deletion in binary search tree

    Consider the tree:

    3 7

    2 15 Deleting 3 2 15

    1 11 1 11

    7 13 13

    The following cases of deletions are possible:

    1. Delete a note with no children, for example 1. This only requires the appropriate link in

    the parent node to be made null.

    2. Delete a node which has only one child, for example 15. In this case, we must set the

    corresponding child link of the parents parent to point to the only child of the node being

    deleted.

    3. Delete a node with two children, for example 3. The delete method is based on thefollowing consideration: in-order traversal of the resulting tree (after delete operation)

    must yield an ordered list. To ensure this, the following steps are carried out:

    Step 1: Replace 3with the node with the next largest datum, i.e. 7.

    Step 2: Make the left link of 11point to the right child of 7(which is null here).

    Step 3: Copy the links from the node containing 3to the node containing 7, and make

    the parent node of 3point to 7.

  • 8/13/2019 Lecture 7 data structures

    25/34

    The Tree ADT

    Assuming that a general tree is implemented as a positional container, the

    following is an incomplete set of methods supported by the data structure:

    Container (positional sequence) methods:

    empty(): returns true if the container is empty.

    node(position): returns the node inposition.

    elements(): returns an enumeration of all data stored at nodes of the tree.

    positions(): returns an enumeration of all the positions (nodes) of the tree.

    size(): returns the size of the container.

    replace (position, item): replaces the data atpositionwith item.

    swap (position1, position2): swaps data inposition1andposition2.

    Tree specific methods:

    getRoot(): returns the root node of the tree isRoot(position): returns trueif the node inpositionis the root note.

    isInternal(position): returns trueif the node in that position is an internal node.

    isExternal(position): returns trueif the node in that position is an external node.

    parent(position): returns the parent of the node inposition.

    children(position): returns a set of children of the node inposition.

    siblings(position): returns a set of siblings of the node inposition.

  • 8/13/2019 Lecture 7 data structures

    26/34

    Computing a nodes depth and a trees height

    The depth of a tree node is a number of ancestors of that node, excluding the node itself. That

    is, the depth of the root is 0, while the depth of any other node is the depth of its parent plus

    one. The method, depth, can be implemented recursively as follows:public int depth (int position) {

    if (isRoot(position))

    return 0;

    else

    return (1 + depth(parent(position))); }

    The height of the tree is equal to the maximum depth of external nodes of the tree. The

    method heightcan be implemented as follows:

    public int height () {

    int h = 0;

    Enumeration nodes = positions();

    while (nodes.hasMoreElements()) {

    int nextNode = nodes.nextElement ();

    if (isExternal(nextNode))

    h = Math.max(h, depth(nextNode));

    }

    return h; }

  • 8/13/2019 Lecture 7 data structures

    27/34

    Binary tree representation of a general tree

    Consider the following genealogical tree

    Jim

    Bill Katy Mike Tom

    Dave Mary Leo Bety Rog

    Lary Paul Peny Don

    We can represent it in the following binary tree format:

    1 Jim

    2 Bill 8 Katy 10 Mike 14 Tom

    3 Dave 4 Mary 9 Leo 11 Bety 13 Rog

    5 Lary 6 Paul 7 Peny 12 Don

  • 8/13/2019 Lecture 7 data structures

    28/34

    Binary tree representation of a general tree (contd.)

    In the resulting binary tree, the left-child pointer (we can call it here the children

    pointer) points to the first child of the ordered list of children, while the right-childpointer (we can call it here the siblingpointer) points to the next sibling of a node.

    We can represent the resulting binary tree as a positional sequence:

    position 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    data Jim Bill Dave Mery Lary Paul Peny Katy Leo Mike Bety Don Rog TomfirstChild 2 3 null 5 null null null 9 null 11 12 null null null

    sibling null 8 4 null 6 7 null 10 null 14 13 null null null

    parent null 1 2 3 4 5 6 2 8 8 10 11 11 10

    class TNode {

    private String data;

    private TNode children, sibling;

    int position;

    ... class methods follow ...

    }

  • 8/13/2019 Lecture 7 data structures

    29/34

    Tree traversals

    Consider our example tree

    Jim

    Bill Katy Mike Tom

    Dave Mary Leo Bety Rog

    Lary Paul Peny Don

    Preorder traversal is:

    Jim Bill Dave Mary Lary Paul Peny Katy Leo Mike Bety

    Don Rog Tom

    Postorder traversal is:

    Dave Lary Paul Peny Mary Bill Leo Katy Don Bety Rog

    Mike Tom Jim

  • 8/13/2019 Lecture 7 data structures

    30/34

    Preorder traversal of a general tree

    Preorder traversal works as follows: 1.) select a node and visit it and its children;

    2.) go to the next node at the same level and do the same until all of the tree

    nodes are processed.

    Algorithm preOrder (TNode)

    visit TNode

    for each child TNodeChild of TNode do

    recursively perform preOrder(TNodeChild)

    Or, in JAVA:

    public void preOrder (TNode localRoot) {

    localRoot.displayTNode();

    Enumeration localRootChildren = localRoot.children(localRoot.getPosition());

    while (localRootChildren.hasMoreElements()) {TNode nextNode = localRootChildren.nextElement();

    preOrder (nextNode); } }

    Note: If a general tree is represented as a binary tree, a preorder traversal of the

    general tree and the corresponding binary tree, produces the same result.

  • 8/13/2019 Lecture 7 data structures

    31/34

    Postorder traversal of a general tree

    In postorder traversal, the tree is processed from left to right, ensuring that no

    node is processed until all nodes below it are processed. That is,

    Algorithm postOrder (TNode)

    for each child TNodeChild of TNode do

    recursively perform postOrder(TNodeChild)

    visit TNode

    Or, in JAVA:public void postOrder (TNode localRoot) {

    Enumeration localRootChildren = localRoot.children(localRoot.getPosition());

    while (localRootChildren.hasMoreElements()) {

    TNode nextNode = localRootChildren.nextElement();

    postOrder (nextNode); }localRoot.displayTNode(); }

    Note: If a general tree is represented as a binary tree, a postorder traversal of the

    general tree and the corresponding binary tree, do not generate the same result.

    However, the inorder traversal of the corresponding binary tree generates the

    same result as the postorder traversal of the general tree.

  • 8/13/2019 Lecture 7 data structures

    32/34

    Ternary tree representation of a general tree

    If node siblings in a general tree form ordered lists, then we can represent the

    tree as a ternarytree. In a ternary tree, each node has the following attributes:

    left sibling, which is either null or points to a node whose data precedes

    that of a given node at the same level;

    data stored in the node;

    children, a pointer to the ordered list of children of that node, or nullif the

    node has no children;

    right sibling, which is either nullor points to a node whose data equals or

    follow that of a given node at the same level.

  • 8/13/2019 Lecture 7 data structures

    33/34

    Ternary tree representation of a general tree (contd.)

    Consider the example tree:

    Jim

    Bill Katy Mike Tom

    Dave Mary Leo Bety Rog

    Lary Paul Peny Don

    Represented as a ternary tree, it looks like as follows:

    Jim

    Bill Katy Mike Tom

    Dave Mary Leo Bety Rog

    Lary Paul Peny Don

  • 8/13/2019 Lecture 7 data structures

    34/34

    Preorder traversal of a ternary tree

    The idea is the following: for each node do 1.) process the node, and 2.) access

    the binary tree representing its children, and process this binary tree inorder.

    Algorithm preorderTernary (ternaryNode)

    if (ternaryNode is internal node) {

    preorder (ternaryNode.leftSibling)

    process ternaryNode

    inorder (ternaryNode.children)

    preorder (ternaryNode.rightSibling)

    }