Top Banner

of 35

Lecture-8-CS210-2012 (1).pptx

Jun 03, 2018

Download

Documents

Moazzam Hussain
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    1/35

    Data Structures and Algorithms

    (CS210/ESO207/ESO211)

    Lecture 8

    Inventing a new data structure (Binary Search Tree)

    1

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    2/35

    Important Notice

    There are basically two ways of introducing a new/innovative solution of a

    problem. One way is to just explain it without giving any clue as to how the

    person who invented the concept came up with this solution. Another way is

    to start from scratch and take a journey of the route which the inventor might

    have followed to arrive at the solution. This journey goes through various

    hurdles and questions, each hinting towards a better insight into the problem

    if we have patience and open mind. Which of these two ways is better ?

    I believe that the second way is better and more effective. The current lectureis based on this way. The data structure we shall invent is called a Binary

    Search Tree. This is the most fundamental and versatile data structure. We

    shall realize this fact many times during the course

    2

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    3/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    4/35

    Linkbased Implementation for lists(recap from previous lecture)

    4

    Head

    Value

    Address of next (or right) node

    Singly Linked List

    Address of previous (left) node

    Doubly Linked List

    node

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    5/35

    Doubly Linked List based implementation versus array

    based implementation of List

    Operation Time Complexity peroperation for arraybased

    implementation

    Time Complexity peroperation for doubly linked

    listbased implementation

    IsEmpty(L) O(1) O(1)

    Search(x,L) O(n) O(n)

    Successor(p,L) O(1) O(1)

    Predecessor(p,L) O(1) O(1)

    CreateEmptyList(L) O(1) O(1)

    Insert(x,p,L) O(n) O(1)

    Delete(p,L) O(n) O(1)

    MakeListEmpty(L) O(1) O(1)

    5Arrays are very rigid

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    6/35

    A fundamental data structure Problem

    Maintain a telephone directory

    Operations: Search the phone # of a person with name x

    Insert a new record (name, phone #,)

    6

    Array based Linked list based

    O(n) O(n)

    O(1) O(1)

    Can we achieve O(log n) bound for

    each operation ?

    Can we improve it ?

    Yes. Keep the array sorted

    according to the namesand do

    Binary search for x.

    Log n

    O(n)

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    7/35

    It seems difficult to achieve O(log n) time complexity for insertoperation using Arrays (due to their rigidity which we have

    seen). So let us focus on doubly linked lists to explore if it is

    possible to achieve an efficient search timeusing them.

    7

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    8/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    9/35

    Take a pause for a few minutes to imagine what will

    be the structure that will emerge if we pursue our

    idea further.

    9

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    10/35

    A new data structure emerges

    10

    head

    2

    28

    46

    67

    9625

    5

    31 41

    35 49

    5348 73

    83

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    11/35

    A new data structure emergesTo analyze it mathematically, remove irrlevant details

    Spend some time over this data structure to see its characteristics.

    How does it look like

    11

    head

    2

    28

    46

    67

    9625

    5

    31 41

    35 49

    5348 73

    83

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    12/35

    Nature is a source of inspiration

    12

    leaves

    joints

    root

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    13/35

    Nature is a source of inspiration

    13

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    14/35

    Nature is a source of inspiration

    14

    root

    leaves

    edgesNodes

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    15/35

    Binary Tree: A mathematical model

    Definition: A collection of nodes is said to form a binary tree if

    1. There is exactly one node with no incoming edge. This node is called the

    rootof the tree.

    2. Every node other than root node has exactly one incoming edge.

    3. Each node has at mosttwo outgoing edges.

    15

    Which of these arenotbinary trees ?

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    16/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    17/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    18/35

    Binary Search Tree (BST)

    Definition:A Binary Tree Tstoring values is said to be Binary Search Tree if for each node vin T

    If left(v) NULL, then value(v) > value of every node in subtree(left(v)).

    If right(v)NULL, then value(v) < value of every node in subtree(right(v)).

    18

    head

    2

    28

    46

    67

    9625

    5

    31 41

    35 49

    5348 73

    83

    Look at the similarity of a BSTwith a sorted array.

    This will be exploited for searching efficiently an element in a BST.

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    19/35

    Search(T,x)

    Searching in a Binary Search Tree

    19

    2

    28

    46

    67

    9625

    5

    31 41

    35 49

    5348 73

    83

    TSearch(T,33) :

    Searching for 33in T.

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    20/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    21/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    22/35

    Homework 1

    Write pseudocode for Insert(T,x) operation similar to

    the pseudocode we wrote forSearch(T,x).

    22

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    23/35

    Homework 2

    Design an algorithm for the following problem:

    Given a sorted array Astoring nelements, build a perfectly

    balanced binary search tree storing all elements of Ain O(n)

    time.

    23

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    24/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    25/35

    A question

    Time complexity of Search(T,x)and Insert(T,x) in a Binary

    Search Tree T= ??

    25

    O(Height(T))

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    26/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    27/35

    Time complexity of Searching and inserting in a

    skewed Binary Search tree on nnodes

    27

    23

    T2

    39

    48

    19

    11

    14

    18

    O(n) time !!

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    28/35

    A hurdle on our way

    Since the elements may be inserted in arbitrary order into (initially empty)

    Binary Search Tree, we may get a skewed tree (with height O(n)). This will

    force O(n) time complexity for search and insert operation.

    Therefore, we need to modify our algorithm so that the height of tree

    remains polylogarithmicof the number of nodes in the tree.

    How to do it ? .

    Since it is difficult to maintain perfectly balanced BST efficiently (think over

    it), we maintain a partially balancedbinary search tree.

    28

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    29/35

    Partially balanced Binary Search Tree

    Terminology:Henceforth, sizeof a binary tree would mean the number of

    nodes present in it.

    Definition:A binary search tree Tis said to be partially balanced at node v, if

    subtree(v) consists of either one or two nodes. Or the ratioof the sizeof the subtreesrooted at its two childrenis at most 2.

    If none of these conditions hold at v, T is said to be partially imbalanced at

    node v.

    Definition:A binary search tree T is said to be partially balanced if it is

    partially balanced at each node; otherwise T is called partially imbalanced.

    29

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    30/35

    Balancing BST periodically:Preserving O(log n)height after each operation

    Each node in T maintains additional field size(v) which is the

    number of nodes in the subtree(v).

    Keep Search(T,x) operation unchanged.

    Modify Insert(T,x) operation as follows:

    Carry out normal insert and update the sizefield of appropriate nodes.

    If BST T gets partially imbalancedat any node v, perfectly balance

    subtree(v).

    30

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    31/35

    Perfectly Balancing subtree at a node v

    31

    v

    Size= k

    Size> 2kSize differs by at most 1

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    32/35

    Notice that the modified Insert operation described is not

    difficult to understand and implement. But it is quite nontrivial

    to show that this algorithm solves our problem. In particular,

    the following facts are not immediate:

    The height of the partially balanced BST on nnodes will be

    O(log n).

    The total time spent in balancing various partially imbalanced

    trees during a sequence of ninsert operations will be small.

    32

    Hopefully you would have now realized that sometimes

    analyzing an algorithm is moredifficult than just designing

    an algorithm. We shall see many such examples during this

    course and the next course (CS345).

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    33/35

    How to analyze this algorithm ?

    H(n): maximum height of a partially balanced BST on nnodes.

    Question:How to show that H(n) = O(log n) ?

    H(1) = 0;H(2) = 1;

    H(3) = 1;

    Hence H(n) 1 + H(

    n)

    1 + 1 + H((

    )n)

    = O( log/n)

    33

    If tree Ton nnodes is partially balanced,

    the maximum size of a subtreerooted at

    any childof root(T) will be ??2

    3( 1)

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    34/35

  • 8/11/2019 Lecture-8-CS210-2012 (1).pptx

    35/35

    Try to achieve the goal mentioned in the

    previous slide (hints mentioned will be very

    useful). The same goal will be accomplished in

    some class next week.

    35