+ All Categories
Home > Documents > Trees Notes NoRestrictiona

Trees Notes NoRestrictiona

Date post: 03-Apr-2018
Category:
Upload: kumarecit
View: 221 times
Download: 0 times
Share this document with a friend

of 36

Transcript
  • 7/29/2019 Trees Notes NoRestrictiona

    1/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 1

    TREES

    Consider a scenario where you are required to represent the directory structure of your

    operating system.

    The directory structure contains various folders and files. A folder may further contain

    any number of sub folders and files.

    In such a case, it is not possible to represent the structure linearly because all the items

    have a hierarchical relationship among themselves.

    In such a case, it would be good if you have a data structure that enables you to store

    your data in a nonlinear fashion.

    DEFINITION:

    A tree is a nonlinear data structure that represent a hierarchical relationship among

    the various data elements

    Trees are used in applications in which the relation between data elements needs to

    be represented in a hierarchy.

  • 7/29/2019 Trees Notes NoRestrictiona

    2/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 2

    Each element in a tree is referred to as a node.

    The topmost node in a tree is called root.

    Each node in a tree can further have subtrees below its hierarchy.

    Let us discuss various terms that are most frequently used with trees.

    Leaf node: It refers to a node with no children.

    Nodes E, F, G, H, I, J, L, and M are leaf nodes.

    Subtree: A portion of a tree, which can be viewed as a separate tree in itself is called a

    subtree.

  • 7/29/2019 Trees Notes NoRestrictiona

    3/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 3

    A subtree can also contain just one node called the leaf node.

    Tree with root B, containing nodes E, F, G, and H is a subtree of node A.

    Children of a node: The roots of the subtrees of a node are called the children of the

    node.o F, G, and H are children of node B. B is the parent of these nodes.

    Degree of a node: It refers to the number of subtrees of a node in a tree.

    Degree of node C is 1

    Degree of node D is 2

    Degree of node A is 3

    Degree of node B is 4

    Edge: A link from the parent to a child node is referred to as an edge.

    Siblings/Brothers: It refers to the children of the same node.

    Nodes B, C, and D are siblings of each other.

    Nodes E, F, G, and H are siblings of each other.

    Level of a node: It refers to the distance (in number of nodes) of a node from the root.

    Root always lies at level 0.

    As you move down the tree, the level increases by one.

  • 7/29/2019 Trees Notes NoRestrictiona

    4/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 4

    Depth of a tree: Refers to the total number of levels in the tree.

    The depth of the following tree is 4.

    Internal node: It refers to any node between the root and a leaf node.

    Nodes B, C, D, and K are internal nodes.

    Example:

    Consider the above tree and answer the questions that follow:

    a. What is the depth of the tree?

    b. Which nodes are children of node B?

    c. Which node is the parent of node F?

    d. What is the level of node E?

    e. Which nodes are the siblings of node H?

    f. Which nodes are the siblings of node D?

    g. Which nodes are leaf nodes?

    Answer:

    a. 4

    b. D and E

    c. C

    d. 2

    e. H does not have any siblings

    f. The only sibling of D is E

    g. F, G, H, and I

  • 7/29/2019 Trees Notes NoRestrictiona

    5/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 5

    Strictly binary tree:

    A binary tree in which every node, except for the leaf nodes, has non-empty left

    and right children.

    Binary tree is a specific type of tree in which each node can have at most two childrennamely left child and right child.

    There are various types of binary trees:

    Strictly binary tree

    Full binary tree

    Complete binary tree

    Full binary tree:

    A binary tree of depth d that contains exactly 2d 1 nodes.

    Depth d = 3

    Total number of nodes = 2d 1 = 23 1 = 7

  • 7/29/2019 Trees Notes NoRestrictiona

    6/36

    Complete binary tree:

    A binary tree with

    numbered from 0 to

    Binary tree can be represente

    Linked representation of a bin

    It uses a linked list to

    Each node in the link

    Data

    Referenc Referenc

    If a node does not ha

    right child fields of th

    Prepared By M.Raja, CSE, KLU

    nodes and depth d whose nodes correspon

    n 1 in the full binary tree ofdepth k.

    by Array and linked list.

    ary tree:

    implement a binary tree.

    d representation holds the following infor

    to the left childto the right child

    e a left child or a right child or both, the res

    at node point to NULL.

    Unit III - Tree

    6

    to the nodes

    ation:

    pective left or

  • 7/29/2019 Trees Notes NoRestrictiona

    7/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 7

    Traversing a Binary Tree:

    You can implement various operations on a binary tree.

    A common operation on a binary tree is traversal.

    Traversal refers to the process of visiting all the nodes of a binary tree once.

    There are three ways for traversing a binary tree:

    Inorder traversal

    Preorder traversal

    Postorder traversal

    InOrder Traversal:

    In this traversal, the tree is visited starting from the root node. At a particular

    node, the traversal is continued with its left node, recursively, until no further left node

    is found. Then the data at the current node (the left most node in the sub tree) is visited,

    and the procedure shifts to the right of the current node, and the procedure is continued.

    This can be explained as:

    1. Traverse the left subtree

    2. Visit root

    3. Traverse the right subtree (Left Data Right)

  • 7/29/2019 Trees Notes NoRestrictiona

    8/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 8

    Let us consider an example.

    The left subtree of node A is not NULL.

    Therefore, move to node B to traverse the left

    subtree of A.

    1

    The left subtree of node B is not NULL.

    Therefore, move to node D to traverse the

    left subtree of B.

    The left subtree of node D is NULL.

    Therefore, visit node D.

    3

    Left subtree of H is empty.

    Therefore, visit node H.

    4

  • 7/29/2019 Trees Notes NoRestrictiona

    9/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 9

    Right subtree of H is empty.

    Therefore, move to node B.

    5

    The left subtree of B has been visited.

    Therefore, visit node B.

    6

    Right subtree of B is not empty.

    Therefore, move to the right subtree of B.

    Left subtree of E is empty.

    Therefore, visit node E.

  • 7/29/2019 Trees Notes NoRestrictiona

    10/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 10

    Right subtree of E is empty.

    Therefore, move to node A.

    7

    Left subtree of A has been visited.

    Therefore, visit node A.

    8

    Right subtree of A is not empty.

    Therefore, move to the right subtree of A.

    9

    Left subtree of C is not empty.

    Therefore, move to the left subtree of C.

    10

  • 7/29/2019 Trees Notes NoRestrictiona

    11/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 11

    Left subtree of F is empty.

    Therefore, visit node F.

    11

    Right subtree of F is empty.

    Therefore, move to node C.

    12

    The left subtree of node C has been visited.

    Therefore, visit node C.

    13

    Right subtree of C is not empty.

    Therefore, move to the right subtree of

    node C.

    14

  • 7/29/2019 Trees Notes NoRestrictiona

    12/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 12

    Left subtree of G is not empty.

    Therefore, move to the left subtree of node G.

    15

    Left subtree of I is empty.

    Therefore, visit I.

    16

    Right subtree of I is empty.

    Therefore, move to node G.

    17

    Visit node G.

    18

  • 7/29/2019 Trees Notes NoRestrictiona

    13/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 13

    Right subtree of G is empty.

    19

    Preorder Traversal

    In this traversal, the tree is visited starting from the root node. At a particular node,

    the data is read (visited), then the traversal continues with its left node, recursively,

    until no further left node is found. Then the right node of the recent left node is set as the

    current node, and the procedure is continued. This can be explained as:

    1. Visit root

    2. Traverse the left subtree

    3. Traverse the right subtree

    PostOrder Traversal:

    In this traversal, the tree is visited starting from the root node. At a particular node,

    the traversal is continued with its left node, recursively, until no further left node isfound. Then the right node of the current node (the left most node in the sub tree) is

    visited, and the procedure shifts to the right of the current node, and the procedure

    is continued. This can be explained as:

  • 7/29/2019 Trees Notes NoRestrictiona

    14/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 14

    1. Traverse the left subtree

    2. Traverse the right subtree

    3. Visit the root

    BINARY SEARCH TREE

    An important application of binary trees is their use in searching. Let us assume that

    each node in the tree is assigned a key value.

    The property that makes a binary tree into a binary search tree is that for every node,

    X, in the tree, the values of all the keys in the left subtree are smaller than the key value in

    X, and the values of all the keys in the right subtree are larger than the key value in X.

    Notice that this implies that all the elements in the tree can be ordered in some consistent

    manner. In Figure 4.15, the tree on the left is a binary search tree, but the tree on the right is

    not. The tree on the right has a node with key 7 in the left subtree of a node with key 6

    (which happens to be the root).

  • 7/29/2019 Trees Notes NoRestrictiona

    15/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 15

    FIND AN ELEMENT IN A TREE:

    To find an element in a Tree we check if the Tree structure T is exist or not. If it does not exist it

    returns NULL.

    If T is exist, then we can just return. Otherwise, if the key stored at T is x, we can return T.

    Otherwise, we make a recursive call on a subtree of T, either left or right, depending on the

    relationship of x to the key stored in T.

    The code is an implementation of this strategy.

    ypedef

    tree_ptr

    find( element_type x, SEARCH_TREE T )

    {

    if( T == NULL )

    return NULL;

    if( x < T->element )

    return( find( x, T->left ) );else

    if( x > T->element )

    return( find( x, T->right ) );

    else

    return T;

    }

  • 7/29/2019 Trees Notes NoRestrictiona

    16/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 16

    Find_min and find_max:

    These routines return the position of the smallest and largest elements in the tree,

    respectively.

    To perform a find_min, start at the root and go left as long as there is a left child.

    Finally the left most node is the minimum element of the tree.

    The stopping point is the smallest element.

    The find_max routine is the same, except that branching is to the right child.

    The right most element is the maximum value.

    Findmin:

    tree_ptr

    find_min( SEARCH_TREE T )

    {

    if( T == NULL )

    return NULL;

    else

    if( T->left == NULL )

    return( T );

    else

    return( find_min ( T->left ) );

    }

    Findmax:

    tree_ptr

    find_max( SEARCH_TREE T )

    {

    if( T != NULL )

    while( T->right != NULL )

    T = T->right;

    return T;

    }

  • 7/29/2019 Trees Notes NoRestrictiona

    17/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 17

    Insert:

    The insertion routine is conceptually simple. To insert x into tree T, proceed down the

    tree as you would with a find.

    If x is found, do nothing (or "update" something). Otherwise, insert x at the last spot on

    the path traversed.

    To insert 5, we traverse the tree as though a find were occurring. At the node with key

    4, we need to go right, but there is no subtree, so 5 is not in the tree, and this is the

    correct spot.

    Duplicates can be handled by keeping an extra field in the node record indicating the

    frequency of occurrence. This adds some extra space to the entire tree, but is better than

    putting duplicates in the tree (which tends to make the tree very deep). Of course this

    strategy does not work if the key is only part of a larger record.

    If that is the case, then we can keep all of the records that have the same key in an

    auxiliary data structure, such as a list or another search tree.

    Since T points to the root of the tree and the root changes on the first insertion, insert

    is written as a function that returns a pointer to the root of the new tree. Lines 8 and 10

    recursively insert and attach x into the appropriate subtree.

  • 7/29/2019 Trees Notes NoRestrictiona

    18/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 18

    Type Declarations:

    Struct treeNode

    {

    Int element:

    Struct TreeNode *left, *right;}

    Typedef struct treeNode *node;

    Typedef int ElementType;

    Node insert(ElementType, node)

    Node delete(Elementtype, node)

    void Findmin(node)

    void Findmax(node)

    void display(node, int);

    Syntax: Insertion

    insert( element_type x, SEARCH_TREE T )

    {

    if( T == NULL )

    {

    /* Create and return a one-node tree */

    T = (SEARCH_TREE) malloc ( sizeof (struct tree)

    if( T == NULL )

    printf( " tree does not exist!!!" );

    else

    {

    T->element = x;

    T->left = T->right = NULL;

    }

    }

    else

    if( x < T->element )

    T->left = insert( x, T->left );

    else

    if( x > T->element )

    T->right = insert( x, T->right );

  • 7/29/2019 Trees Notes NoRestrictiona

    19/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 19

    /* else x is in the tree already. We'll do nothing

    */

    return T; /* Don't forget this line!! */

    }

    Delete:

    As is common with many data structures, the hardest operation is deletion. Once we

    have found the node to be deleted, we need to consider three possibilities.

    Case I: Node to be deleted is the leaf node

    Case II: Node to be deleted has one child (left or right)

    Case III: Node to be deleted has two children

    If the node is a leaf, it can be deleted immediately.

    If the node has one child, the node can be deleted after its parent adjusts a pointer to

    bypass the node (we will draw the pointer directions explicitly for clarity).

    Notice that the deleted node is now unreferenced and can be disposed of only if a pointer

    to it has been saved.

    The complicated case deals with a node with two children.

    The general strategy is to replace the key of this node with the smallest key of the right

    subtree (which is easily found) and recursively delete that node (which is now empty).

    Because the smallest node in the right subtree cannot have a left child, the second delete

    is an easy one. Figure 4.24 shows an initial tree and the result of a deletion.

    The node to be deleted is the left child of the root; the key value is 2. It is replaced with

    the smallest key in its right subtree (3), and then that node is deleted as before.

  • 7/29/2019 Trees Notes NoRestrictiona

    20/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 20

    The code performs deletion. It is inefficient, because it makes two passes down the

    tree to find and delete the smallest node in the right subtree when this is appropriate. It is

    easy to remove this inefficiency, by writing a special delete_min function, and we have left

    it in only for simplicity.

    If the number of deletions is expected to be small, then a popular strategy to use is

    lazy deletion: When an element is to be deleted, it is left in the tree and merely marked asbeing deleted. This is especially popular if duplicate keys are present, because then the field

    that keeps count of the frequency of appearance can be decremented. If the number of real

    nodes in the tree is the same as the number of "deleted" nodes, then the depth of the tree is

  • 7/29/2019 Trees Notes NoRestrictiona

    21/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 21

    only expected to go up by a small constant (why?), so there is a very small time penalty

    associated with lazy deletion.

    delete( element_type x, SEARCH_TREE T ){

    tree_ptr tmp_cell, child;

    if( T == NULL )

    error("Element not found");

    else

    if( x < T->element ) /* Go left */

    T->left = delete( x, T->left );

    else

    if( x > T->element ) /* Go right */

    T->right = delete( x, T->right );

    else /* Found element to be deleted */

    if( T->left && T->right ) /* Two children */

    { /* Replace with smallest in right subtree */

    tmp_cell = find_min( T->right );

    T->element = tmp_cell->element;

    T->right = delete( T->element, T->right );

    }

    else /* One child */

    }

    tmp_cell = T;

    if( T->left == NULL ) /* Only a right child */

    child = T->right;

    if( T->right == NULL ) /* Only a left child */

    child = T->left;

    free( tmp_cell );

  • 7/29/2019 Trees Notes NoRestrictiona

    22/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 22

    return child;

    }

    return T;

    }

    AVL Tree

    In a binary search tree, the time required to search for a particular value depends upon its

    height.

    The shorter the height, the faster is the search.

    However, binary search trees tend to attain large heights because of continuous insert

    and delete operations.

    Consider an example in which you want to insert some numeric values in a binary

    search tree in the following order:

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

    After inserting values in the specified order, the binary search tree appears as

    follows:

    This process can be very time consuming if the number of values stored in a binary

    search tree is large.

  • 7/29/2019 Trees Notes NoRestrictiona

    23/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 23

    Now if you want to search for a value 14 in the given binary search tree, you will

    have to traverse all its preceding nodes before you reach node 14. In this case, you

    need to make 14 comparisons

    Therefore, such a structure loses its property of a binary search tree in which afterevery comparison, the search operations are reduced to half.

    To solve this problem, it is desirable to keep the height of the tree to a minimum.

    Therefore, the following binary search tree can be modified to reduce its height.

    The height of the binary search tree has now been reduced to 4

    Now if you want to search for a value 14, you just need to traverse nodes 8 and 12,

    before you reach node 14

    In this case, the total number of comparisons to be made to search for node 14 is

    three.

    This approach reduces the time to search for a particular value in a binary search tree.

    This can be implemented with the help of a height balanced tree.

  • 7/29/2019 Trees Notes NoRestrictiona

    24/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 24

    Height Balanced Tree

    A height balanced tree is a binary tree in which the difference between the heights of

    the left subtree and right subtree of a node is not more than one.

    In a height balanced tree, each node has a Balance Factor (BF) associated with it.

    For the tree to be balanced, BF can have three values:

    0: A BF value of 0 indicates that the height of the left subtree of a node is

    equal to the height of its right subtree.

    1: A BF value of 1 indicates that the height of the left subtree is greater than

    the height of the right subtree by one. A node in this state is said to be left

    heavy.

    1: A BF value of 1 indicates that the height of the right subtree is greater

    than the height of the left subtree by one. A node in this state is said to be

    right heavy.

    Fig. Balanced Binary Search Trees

    After inserting a new node in a height balanced tree, the balance factor of one or

    more nodes may attain a value other than 1, 0, or 1.

    This makes the tree unbalanced.

    In such a case, you first need to locate the pivot node.

    A pivot node is the nearest ancestor of the newly inserted node, which has a balance

    factor other than 1, 0 or 1.

    To restore the balance, you need to perform appropriate rotations around the pivot

    node.

  • 7/29/2019 Trees Notes NoRestrictiona

    25/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 25

    Inserting Nodes in a Height Balanced Tree

    Insert operation in a height balanced tree is similar to that in a simple binary search tree.

    However, inserting a node can make the tree unbalanced.

    To restore the balance, you need to perform appropriate rotations around the pivot node.

    This involves two cases:

    When the pivot node is initially right heavy and the new node is inserted in the right

    subtree of the pivot node.

    When the pivot node is initially left heavy and the new node is inserted in the left

    subtree of the pivot node.

    Let us first consider a case in which the pivot node is initially right heavy and the new

    node is inserted in the right subtree of the pivot node.

    In this case, after the insertion of a new element, the balance factor of pivot node

    becomes 2.

    Now there can be two situations in this case:

    If a new node is inserted in the right subtree of the right child of the pivot node.(LL)

    If the new node is inserted in the left subtree of the right child of the pivot node.(RR)

    Consider the first case in which a new node is inserted in the right subtree of the right

    child of the pivot node.

  • 7/29/2019 Trees Notes NoRestrictiona

    26/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 26

    Single Rotation:

    The two trees in Figure 4.31 contain the same elements and are both binary search trees.

    First of all, in both trees k 1 < k 2. Second, all elements in the subtree X are smaller

    than k 1 in both trees.

    Third, all elements in subtree Z are larger than k 2. Finally, all elements in subtree Y are

    in between k 1 and k 2. The conversion of one of the above trees to th e other is known as

    a rotation.

    A rotation involves only a few pointer changes (we shall see exactly how many later),

    and changes the structure of the tree while preserving the search tree property.

    The rotation does not have to be done at the root of a tree; it can be done at any node in

    the tree, since that node is the root of some subtree.

    It can transform either tree into the other.

    This gives a simple method to fix up an AVL tree if an insertion causes some node in an

    AVL tree to lose the balance property: Do a rotation at that node.

    The basic algorithm is to start at the node inserted and travel up the tree, updating the

    balance information at every node on the path. If we get to the root without having found

    any badly balanced nodes, we are done. Otherwise, we do a rotation at the first bad node

    found, adjust its balance, and are done (we do not have to continue going to the root). In

    many cases, this is sufficient to rebalance the tree. For instance, in Figure 4.32, after the

    insertion of the in the original AVL tree on the left, node 8 becomes unbalanced. Thus,

    we do a single rotation between 7 and 8, obtaining the tree on the right.

  • 7/29/2019 Trees Notes NoRestrictiona

    27/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 27

    Figure 4.32 AVL property destroyed by insertion of 6 1/2 , then fixed by a rotation

    Let us work through a rather long example. Suppose we start with an initially empty

    AVL tree and insert the keys 1 through 7 in sequential order. The first problem occurs when

    it is time to insert key 3, because the AVL property is violated at the root. We perform a

    single rotation between the root and its right child to fix the problem. The tree is shown in

    the following figure, before and

    after the rotation:

  • 7/29/2019 Trees Notes NoRestrictiona

    28/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 28

    To make things clearer, a dashed line indicates the two nodes that are the subject of

    the rotation. Next, we insert the key 4, which causes no problems, but the insertion of 5

    creates a violation at node 3, which is fixed by a single rotation. Besides the local change

    caused by the rotation, the programmer must remember that the rest of the tree must be

    informed of this change. Here, this means that 2's right child must be reset to point to 4

    instead of 3. This is easy to forget to do and would destroy the tree (4 would be

    inaccessible).

    Next, we insert 6. This causes a balance problem for the root, since its left subtree is

    of height 0, and its right subtree would be height 2. Therefore, we perform a single rotation

    at the root between 2 and 4.

  • 7/29/2019 Trees Notes NoRestrictiona

    29/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 29

    The rotation is performed by making 2 a child of 4 and making 4's original left

    subtree the new right subtree of 2. Every key in this subtree must lie between 2 and 4, so

    this transformation makes sense. The next key we insert is 7, which causes another rotation.

    Double Rotation:

    The algorithm described in the preceding paragraphs has one problem. There is a

    case where the rotation does not fix the tree. Continuing our example, suppose we insert

    keys 8 through 15 in reverse order. Inserting 15 is easy, since it does not destroy the balance

    property, but inserting 14 causes a height imbalance at node 7.

  • 7/29/2019 Trees Notes NoRestrictiona

    30/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 30

    As the diagram shows, the single rotation has not fixed the height imbalance. The

    problem is that the height imbalance was caused by a node inserted into the tree containing

    the middle elements (tree Y in Fig. 4.31) at the same time as the other trees had identical

    heights. The case is easy to check for, and the solution is called a double rotation, which is

    similar to a single rotation but involves four subtrees instead of three. In Figure 4.33, the

    tree on the left is converted to the tree on the right. By the way, the effect is the same as

    rotating between k1 and k2 and then between k2 and k3.

  • 7/29/2019 Trees Notes NoRestrictiona

    31/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 31

    In our example, the double rotation is a right-left double rotation and involves 7, 15,

    and 14. Here, k3 is the node with key 7, k1 is the node with key 15, and k2 is the node withkey 14. Subtrees A, B, C, and D are all empty.

    Next we insert 13, which require a double rotation. Here the double rotation is again

    a right-left double rotation that will involve 6, 14, and 7 and will restore the tree. In this

    case, k 3 is the node with key 6, k 1 is the node with key 14, and k 2 is the node with key 7.

    Subtree A is the tree rooted at the node with key 5, subtree B is the empty subtree that was

    originally the left child of the node with key 7, subtree C is the tree rooted at the node with

    key 13, and finally, subtree D is the tree rooted at the node with key 15.

  • 7/29/2019 Trees Notes NoRestrictiona

    32/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 32

    If 12 is now inserted, there is an imbalance at the root. Since 12 is not between 4 and

    7, we know that the single rotation will work.

    Insertion of 11 will require a single rotation:

  • 7/29/2019 Trees Notes NoRestrictiona

    33/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 33

    To insert 10, a single rotation needs to be performed, and the same is true for the

    subsequent insertion of 9. We insert 8 without a rotation, creating the almost perfectly

    balanced tree that follows.

    Finally, we insert 81/2 to show the symmetric case of the double rotation. Notice that

    81/2 causes the node containing 9 to become unbalanced. Since 81/2 is between 9 and 8

    (which is 9's child on the path to 81/2, a double rotation needs to be performed, yielding the

    following tree.

  • 7/29/2019 Trees Notes NoRestrictiona

    34/36

    Example:

    Let us consider an example t

    whenever required.

    50 40 30 60 55

    Insert 50

    Insert 40

    Tree is balan

    Insert 30

    Before rotation

    Tree becomes unbalanced A si

    Prepared By M.Raja, CSE, KLU

    insert values in a binary search tree and restor

    80 10 35 32

    tree is balanced.

    ced.

    After rotation

    gle right rotation restores the balance

    Unit III - Tree

    34

    its balance

  • 7/29/2019 Trees Notes NoRestrictiona

    35/36

    Unit III - Tree

    Prepared By M.Raja, CSE, KLU 35

    Tree becomes unbalanced, A single left rotation restores the balance

    Before Rotation After rotation

    Now the Tree is unbalanced, A double rotation restores the balance

    Before Rotation After Rotation

    Insert 80 (single left rotation)

  • 7/29/2019 Trees Notes NoRestrictiona

    36/36

    Unit III - Tree

    Insert 10 Insert 35

    Insert 32 (double rotation)

    Finally the Tree become balanced.

    *******


Recommended