data structure lecture 7 tree[1]

Upload: sweetmaina

Post on 03-Apr-2018

226 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    1/49

    Data Structure

    Lecture-7Prepared by:

    Shipra Shukla

    Assistant ProfessorKaziranga University

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    2/49

    Tree Non Linear data structure

    This structure is mainly used to represent datacontaining a hierarchical relationship betweenelements e.g. record, tree

    A node that has a child is called the child's parent node(or ancestor node, or superior). A node has at mostone parent.

    The topmost node in a tree is called the root node.

    A node that has no children is called a leaf, and that

    node is of course at the bottommost level of the tree.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    3/49

    Cont..

    The height of a node is the length of the

    longest path to a leaf from that node. The

    height of the root is the height of the tree.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    4/49

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    5/49

    Cont..

    An empty tree has a height of zero.

    A single child tree is a tree of height 1.

    According to the definition of trees, a nodecan have any number of children.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    6/49

    Binary tree A binary tree is composed of zero or more nodes

    Each node contains: A value (some sort of data item)

    A reference or pointer to a left child (may be null), and

    A reference or pointer to a right child (may be null)

    A binary tree may be empty(contain no nodes)

    If not empty, a binary tree has a root node

    Every node in the binary tree is reachable from the

    root node by a unique path A node with neither a left child nor a right child is

    called a leaf

    In some binary trees, only the leaves contain a value

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    7/49

    Picture of a binary tree

    a

    b c

    d e

    g h i

    l

    f

    j k

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    8/49

    Size and depth

    The size of a binary tree is thenumber of nodes in it

    This tree has size 12

    The depth of a node is its

    distance from the root

    a is at depth zero

    e is at depth 2

    The depth of a binary tree isthe depth of its deepest node

    This tree has depth 4

    a

    b c

    d e f

    g h i j k

    l

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    9/49

    Cont..

    A complete binary tree is one where all thelevels are full with exception to the last leveland it is filled from left to right.

    A full binary tree is one where if a node has achild, then it has two children.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    10/49

    Balance

    A binary tree is balanced if every level above the lowest is full(contains 2n nodes)

    In most applications, a reasonably balanced binary tree isdesirable

    a

    b c

    d e f g

    h i jA balanced binary tree

    a

    b

    c

    d

    e

    f

    g h

    i jAn unbalanced binary tree

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    11/49

    Other Kinds of Binary Trees

    (Full Binary Trees)

    Full Binary Tree: A full binary tree is a

    binary tree where all the leaves are on the

    same level and every non-leaf has two

    children

    The first four full binary trees are:

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    12/49

    Binary Tree Traversal

    Traversal is the process of visiting every node

    once

    Visiting a node entails doing some processing

    at that node, but when describing a traversal

    strategy, we need not concern ourselves with

    what that processing is

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    13/49

    Binary Tree Traversal Techniques

    Three recursive techniques for binary tree

    traversal

    In each technique, the left subtree is traversed

    recursively, the right subtree is traversed

    recursively, and the root is visited

    What distinguishes the techniques from one

    another is the order of those 3 tasks

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    14/49

    Preoder, Inorder, Postorder

    In Preorder, the rootis visited before (pre)

    the subtrees traversals

    In Inorder, the root isvisited in-between left

    and right subtree traversal

    In Preorder, the rootis visited after (pre)

    the subtrees traversals

    Preorder Traversal:

    1. Visit the root

    2. Traverse left subtree

    3. Traverse right subtree

    Inorder Traversal:

    1. Traverse left subtree

    2. Visit the root

    3. Traverse right subtree

    Postorder Traversal:

    1. Traverse left subtree

    2. Traverse right subtree

    3. Visit the root

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    15/49

    Illustrations for Traversals

    Assume: visiting a node

    is printing its label

    Preorder:

    1 3 5 4 6 7 8 9 10 11 12

    Inorder:

    4 5 6 3 1 8 7 9 11 10 12

    Postorder:

    4 6 5 3 8 11 12 10 9 7 1

    1

    3

    11

    98

    4 6

    5

    7

    12

    10

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    16/49

    Example

    [a+(b-c)]*[(d-e)/(f+g-h)]

    Preorder: *+a-bc/-de-+fgh

    Postorder: abc-+de-fg+h-/*

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    17/49

    Preorder, Postorder and Inorder

    Preorder traversal

    Root, left, right

    prefix expression

    ++a*bc*+*defg

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    18/49

    Preorder, Postorder and Inorder

    Postorder traversal

    left, right, Root

    postfix expression

    abc*+de*f+g*+

    Inorder traversal

    left, Root, right.

    infix expression

    a+b*c+d*e+f*g

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    19/49

    Binary search tree

    Binary search tree

    Every element has a unique key.

    The keys in a nonempty left subtree (right subtree)

    are smaller (larger) than the key in the root of

    subtree.

    The basic idea behind this data structure is to

    have such a storing repository that providesthe efficient way of data sorting, searching

    and retriving.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    20/49

    Binary Search Trees

    Stores keys in the nodes in a way so that searching,insertion and deletion can be done efficiently.

    Binary search tree property For every node X, all the keys in its left subtree are smaller than

    the key value in X, and all the keys in its right subtree are larger

    than the key value in X

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    21/49

    Binary Search Trees

    A binary search tree Not a binary search tree

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    22/49

    Binary search trees

    A BST is a binary tree where nodes are

    ordered in the following way:

    each node contains one key (also known as

    data)

    the keys in the left subtree are less then the

    key in its parent node, in short L < P;

    the keys in the right subtree are greater the

    key in its parent node, in short P < R;

    duplicate keys are not allowed.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    23/49

    Cont.. In the following tree all nodes in the left

    subtree of 10 have keys < 10 while all nodes inthe right subtree > 10.

    Because both the left and right subtrees of a

    BST are again search trees; the abovedefinition is recursively applied to all internal

    nodes:

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    24/49

    Searching BST

    If we are searching for 15, then we are done.

    If we are searching for a key < 15, then we

    should search in the left subtree.

    If we are searching for a key > 15, then we

    should search in the right subtree.

    Searching in a BST

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    25/49

    Searching in a BST Searching in a BST always starts at the root.

    We compare a data stored at the root with the

    key we are searching for.

    If the node does not contain the key we

    proceed either to the left or right childdepending upon comparison.

    If the result of comparison is negative we goto the left child, otherwise - to the right child.The recursive structure of a BST yields arecursive algorithm.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    26/49

    Exercise.

    Given a sequence of numbers:

    11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31

    Draw a binary search tree by inserting the abovenumbers from left to right.

    i

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    27/49

    Insertion The insertion procedure is quite similar to

    searching. We start at the root and recursively go down

    the tree searching for a location in a BST to

    insert a new node. If the element to be inserted is already in the

    tree, we are done (we do not insert

    duplicates). The new node will always replace a NULL

    reference.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    28/49

    BST Insertion

    Insert 7:

    Deletion

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    29/49

    Deletion Deletion is somewhat more tricky than insertion.

    There are several cases to consider. A node to bedeleted (let us call it as Delete)

    is not in a tree;

    is a leaf;

    has only one child;

    has two children.

    If Delete is not in the tree, there is nothing todelete.

    If Delete node has only one child the procedureof deletion is identical to deleting a node from alinked

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    30/49

    cont.. Delete 9:

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    31/49

    Cont..

    If delete node has 2 child then find inorder

    successor of node and replace it.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    32/49

    B Tree

    A node of a tree may contain many records or

    key and pointers to the children.

    A B-Tree is also known as the balanced sort

    tree.

    To reduce disk accesses several conditions

    must be true:

    The height of the tree must be kept to a

    minimum

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    33/49

    Cont.

    DEFINITION:

    A B-tree of order m is an m-way search tree in

    Which, the root has at most m children, but may

    have as few as 2 if it is not a leaf, or none if the

    tree consists of the root alone.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    34/49

    Cont..

    There must be no empty sub trees above the

    leaves of the tree.

    All leaves are on the same level.

    All nodes except the leaves must have at least

    some minimum no. of children.

    B tree of order m has the following properties:

    Each node has maximum of m children or

    minimum of m/2 children or any no. from 2 to

    the maximum.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    35/49

    Cont..

    Each node has one fewer keys than with a

    maximum of m-1 keys.

    Keys are arranged in a defined order within

    the node.

    When a new key is to be inserted into a full

    node, they split into 2 nodes and key with the

    median value is inserted in the parent node

    In case parent node is the root, a new root is

    created

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    36/49

    Operations

    B-Tree of order 4

    Each node has at most 4 pointers and 3 keys, and

    at least 2 pointers and 1 key.

    Insert: 5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, 8

    Delete: 2, 21, 10, 3, 4

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    37/49

    Insert 5, 3, 21

    * 5 *

    * 3 * 5 * 21 *

    * 3 * 5 *

    a

    a

    a

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    38/49

    Insert 9

    * 9 *

    * 3 * 5 * * 21 *

    a

    b c

    Node a splits creating 2 children: b and c

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    39/49

    Insert 1, 13

    * 9 *

    * 1 * 3 * 5 * * 13 * 21 *

    a

    b c

    Nodes b and c have room to insert more elements

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    40/49

    Insert 2

    * 3 * 9 *

    * 1 * 2 * * 13 * 21 ** 5 *

    a

    b d c

    Node b has no more room, so it splits creating node d.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    41/49

    Insert 7, 10

    * 3 * 9 *

    * 1 * 2 * * 10 * 13 * 21 ** 5 * 7 *

    a

    b d c

    Nodes d and c have room to add more elements

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    42/49

    Insert 12

    * 3 * 9 * 13 *

    * 1 * 2 * * 21 ** 5 * 7 * * 10 * 12 *

    a

    b d

    Nodes c must split into nodes c and e

    ec

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    43/49

    Insert 4

    * 3 * 9 * 13 *

    * 1 * 2 * * 21 ** 4 * 5 * 7 * * 10 * 12 *

    b

    a

    ecd

    Node d has room for another element

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    44/49

    Insert 8

    * 3 * 7 *

    * 21 *

    * 13 *

    * 9 *

    * 1 * 2 * * 4 * 5 * * 10 * 12 ** 8 *

    b

    a

    f g

    d h c e

    Node d must split into 2 nodes. This causes node a to split into 2 nodes and the

    tree grows a level.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    45/49

    Delete 2

    * 3 * 7 *

    * 21 *

    * 13 *

    * 9 *

    * 1 * * 4 * 5 * * 10 * 12 ** 8 *

    a

    f g

    b d h c e

    Node b can loose an element without underflow.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    46/49

    Delete 21

    * 3 * 7 *

    * 13 *

    12 *

    * 9 *

    * 1 * * 4 * 5 * * 10 ** 8 *

    a

    f g

    b d h c e

    Deleting 21 causes node e to underflow, so elements are redistributed

    between nodes c, g, and e

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    47/49

    Delete 10

    * 12 * 13 *

    * 3 * 7 * 9 *

    * 1 * * 4 * 5 * * 8 *

    a

    b d h e

    Deleting 10 causes node c to underflow. This causes the parent, node g to

    recombine with nodes f and a. This causes the tree to shrink one level.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    48/49

    Delete 3

    * 12 * 13 *

    * 4 * 7 * 9 *

    * 1 * * 5 * * 8 *

    a

    b d h e

    Because 3 is a pointer to nodes below it, deleting 3 requires keys to be

    redistributed between nodes a and d.

  • 7/28/2019 Data Structure Lecture 7 Tree[1]

    49/49

    Delete 4

    * 12 * 13 *

    * 7 * 9 *

    * 1 * 5 * * 8 *

    a

    h eb

    Deleting 4 requires a redistribution of the keys in the subtrees of 4; however,

    nodes b and d do not have enough keys to redistribute without causing anunderflow. Thus, nodes b and d must be combined.