l13dsa

Upload: hargobindb

Post on 08-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 L13DSA

    1/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 1 of 10

    13. Binary Search Trees, AVL trees and Heaps ................................................................. 213.1 Binary search tree ....................................................................................................... 2

    13.1 Operations............................................................................................................... 213.1.1 Searching a BST .............................................................................................. 213.1.2 Inserting into a BST......................................................................................... 3

    13.1.3 Deleting From a BST....................................................................................... 313.1.4 Running-time complexity of BST operations .................................................. 413.2 AVL trees................................................................................................................ 5

    13.2.1 Tree Rotation to obtain a AVL tree from an unbalanced BST ........................ 7Review Questions ......................................................................................................... 10

  • 8/7/2019 L13DSA

    2/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 2 of 10

    13. Binary Search Trees, AVL trees and Heaps

    13.1 Binary search tree

    In computer science, a binary search tree (BST) is a binary tree data structure which hasthe following properties:

    1. Each node (item in the tree) has a distinct value.2. Both the left and right sub trees must also be binary search trees.3. The left sub tree of a node contains only values less than the node's value.4. The right sub tree of a node contains only values greater than the node's value.

    The major advantage of binary search trees over other data structures is that the relatedsorting algorithms and search algorithms such as in-order traversal can be very efficient.

    Binary search trees can choose to allow or disallow duplicate values, depending on theimplementation.

    Figure 1: A binary search tree of size 9 and depth 3, with root 8 and leaves 1, 4, 7 and 13

    13.1 Operations

    13.1.1 Searching a BSTTREE-SEARCH (root, item)P = rootk = item if P = NULL OR k = INFO[P]

    then return Pif k < INFO[P]

    Then return TREE-SEARCH (LLink[P], k)Else return TREE-SEARCH (RLink[P], k)

    ITERATIVE-TREE-SEARCH (root, item)P = rootk = item while (P not equal NULL AND k INFO[P]) do

    if k < INFO [P]Then P = LLink[P]

  • 8/7/2019 L13DSA

    3/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 3 of 10

    Else P = RLink [P]Wendreturn P

    13.1.2 Inserting into a BSTInsertintoBST(item,root)

    P = rootK=item X = GetNode()Info[X] = KLlink = NULLRlink = NULL

    While P!=NULLPrev = PIf K< Info [P]

    P = Llink[P]Else

    P = Rlink[P]End

    WendIf K / \

    (1) (5) (13) (1) (5)/ /

    (3) (3)

  • 8/7/2019 L13DSA

    4/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 4 of 10

    2. Deleting a node with one child --- remove it and move its child (the subtree rootedat its child) up:

    (8) (8)/ \ / \

    (2) (21) (2) (13)/ \ / ===> / \

    (1) (5) (13) (1) (5)/ /

    (3) (3)

    3. Deleting a node with two children --- swap with the smallest keyed-child in itsright subtree, then remove:

    (8) (8)/ \ / \

    (2) (21) (3) (13)/ \ / ===> / \

    (1) (5) (13) (1) (5)/

    (3)

    or swap with the largest keyed-child in its left subtree, then remove:

    (8) (8)/ \ / \

    (2) (21) (1) (13)/ \ / ===> \

    (1) (5) (13) (5)

    / /(3) (3)

    13.1.4 Running-time complexity of BST operations

    search --- in the worst case the search key is not found, but would be located nextto the deepest leaf in the tree. Each step takes only a constant amount of work sothe algorithm is O(h), where h is the height of the tree.

    insert --- in the worst case the insertion takes place at deepest leaf in the tree ---

    the algorithm is also O(h). delete --- the worst case is either the same as for search or occurs when the the

    delete key is found, but that node has two children and either the predecessor orsuccessor of that key is located at the deepest leaf. In either case, the amount of work is bounded by O(h).

    InorderSuccessor(x)P=RLink[x];

  • 8/7/2019 L13DSA

    5/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 5 of 10

    while(LLink[P]!=NULL)P= LLink[P]

    WendReturn P

    DeleteBST(item, root)P = TREE-SEARCH(item,root)X = InorderSuccessor(P)Temp = INFO[P]INFO[P] = INFO[X]INFO[X] = TempFreeNode(X)

    13.2 AVL trees

    A binary tree is more efficient when it is balanced, that is the depth of the two subtrees isalways equal or different by one. This ensures O(log n) performance for most operations.A tree can be 'balanced' by rotation.

    A tree rotation is an operation on a binary search tree that changes the structure withoutinterfering with the order of the elements. A tree rotation moves one node up in the treeand one node down. Another way of describing it is changing the root node of the sub-tree. It helps to try to visualize the tree shifting in the direction of rotation. eg a rightrotation on node Q means Q shifts right, making the left node of Q also shift right andbecome the new root. The subsequent restructuring of the nodes results in a new tree.Rotations can result in the overall height of the tree being reduced.

    In computer science, an AVL tree is a self-balancing binary search tree, and it is the firstsuch data structure to be invented.In an AVL tree, the heights of the two child subtrees of

    any node differ by at most one; therefore, it is also said to be height-balanced. Lookup,insertion, and deletion all take O(log n) time in both the average and worst cases, where nis the number of nodes in the tree prior to the operation. Insertions and deletions mayrequire the tree to be rebalanced by one or more tree rotations.

    The balance factor of a node is the height of its right subtree minus the height of its leftsubtree and a node with balance factor 1, 0, or -1 is considered balanced. A node with anyother balance factor is considered unbalanced and requires rebalancing the tree. Thebalance factor is either stored directly at each node or computed from the heights of thesubtrees.

  • 8/7/2019 L13DSA

    6/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 6 of 10

    Figure 2: Examination shows that each left sub-tree has a height 1 greater than each right sub-tree.

    Figure 3 : Sub-tree with root 8 has height 4 and sub-tree with root 18 has height 2

    Figure 4 : An example of an unbalanced non-AVL tree

  • 8/7/2019 L13DSA

    7/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 7 of 10

    Figure 5 : The same tree after being height-balanced

    13.2.1 Tree Rotation to obtain a AVL tree from an unbalanced BSTA tree rotation is an operation on a binary search tree that changes the structure withoutinterfering with the order of the elements. A tree rotation moves one node up in the treeand one node down. They are used to change the shape of the tree, and in particular todecrease its height by moving smaller subtrees down and larger subtrees up, resulting inimproved performance of many tree operations.

    There exists an inconsistency in different descriptions as to the definition of the directionof rotations. Some say that the direction of a rotation depends on the side which the treenodes are shifted upon whilst others say that it depends on which child takes the root'splace (opposite of the former). This article takes the approach of the side where the nodesget shifted to.

    The right rotation operation as shown in the image above is performed with Q as the root

    and hence is a right rotation on, or rooted at, Q. This operation results in a rotation of thetree in the clockwise direction. The symmetric operation is the left rotation which resultsin a movement in an anti-clockwise direction (the left rotation shown above is rooted atP).

    Assuming this is a binary search tree, as stated above, the elements must be interpreted asvariables and not as alphabetic characters.

  • 8/7/2019 L13DSA

    8/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 8 of 10

  • 8/7/2019 L13DSA

    9/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 9 of 10

    The following examples show rotation around different nodes of varying places anddifferent numbers of children.

  • 8/7/2019 L13DSA

    10/10

    ELE 204 Data Structure and Algorithms L13 -Binary Search Trees, AVL trees and Heaps

    Page 10 of 10

    Review Questions1. Write an algorithm to find the In-order predecessor of a given node in a BST.

    2. Write stackless iterative algorithm to find INORDER, PREORDER andPOSTORDER of BST 3. Find the minimum and maximum values in a BST using a pseudo-code algorithm. 4. Find the height of a BST using a pseudo-code algorithm. 5. Illustrate with a suitable example how a BST can be used as priority queue.

    Further Reading:1. Chapters 8 Data Structures And Algorithms by G A V Pai TMH Publication2. A systemic approach to Data Structures Using C A. M. Padma Reddy3. Internet (search in google for binary search trees & AVL trees)