+ All Categories
Home > Documents > Comp1927 Qs

Comp1927 Qs

Date post: 15-Feb-2018
Category:
Upload: vem
View: 221 times
Download: 0 times
Share this document with a friend

of 22

Transcript
  • 7/23/2019 Comp1927 Qs

    1/22

    Tutorial Exercises Week 1

    Exercise 1 - Linked Lists

    1. Consider the material on linked lists that we discussed in the lecturewith the following definition (Itemis a charnow):

    typedef char Item; typedef struct node *link; struct

    node { Item item; link next; };

    Write the following functions:

    o A function called length that takes a link to the first element of a

    list and returns the length of the list

    o A function called duplicate that takes a link to the first element of

    a list as an argument and returns a copy of the list (ie a new listthat contains the same data, in the same order).

    o Here are the prototypes for some functions you may wish to use

    for this task (you can take the code from the lecture)

    // Create a new node, initialised with the item

    provided. Return // pointer to node (link) link

    newNode (Item item); // Insert a new node into a

    given non-empty list // The node is inserted

    directly after the head of the list ls void

    insertNext (link ls, link node);

    . Assume we ha!e a function printListwhich, gi!en a list of characters,

    prints each character, and a function "cstr#o$ist" which con!erts aregular C string (i.e., "%&" terminated array of characters) into a list ofcharacters. What is the output of the following program' (eeimplementation of reversein *ercise .)

    int main (int argc, const char * argv[]) { link ls =

    cstrToList ("hello world!"); link ls2 = duplicate(ls);

    printList (ls); printList (ls2); printList (reverse

    (ls)); printList (ls); printList (ls2); return 0; }

    Exercise 2 - Linked Lists, an alternativeimplementation

    #he following implementation of lists distinguishes +etween a link to a

    seuence of items and a list. #he list contains additional information, inthis case, the length and also a pointer to the last element of the list.

  • 7/23/2019 Comp1927 Qs

    2/22

    typedef char Item; typedef struct node *link; struct

    node { Item item; link next; }; typedef struct

    listImpl *list; struct listImpl { int size; link

    first; link last; };

    Write a function called create$ist to create a new empty list. -iscussthe difference +etween an empty list in our original implementation andour current implementation.

    odify the length function you wrote in e*cercise 1 to work with our

    new implementation.

    How does the prototypes of the following function change as aconseuence of the alternati!e implementation:

    link reverse (link ls); void insertNext(link ls, link

    node); void deleteNext(link ls);

    /elow is the code presented in the lecture for re!ersing a list, +y

    passing in a pointer to the first element of the list. Adapt this code towork on the list type descri+ed a+o!e.

    link reverse (link ls) { link tmp; link curr = ls;

    link rev = NULL; while (curr != NULL) { tmp =

    curr->next; curr->next = rev; rev = curr;

    curr = tmp; } return rev; }

    What are the ad!antages and disad!antages of your adapted !ersiono!er the one discussed in the lecture' Which operations can +eimplemented more efficiently'

    Exercise 3 - Double linked lists

    0n the lecture, we +riefly discussed dou+ly linked lists.

    typedef char Item; typedef struct dnode *dlink;

    struct dnode { Item item; dlink next;dlink prev; };

    Write a function

    dlink append (dlink list1, dlink2 list2)

    which attaches the list list2at the end of list1and returns the resulting list.

    0s it necessary to return the resulting list, or would the following interface work(as list1is altered)

  • 7/23/2019 Comp1927 Qs

    3/22

    void append (dlink list1, dlink list2)

    #utorial *ercises Week &Assignment 1-iscuss the assignment. What types of information do you need to record intesting.t*t' #hink of some testing ideas with your class and your tutor.

    our implementation must keep the lines of a te*t+uffer in a linked datastructure such as a linked list or !ariant of that. ach line must +e representedas a dynamically allocated character array. -iscuss what this means.

    *ercise 1Using tacks and !ueues

    Consider the following interfaces gi!en in lectures, for a stack and a ueue

    // Stack.h typedef struct stackImp *Stack; //Function

    Prototypes Stack createStack( void ); void

    destroyStack( Stack stack ); Item pop( Stack stack);

    void push( Stack stack, Item data); int

    stackSize( Stack stack);

    // Queue.h typedef struct queueImp *Queue; //Function

    Prototypes Queue createQueue( void ); void

    destroyQueue( Queue queue ); Item get( Queue queue);

    void put( Queue queue, Item data); int

    queueSize( Queue queue);

    a. 2sing two stack data structures, show how you can implement a ueuedata structure.

    +. 2sing two ueue data structures, show how you can implement a stackdata structure.

    c. 2sing only the functions for manipulating stacks in the stack interface,write a function that 3oins the two stacks stack1 and stack so thatstack1 is 4stacked4 on stack.

    5ote that the contents of stack1 do not need to +e preser!ed.

    void stackStacks(Stack stack1, Stack stack2);

    *ercise "mplementing a tack #it$ a Linked List

    0mplement the functions from the tack.h interface gi!en in *ercise 1, usinga linked list structure. How will you represent your stack'

  • 7/23/2019 Comp1927 Qs

    4/22

    typedef struct stackNode * link; struct stackNode{

    //You need to decide what goes in here } struct

    stackImp { //You need to decide what goes in

    here };

    Assume you ha!e the following local helper function to create 5odes asfollows

    static link createNode(Item item);

    *ercise 6 7 tacksWrite a program which, gi!en a string containing round, curly, and suare+rackets, will print 8success8 if the parentheses are correctly +alanced, and8fail8 otherwise.

    > ./check_parens "()" success > ./check_parens

    "([])" success > ./check_parens "([]){}" success> ./check_parens "([]){" fail > ./check_parens

    "([sdfdf]ss)fdfsd" success

    Hint:the pro+lem is similar to the postfi* e*pression e!aluation pro+lem.*ercise 9 7 unction ;ointers

  • 7/23/2019 Comp1927 Qs

    5/22

    Calculate how long #(n) steps would take for different si?es of n for the #(n)functions in the ta+le +elow. Assume you are running it on a computer thatperforms one million steps per millisecond. 5ote: A millisecond is athousandth of a second.

    nT(n) =log n

    T(n)= n

    T(n) = nlog n

    T(n) =n2

    T(n) =n3

    T(n) =2n

    10

    20

    50

    100

    1000

    10000

    or what si?e of n does the computation time for #(n) @ n+ecome too large to+e practical' Would it help if we used a computer that was a million timesfaster'

    *ercise Write a recursi!e function

    int allEven (int a[], int l, int r);

    which takes an array, the left7most and right7most inde* of the currentsegment of the array as an argument and checks if all elements in an arrayare e!en.

    0t must use a di!ide and conuer approach, +y splitting the array in half, firstchecking if all the elements in the left half are e!en, and then (only if

    necessary) checking the right half.

    *ercise 6(inar) earc$ Tree "nsertion, Deletion and Traversal

    0nsert the following keys into a /#: 1& & = 6& 1= = 9

    What is the height of the resulting tree'

    -elete = 6& & (assume we replace nodes with the left7most of the right su+7tree when necessary)

    What is the height of the resulting tree'

  • 7/23/2019 Comp1927 Qs

    6/22

    how the output o+tained +y tra!ersing the tree and printing out each node inthe following orders:

    prefi* (5$)

    postfi* ($5)

    infi* ($5)

    *ercise 9(T %unctions

    Assume the following representation of a /#

    typedef struct treenode *treelink; struct treenode{

    Item item; treelink left; treelink right; }

    Assume your tree holds items of type int. Write a function to recursi!ely

    sum the items of a /# tree. our function should ha!e the followingprototype:

    int sumItems(treelink tree);

    Write a function that searches for a gi!en item in a /#. our function

    should return 1 if the item is found and & otherwise. ou should use aniterati!e approach and a recursi!e approach

    int iterativeSearch(treelink t, Item i);

    int recursiveSearch(treelink t, Item i);

    Write a function that will free all the memory associated with a tree

    void freeTree(treelink t);

    Write a function to insert an item into a /#. 0t should return the root ofthe tree.

    treelink insert(treelink t, Item i);

    #utorial *ercises Week &9*ercise 1*eaps#race the addition of the following keys in turn to a heap with ma*imum ontop. #race it with +oth the linked tree7+ased and array heap structures.

    ==, 9=, 91, B=, 1, BD, 19, ==, 9, >, B6, 6=

    #race the deletion of the ma*imum element (>) from the heap.

    *ercise

  • 7/23/2019 Comp1927 Qs

    7/22

    +aive (ubble ort

    1 void bubbleSort(int items[], int n) { 2 int i,

    j; 3 4 for(i = n - 1; i > 0 ; i--) { 5

    for(j = 1; j

  • 7/23/2019 Comp1927 Qs

    8/22

    a. how how each of the arrays from the pre!ious uestion (sorted,random, re!erse), change as they are sorted +y the program a+o!e.or each one count the num+er of comparisons and num+er of shifts.

    +. With each line of code associate a cost and a formula e*pressing the

    num+er of times the C statement on that line will +e e*ecuted whensorting nitems in the worst case.

    c. What is the asymptotic worst case time comple*ity of the algorithmimplemented +y this program.

    d. What is the time comple*ity of the algorithm implemented +y thisprogram in the +est case'

    *ercise 9

    a. *plain what sta+ility means in the conte*t of sorting

    +. uppose you ha!e an implementation of a sorting algorithm that sortsstrings and is case insensiti!e (for e*ample 8a8 and 8A8 are considered to+e eual). *plain what is wrong with the following argument:

    0 ran the following input through the program

    AAAAA zzzzz abcde aaaaa

    and the output of the program was

    AAAAA aaaaa abcde zzzzz

    #his means my sorting program is sta+le

    *ercise =orting Linked Lists

    0mplement selection sort, gi!en the following definition of a linked list

    typedef struct node * link; struct node{ Item

    item; link next; };

    #utorial *ercises Week &=*ercise 1!uicksort

    #he following implementation for uicksort is taken fromedgewick Algorithms in C:

  • 7/23/2019 Comp1927 Qs

    9/22

    typedef int Item; #define key(A) (A) #define less(A,

    B) (key(A) < key(B)) #define exch(A, B) { Item t = A;

    A = B; B = t; } int partition(Item a[], int l, int

    r); void quicksort (Item a[], int l, int r) { int

    i; if (r = j) break; exch(a[i],

    a[j]); } exch(a[i], a[r]); return i; }

    #race the e*ecution of the partition function on sorting the following inputseuence of num+ers: 4 7 1 1 4 6 7 2 5 6.

    #race the e*ecution of the partition function on the following seuence ofnum+ers: 1 2 3 4 5 6 7 8 9 10

    *ercise !uicksort #it$ median o t$ree partitioning

    Ene impro!ement to the Fuicksort algorithm that we discussed was the use ofedian7of7#hree partitioning. 0n this !ersion of Fuicksort, three items in thearray are sampled and the median of the three !alues is used to partition thearray. Without looking at the C code gi!en in lectures, complete the followingprogram +y replacing the comments with the rele!ant C program statements.

    // Quick sort with Median of Three Partitioning void

    quicksortMT (Item a[], int l, int r) { int i;

    if (r 1){

    medianOfThreePivot(a,l,r); i = partition (a,

    l+1, r-1); } else { i = partition (a, l, r);

    } quicksortMT (a, l, i-1); quicksortMT (a, i+1,

    r); } void medianOfThreePivot(int items[], int low,

    int high){ // Swap median (value mid-way

    between low and high) with items[high - 1] //

    Compare items[low], items[high - 1] and items[high]// Rearrange values: // lowest value to be stored

    in items[low] // highest value to be stored in

    items[high] // median value to be stored in

    items[high-1] }

    #race the call of medianEf#hree;i!ot and then partition on the seuence 1 23 4 5 6 7 8 9 10

    *ercise 6ergesort

    /riefly e*plain the top down recursi!e ergesort algorithm.

  • 7/23/2019 Comp1927 Qs

    10/22

    #race it8s e*ecution on the input: 4 7 1 1 4 6 7 2 5 6.

    *plain the good and +ad points of the ergesort algorithm.

    *ercise 9

  • 7/23/2019 Comp1927 Qs

    11/22

    *ercise 1.$eapest Least /isited trategies

    0n what order would you !isit the nodes if you started at !erte* & and used thecheapest least !isited approach from assn and you had stamina of 1&&&&&'

    (how the first 1& !ertices you would !isit in the order you would tra!el tothem).

    What a+out if you had a stamina le!el of =&'

    *ercise Dept$ %irst Traversaluppose that you started at the node la+elled &. how the order in whichnodes might +e !isited while performing a depth7first tra!ersal of the graph.(ou can assume, when faced with a choice of which node to !isit ne*t, you!isit the node with the lowest la+el.)

    What would the contents of the st array +e'

    how the same - tra!ersal, +ut include !ertices you +acktrack to.

    What a+out if you started from node 6'

    *ercise 6&rap$ "mplementations

    //Graph.h definitions // vertices denoted by integers0..N-1 typedef struct graphRep * Graph; typedef int

    Vertex; typedef struct edge Edge; // edges are

    pairs of vertices (end-points) struct edge

    { Vertex v; Vertex w; } ;

    //Adjacency matrix unweighted graph representation

    struct graphRep { int nV; // #vertices

    int nE; // #edges int **adj; // matrix of

    booleans (0 or 1) };

    //Adjacency list graph representation typedef struct

    vNode *VList; struct vNode { Vertex v;VList next; }; struct graphRep { int nV; //

    #vertices int nE; // #edges VList VList

    *adj; // array of lists };

    0mplement a function

    int isEdgeInGraph (Graph G, Edge e);

    that tests whether a gi!en graph edge is present in the graph. #he

    function should return 1 if the edge e*ists in the graph and & otherwise.

  • 7/23/2019 Comp1927 Qs

    12/22

    0mplement the function for +oth the ad3acency7matri* and thead3acency7list representations.

    How could you change the implementations to represent a weighted

    graph with weights of type int'

    Would your implementations of isdge0n

  • 7/23/2019 Comp1927 Qs

    13/22

    how the resulting dist and st array !alues.

    . the Iruskal # algorithm

    What does the spanning tree look like'

    What is the total cost'

    *ercise 9(inar) earc$ Trees

    Consider the alternate implementation of /#s discussed in lecturesrepresented +y:

    typedef struct STnode* link; struct STnode { Item

    item; link left, right; int size; }; staticlink rootNodeLink, emptyTree; void STinit (int n) {

    rootNodeLink = NEW (NULLitem, NULL, NULL, 0);

    emptyTree = rootNodeLink; }

  • 7/23/2019 Comp1927 Qs

    14/22

    . 0mplement a non7recursi!e !ersion of the select function show +elow

    Item selectR (link currentTree, int k) { if (currentTree

    == emptyTree) { return NULLitem; } if

    (currentTree->left->size == k) { return (currentTree-

    >item); } if (currentTree->left->size > k){ return (selectR (currentTree->left, k)); }

    return (selectR (currentTree->right, k - 1 - currentTree-

    >left->size)); } Item STselect (ST st,int k) { return

    (selectR (st->root, k)); }

    6. 0n each of the following uestions, start with the tree in the state showna+o!e. how the effect of e*ecuting each of the following operations onthe tree:

    a. t97Jright @ rotateight(t=)

    +. t& @ rotate$eft(t&)

    c. t& @ rotateight(t&)

    d. t& @ partition(t&,=)

    e. t& @ partition(t&,)

    f. t&7Jleft @ partition(t1,6)

    #he following uestions are e*tra uestions to do at home as practice for theprac e*am.

    *tra Fuestion 1Directed &rap$ D%

  • 7/23/2019 Comp1927 Qs

    15/22

    What order would the nodes +e !isited while performing a depth first searchstarting at node d. What a+out if we started at node g'

    *tra Fuestion Directed &rap$ (%

    What order would the nodes +e !isited while performing a +readth first searchstarting at node d. What a+out if we started at node g'

    *tra Fuestion 6&rap$ roperties

    0n the 1th Century, the ;russian town of Ionigs+erg (now Ialiningrad) wasfamous for the se!en +ridges connecting its two central islands with the +an

  • 7/23/2019 Comp1927 Qs

    16/22

    ks of the i!er ;regel, as shown in the diagram. Can you draw a path which crosses each +ridge e*actly once' 0f not, e*plain why.

    #utorial *ercises Week &

    *ercise 1(alanced Trees*plain how we could glo+ally +alance a +inary search tree using the partitionfunction.

    *ercise pla) Trees

    What is the difference +etween splay tree insertion and root insertion'

    0nsert items with the keys 1&, D, , B, 11

    o in an empty +inary search tree

    o +inary search tree with root insertion

    o a splay tree

    and draw the resulting trees.

    *plain the concept of 8amortisation8 in the conte*t of +inary search

    trees and splay trees.

    0nsert the same keys into a randomised +inary search tree 7 get

    someone to flip a coin each time a decision needs to +e made as towhether the node is inserted at the leaf or root of the gi!en su+tree.Heads can mean root, tails can mean leaf. 5ote: in the actualimplementation from lectures the pro+a+ility that it is inserted at theroot is appro* 1Kn and not 3ust &.= like a coin toss.

    *ercise 6*as$tables

    -raw a hash ta+le of si?e 16. 2se the hash function 4kL164 and insert thekeys: =, D, 6, &, 61, &, 6 and 1 into your ta+le (in that order) using thefollowing strategies for handling collisions.

    1. Chaining

    . $inear ;ro+ing

    6. -ou+le Hashing with a second hash function 4B 7 (kLB)4. (Why would asecond hash function such as 4kLB4 not +e !ery useful')

    0f there is any remaining time, use it to finish any uestions that you did notco!er in pre!ious weeks.

  • 7/23/2019 Comp1927 Qs

    17/22

    $a+ *ercises Week &BE+3ecti!es

    Work on assn

    2se a sym+ol ta+le A-#

    /inary earch tree programming

    AssessmentDeadline: 11:=Dpm #uesday 9th e+ruary &19

    Total arks: 6

    etup for #ask of the la+Change into your la+&B directory and run the following command:

    cp /home/cs1927/public_html/14x1/labs/lab07/files/* .

    5ote the 4.4 at the end of the command, meaning 4into the current directory4. 0fyou8!e done the a+o!e correctly, you should find the following files now in thedirectory:

    client.c a program to use and test your sym+ol ta+le A-#

    testMdata data to test your A-# with#.h A sym+ol ta+le A-# interface

    0tem.h An interface for an item.

    0temMstr.c An implementation of an item.

    #M+streeMla+.c A partially implemented +st that you must complete

    #ask1: Assignment 1 ark&.= la+ mark for implementing stage & of the assignment and +eing a+le topass the first dryrun test of the assignment

    &.= la+ mark for implementing stage 1 of the assignment and +eing a+le topass the second dryrun test of the assignment

    or this part of the la+ 7 use the normal gi!e set up for assn.

    #ask: ym+ol #a+les 7 arks

    1 ark Compile the client program and run it with testMdata as input:

    % gcc -Wall -Werror -g -o client client.c Item_str.c

    ST_bstree_lab.c % ./client < test_data

    #he implementation of partition is not correct 7 it doesn8t update the

    si?e information in the tree nodes. i* it (you ha!e to change the rotatefunctions to do so).

  • 7/23/2019 Comp1927 Qs

    18/22

    1 ark

    ou must write some white +o* tests to ensure the partition and rotatefunctions are working correctly. /e thorough.

    u+missionWhen you are happy with your work, please show it to your tutor to get itmarked. /efore you lea!e your la+, remem+er to su+mit your la+ !ia the gi!ecommand

    give cs1927 lab07 ST_bstree_lab.c client.c

    $a+ *ercises Week E+3ecti!es

    #o learn a+out +alanced trees

    #o practise tree implementations

    AssessmentDeadline: 11:=Dpm #uesday 11th e+ruary &19

    Total arks: 6

    elated Chapters of te*t+ookChapter 1. 7 16. edgewick

    Chapter 19 edgewick

    etting 2pet up a directory for this la+ under your cs1927/labsdirectory, change intothat directory, and run the following command:

    cp /home/cs1927/public_html/14x1/labs/lab08/files/* .

    ln -s

    /home/cs1927/public_html/14x1/labs/lab08/largeFiles/d

    ict3 dict3 ln -s

    /home/cs1927/public_html/14x1/labs/lab08/largeFiles/d

    ict4 dict4 ln -s

    /home/cs1927/public_html/14x1/labs/lab08/largeFiles/d

    ict5 dict5

    0f you8!e done the a+o!e correctly, you should now find the following files:

    #ree.h A +inary search tree interface

    #ree.c A +inary search tree implementation that needs to +e modified

    words.c a main program which loads words into the tree

  • 7/23/2019 Comp1927 Qs

    19/22

    0tem.h a simple 0tem definition. 0tems are arrays of characters

    akfile A simple makefile for the la+

    dict& a file containing 3ust 16 words

    dict1 a file containing 1BBB words

    dict a file containing B1&> words

    etup2ni* has a large dictionary of nglish words which a!aila+le to use for taskssuch as spell checking. #his dictionary is normally held in a file calledKusrKdictKwords which contains almost 1&&&&& words, including a large num+erof proper names (e.g. ohn, ydney), and also including possessi!e forms ofmost nouns (e.g. cat and cat8s). or spell7checking, we would scan a file ofte*t and check whether each word in the file also appeared in the dictionaryNany word that did not would +e treated as a spelling error. #his essentially

    means using the dictionary as a 4lookup ta+le4.

    /efore KusrKdictKwords can reasona+ly +e used as a lookup ta+le, it needs to+e put into a more useful form than a list of words (al+eit alpha+eticallyordered).Ene possi+ility is to load the dictionary into a +inary search tree anddo the searches on this data structure.

    #he difference +etween dict9 and dict= is that all of the proper names and allof the possessi!e forms ha!e +een remo!ed. Apart from dict&, the otherdictionaries are randomly7chosen su+sets of dict9. All of the files are sorted inalpha+etical order. 5ote that we gi!e you links to the large dictionaries ratherthan copies of them +ecause they occupy a non7tri!ial amount of disk uota. 0fyou want to work on this e*ercise at home and use the large dictionaries,you8ll need to gra+ copies of them.

    We ha!e implemented a program, words.c that reads the dictionary words into#ree structures for fast lookup.

    #ask1: /alancing #rees 7 1 arks#he main program wordsreads from a user7specified file of words, and, oncethe words are loaded, prints out the num+er of nodes and the height of the

    tree used to store the words. 0t also prints out what kind of re+alancingstrategy was used.

    0t takes a command7line argument containing the name of the dictionary file to+e loaded along with an integer that represents what tree +alancing strategyshould +e used +y the tree. #he program works as follows:

    0t inserts all the words from the test file dictionary into a dictionary A-# that isimplemented !ia a tree. #he program then prints out properties of the resultingunderlying tree.

    ake the file and confirm that it runs using the dict& file and using +alancestrategy & (5o re+alancing) +y running it as follows

  • 7/23/2019 Comp1927 Qs

    20/22

    ./words dict0 0

    Ence you ha!e implemented /A$A5CM1 strategy you could also try theapproach where the tree has +een re+alanced after e!ery insertion +yrunning:

    ./words dict0 1

    Consider the following approaches for re+alancing a /#.

    1. 5EM/A$A5C : 5o re+alancing 7 normal /# insertion

    . /A$A5CM1 :

  • 7/23/2019 Comp1927 Qs

    21/22

    5ote: ake sure you run the randomised /# approach a num+er of times toget an a!erage (as this is a randomised approach and heights will differ8randomly8 each time).-iscuss the reasons for the differences in height and run times +etween thesedifferent approaches and algorithms. How does each algorithm influence the

    time to insert !s the time to search !s the height of the resulting tree' Whatother tests could you run to compare these implementations.

    #ask: our own re+alancing strategy 7 1 ark0n task 1 you e*perimented with different approaches to calling re+alance onthe tree. or e*ample re+alancing the tree after e!ery insertion !s re+alancingthe tree after e!ery 1&&& insertions.or this part of the la+, implement your own approach M/A$A5C andrerun the tests from a+o!e and compare your approach to the ones fromtask1.

    0n your approach you should decide upon some kind of strategy for when tore+alance the whole tree. 0t does not matter if it performs worse than the onesfrom task 1 as long as you can e*plain why. or e*ample, you could try andre+alance when the tree reached a certain le!el of im+alance 7 of course youwould ha!e to decide how to determine this measure. Er you can use anyother ideas of your own.

    ecord and discuss your results in /alanced#rees.t*t

    #ask6: earch in a splay tree 7 1 ark0n lectures, we discussed inserting an item into a splay tree (/alanced treee*amples). #he amortised cost of insert and search operations on splay treesis only !alid if we also perform splay rotations during searching for an item. Anpartial of implementation of splayeach has +een pro!ided in #ree.c to getyou started. 0ts prototype is as follows:

    link searchSplay (link n, Key k, int * found);

    0t searches for an item and performs splay rotations on the tra!ersed path,much like the function splay0nsert() does.5E#: Ence an item has +een found it ismo!ed to the root of tree. 0f the key

    does not e*ist in the tree, the last node on the search path should +e mo!edto the root.

    ou can look at insertplay to help you implement this function.

    5E#: /ecause the search function changes the root of the tree, it needs toreturn a pointer to the new root, thus the actual result of the search should +estored in the !aria+le pointed to +y found. #his should +e 1 if the key was inthe tree and & otherwise.

    #he treeearch function needs to +e modified so that it calls the appropriate

    !ersion of searching ,depending what +alancing strategy is used. ie it calls

  • 7/23/2019 Comp1927 Qs

    22/22

    splayearch function if the +alancetrategy is set to ;$A or the standardsearchecursi!e otherwise.

    #ry running the program on all dictionaries using splay insert and search,inserting all keys, printing out the height and then calling the searchAllWords

    function and printing out the height again and recording the times again.ecord the heights +efore and after searching. Compare the heights andtimes and discuss your results.

    u+missionWhen you are happy with your work, please show it to your tutor to get itmarked. /efore you lea!e your la+, remem+er to su+mit your la+ !ia the gi!ecommand

    give cs1927 lab08 Tree.c BalancedTrees.txt


Recommended