Íàçàä | Ñîäåðæàíèå| Âïåð¸ä Ãëàâà 10 ÓÑÎÂÅÐØÅÍÑÒÂÎÂÀ...

Íàçàä | Ñîäåðæàíèå| Âïåð¸ä

Ãëàâà 10

ÓÑÎÂÅÐØÅÍÑÒÂÎÂÀÍÍÛÅ ÌÅÒÎÄÛ

ÏÐÅÄÑÒÀÂËÅÍÈß ÌÍÎÆÅÑÒ ÄÅÐÅÂÜßÌÈ

 äàííîé ãëàâå ìû ðàññìîòðèìóñîâåðøåíñòâîâàííûå ìåòîäû ïðåäñòàâëåíèÿìíîæåñòâ ïðè ïîìîùè äåðåâüåâ. Îñíîâíàÿ èäåÿñîñòîèò â òîì, ÷òîáû ïîääåðæèâàòüñáàëàíñèðîâàííîñòè èëè ïðèáëèæåííóþñáàëàíñèðîâàííîñòü äåðåâà, ñ òåì ÷òîáû èçáåæàòüâûðîæäåíèÿ åãî â ñïèñîê. Ìåõàíèçìû áàëàíñèðîâêèäåðåâüåâ ãàðàíòèðóþò, äàæå â õóäøåì ñëó÷àå,îòíîñèòåëüíî áûñòðûé äîñòóï ê ýëåìåíòàì äàííûõ,õðàíÿùèõñÿ â äåðåâå, ïðè ëîãàðèôìè÷åñêîì ïîðÿäêåâðåìåíè äîñòóïà.  ýòîé ãëàâå èçëîæåíî äâà òàêèõìåõàíèçìà: äâîè÷íî-òðîè÷íûå (êðàòêî, 2-3) äåðåâüÿ èAVL-äåðåâüÿ. (Äëÿ èçó÷åíèÿ îñòàëüíûõ ãëàâïîíèìàíèå äàííîé ãëàâû íå îáÿçàòåëüíî.)

10. 1.    Äâîè÷íî - òðîè÷íûå ñïðàâî÷íèêè

Äâîè÷íîå äåðåâî íàçûâàþò õîðîøîñáàëàíñèðîâàííûì, åñëè îáà åãî ïîääåðåâà èìåþòïðèìåðíî îäèíàêîâóþ ãëóáèíó (èëè ðàçìåð) è ñàìèñáàëàíñèðîâàíû. Ãëóáèíà ñáàëàíñèðîâàííîãîäåðåâà ïðèáëèæåííî ðàâíà log n , ãäå n -÷èñëî âåðøèí äåðåâà. Âðåìÿ, íåîáõîäèìîå äëÿâû÷èñëåíèé, ïðîèçâîäèìûõ îòíîøåíèÿìè âíóòðè,äîáàâèòü è óäàëèòü íàääâîè÷íûìè ñïðàâî÷íèêàìè, ïðîïîðöèîíàëüíîãëóáèíå äåðåâà. Òàêèì îáðàçîì, â ñëó÷àå äâîè÷íûõñïðàâî÷íèêîâ ýòî âðåìÿ èìååò ïîðÿäîê log n.Ëîãàðèôìè÷åñêèé ðîñò ñëîæíîñòè àëãîðèòìà,ïðîâåðÿþùåãî ïðèíàäëåæíîñòü ýëåìåíòà ìíîæåñòâó,- ýòî îïðåäåëåííîå äîñòèæåíèå ïî ñðàâíåíèþ ñîñïèñêîâûì ïðåäñòàâëåíèåì, ïîñêîëüêó â ïîñëåäíåìñëó÷àå ìû èìååì ëèíåéíûé ðîñò ñëîæíîñòè

Ðèñ. 10. 5.  Íåêîòîðûå èç ñëó÷àåâ ðàáîòû îòíîøåíèÿ âñòàâ.

(a)  âñòàâ( â2( Ä1, Ì, Ä2), X, â2( ÍÄ1, Ì, Ä2) );

(b)  âñòàâ( â2( Ä1, Ì, Ä2), X,

                        â3( ÍÄ1à, Ìá, ÍÄ1á, Ì, Ä2) );

(c)  âñòàâ( â3( Ä1, Ì2, Ä2, Ì3, Ä3), X,

                        â2( ÍÄ1à, Ìá, ÍÄ1á), Ì2, â2( Ä2, Ì3, Ä3) ).

% Âñòàâëåíèå ýëåìåíòà â 2-3 ñïðàâî÷íèê

        äîá23( Äåð, X, Äåð1) :-            %Âñòàâèòü Õ â Äåð, ïîëó÷èòü Äåð1

               âñòàâ( Äåð, X, Äåð1).        % Äåðåâî ðàñòåò âøèðü

        äîá23( Äåð, X, â2( Ä1, Ì2,Ä2) ) :-

               âñòàâ( Äåð, X, Ä1, Ì2, Ä2).        %Äåðåâî ðàñòåò âãëóáü

        äîá23( nil, X, ë( Õ) ).

        âñòàâ( ë( À), X, ë( À), X,ë( Õ) ) :-

               áîëüøå( X, À).

        âñòàâ( ë( À), X, ë( Õ), À,ë( À) ) :-

               áîëüøå( À, X).

        âñòàâ( â2( Ä1, Ì, Ä2), X,â2( ÍÄ1, Ì, Ä2) ) :-

               áîëüøå( Ì, X),

               âñòàâ( Ä1, X, ÍÄ1).

        âñòàâ( â2( Ä1, Ì, Ä2), Õ,â3( ÍÄ1à, Ìá, ÍÄ1á, Ì, Ä2) ) :-

               áîëüøå( Ì, X),

               âñòàâ( Ä1, X, ÍÄ1à, Ìá, ÍÄ1á).

        âñòàâ( â2( Ä1, Ì, Ä2), X,â2( Ä1, Ì, ÍÄ2) ) :-

               áîëüøå( X, Ì),

               âñòàâ( Ä2, X, ÍÄ2).

        âñòàâ( â2( Ä1, Ì, Ä2), Õ,â3( Ä1, Ì, ÍÄ2à, Ìá, ÍÄ2á) ) :-

               áîëüøå( X, Ì),

               âñòàâ( Ä2, X, ÍÄ2à, Ìá, ÍÄ2á).

        âñòàâ( â3( Ä1, Ì2, Ä2, Ì3,Ä3), Õ, â3( ÍÄ1, Ì2, Ä2, Ì3, Ä3) :-

               áîëüøå( Ì2, X),

               âñòàâ( Ä1, X, ÍÄ1).

        âñòàâ( â3( Ä1, Ì2, Ä2, Ì3,Ä3), X,

               â2( ÍÄ1à, Ìá, ÍÄ1á), Ì2, â2( Ä2, Ì3, Ä3) ) :-

               áîëüøå( Ì2, X),

               âñòàâ( Ä1, X, ÍÄ1à, Ìá, ÍÄ1á).

        âñòàâ( â3( Ä1, Ì2, Ä2, Ì3,Ä3), X,

               â3( Ä1, Ì2, ÍÄ2, Ì3, Ä3) ) :-

               áîëüøå( X, Ì2), áîëüøå( Ì3, X),

               âñòàâ( Ä2, X, ÍÄ2).

        âñòàâ( â3( Ä1, Ì2, Ä2, Ì3,Ä3), X,

               â2( Ä1, Ì2, ÍÄ2à), Ìá, â2( ÍÄ2á, Ì3, Ä3) ) :-

               áîëüøå( X, Ì2), áîëüøå( Ì3, X),

               âñòàâ( Ä2, X, ÍÄ2à, Ìá, ÍÄ2á).

        âñòàâ( â3( Ä1, Ì2, Ä2, Ì3,Ä3), X,

               â3( Ä1, Ì2, Ä2, Ì3, ÍÄ3) ) :-

               áîëüøå( X, Ì3),

               âñòàâ( Ä3, X, ÍÄ3).

        âñòàâ( â3( Ä1, Ì2, Ä2, Ì3,Ä3), X,

               â2( Ä1, Ì2, Ä2), Ì3, â2( ÍÄ3à, Ìá, ÍÄ3á) ) :-

               áîëüøå( X, Ì3),

               âñòàâ( Ä3, X, ÍÄ3à, Ìá, ÍÄ3á).

Ðèñ. 10. 6.  Âñòàâëåíèåýëåìåíòà â 2-3 ñïðàâî÷íèê.  ýòîé

ïðîãðàììå ïðåäóñìîòðåíî, ÷òî ïîïûòêàïîâòîðíîãî

âñòàâëåíèÿ ýëåìåíòà òåðïèò íåóäà÷ó.

Ïðîãðàììà äëÿ âñòàâëåíèÿ íîâîãî ýëåìåíòà â 2-3ñïðàâî÷íèê ïîêàçàíà ïîëíîñòüþ íà ðèñ. 10.6. Íà ðèñ.10.7 ïîêàçàíà ïðîãðàììà âûâîäà íà ïå÷àòü 2-3äåðåâüåâ.

Íàøà ïðîãðàììà èíîãäà âûïîëíÿåò ëèøíèåâîçâðàòû. Òàê, åñëè âñòàâ ñ òðåìÿàðãóìåíòàìè òåðïèò íåóäà÷ó, òî âûçûâàåòñÿïðîöåäóðà âñòàâ ñ ïÿòüþ àðãóìåíòàìè,êîòîðàÿ ÷àñòü ðàáîòû äåëàåò ïîâòîðíî. Ìîæíîóñòðàíèòü èñòî÷íèê íåýôôåêòèâíîñòè, åñëè,íàïðèìåð, ïåðåîïðåäåëèòü âñòàâ êàê

        âñòàâ2( Äåð, X,Äåðåâüÿ)

ãäå Äåðåâüÿ - ñïèñîê, ñîñòîÿùèé ëèáî èçîäíîãî, ëèáî èç òðåõ àðãóìåíòîâ:

        Äåðåâüÿ = [ ÍîâÄåð], åñëèâñòàâ( Äåð, X, ÍîâÄåð)

        Äåðåâüÿ = [ ÍÄà, Ìá, ÍÄá],

        åñëè âñòàâ( Äåð, X, ÍÄà,Ìá, ÍÄá)

Òåïåðü îòíîøåíèå äîá23 ìîæíîïåðåîïðåäåëèòü òàê:

        äîá23( Ä, X, Ä1) :-

               âñòàâ( Ä, X, Äåðåâüÿ),

               ñîåäèíèòü( Äåðåâüÿ, Ä1).

Îòíîøåíèå ñîåäèíèòü ôîðìèðóåò îäíîäåðåâî Ä1 èç äåðåâüåâ, íàõîäÿùèõñÿ â ñïèñêå Äåðåâüÿ.

Óïðàæíåíèÿ

10. 1.    Îïðåäåëèòå îòíîøåíèå

        âíóòðè( Ýäåì, Äåð)

äëÿ ïîèñêà ýëåìåíòà Ýëåì â 2-3ñïðàâî÷íèêå Äåð.

Ïîñìîòðåòü îòâåò

10. 2.    Ââåäèòå â ïðîãðàììó ðèñ.10.6 èçìåíåíèÿ äëÿ óñòðàíåíèÿ ëèøíèõ âîçâðàòîâ(îïðåäåëèòå îòíîøåíèÿ âñòàâ2 è ñîåäèíèòü).

%  Îòîáðàæåíèå 2-3 ñïðàâî÷íèêîâ

        îòîáð(Ä) :-                                                                                                       15

            îòîáð( Ä, 0).                                                                                           --

        îòîáð( nil, _ ).                                                                                          15

        îòîáð( ë(À), Í) :-                                                                                      --

               tab( H), write( A), nl.                                                                             13

        îòîáð( â2( Ä1, Ì, Ä2), Í):-                                                                --

               H1 is H + 5                                                                                     13

               îòîáð( Ä2, H1),                                                                             --

               tab( H), write( --), nl,                                                                             12

               tab( H), write( M), nl,                                                                         --

               tab( H), write( --), nl,                                                                       12

               îòîáð( Ä1, H1).                                                                                       10

        îòîáð( â3( Ä1, M2, Ä2, Ì3,Ä3), H) :-                                                      10

               H1 is H + 5                                                                                           --

               îòîáð( Ä3, H1),                                                                                        8

               tab( H), write( --), nl,                                                    --

               tab( H), write( M3), nl,                                                  8

               îòîáð( Ä2, H1),                                                              --

               tab( H), write( M2), nl,                                                                             7

               tab( H), write( --), nl,                                                                         --

               îòîáð( Ä1, H1).                                                                                    7

                                                                                                                                --

                          (a)                                                                                                    5

                                                                                                                          --                                                                                                                            5

                                                                                                                          --

                                                                                                                                      4

                                                                                                                                --

                                                                                                                                4

                                                                                                                                      3

                                                                                                                                3

                                                                                                                                --

                                                                                                                                       1

                                                                                                     (á)

Ðèñ. 10. 7.    (à)  Ïðîãðàììà äëÿ îòîáðàæåíèÿ 2-3 ñïðàâî÷íèêà.

(b)   Ñïðàâî÷íèê (ñì. ðèñ. 10.2), îòïå÷àòàííûéýòîé ïðîãðàììîé.

Íàçàä | Ñîäåðæàíèå| Âïåð¸ä









Ãëàâíàÿ |  èçáðàííîå | Íàø E-MAIL | Äîáàâèòü ìàòåðèàë | Íàø¸ë îøèáêó | Íàâåðõ