Problema la arbori binari

Pascal este un limbaj imperativ, creat inițial pentru a ajuta la predarea noțiunilor de programare structurati studenților. Delphi, urmașul lui Pascal, este un mediu de dezvoltare pentru aplicații Windows. Delphi este primul limbaj de programare (Object Pascal, mai exact) care a îmbinat ușurința în programare a limbajelor de nivel înalt și puterea uneltelor de lucru de nivel scăzut într-un mediu RAD.

Problema la arbori binari

Postby xlad » 28 Mar 2010, 00:02

De ceva vreme tot incerc sa implementez o metoda de memorare a unor date ordonate dupa un anumit criteriu si incerc sa le salvez intr-un arbore binar de cautare, dar care sa fie cat mai rapid (la insertie,stergere si cautare).

Am cautat diverse astfel de metode si cea mai buna mi s-a parut AVL Tree ( http://people.ksp.sk/~kuko/bak/big/ ) si am si gasit un exemplu foarte bun, insa are o problema. :((

http://www.filefront.com/15949169/0315c.txt/

La acel cod sursa merge foate bine inserarea elementelor in arbore, dar can vreau sa sterg un element nu este sters cum trebuie (fie sterge si descendentii lui, fie da access violation error)

As fi recunoscator daca cineva m-ar ajuta sa corectez functia de stergere, astfel incat sa stearga doar elementul dorit, nu si celelalte elemente care se afla sub el si fara sa aiba buguri (access violation).



De exemplu in arborele de mai jos, daca ii dau sa stearga elementul 7 imi da eroare memory access violation, sau daca sterg 2 imi sterge si 1 si 3.


  1.  
  2.  
  3.        4
  4.      /   \
  5.     2     6
  6.    / \   /  \
  7.   1  3   5   7
  8.               \
  9.                9
  10.  
  11.  
0,0p / 0 votes
User avatar
xlad
Bit
 
Joined: 06 Jan 2010
Status: 2

Re: Problema la arbori binari

Postby DarkByte » 28 Mar 2010, 00:48

Posteaza si sursa ta, te rog.
0,0p / 0 votes
User avatar
DarkByte
11011011
 
Joined: 29 Dec 2009
Status: 136

Re: Problema la arbori binari

Postby xlad » 28 Mar 2010, 11:16

AVL.pas
  1.  
  2. Unit AVL ;
  3.  
  4.   Interface
  5.  
  6.   Const
  7.     Max_Tree_Nodes = 500 ;  { Constant is significant only if an ordered }
  8.                             { array of the AVL tree nodes is desired.    }
  9.  
  10.   Type
  11.     AVLTreeStr = String[12] ;
  12.  
  13.     BalanceSet = (Left_Tilt , Neutral , Right_Tilt) ;
  14.  
  15.     AVLDataRec = Record
  16.                    Key    : AVLTreeStr ;
  17. {                   RecOfs : LongInt    ;
  18.                    RecNum : Word       ; }
  19.                  End ;
  20.  
  21.  
  22.     AVLPtr = ^AVL_Tree_Rec ;
  23.     AVL_Tree_Rec = Record
  24.                      TreeData : AVLDataRec ;
  25.                      Balance  : BalanceSet ;
  26.                      Left  ,
  27.                      Right    : AVLPtr     ;
  28.                    End ;
  29.  
  30.     TreeRecArray = Array[1..Max_Tree_Nodes] of AVLDataRec ;
  31. {.PA}
  32.     Procedure Insert_AVLTree(Var Root : AVLPtr     ;
  33.                                  X    : AVLDataRec  ) ;
  34.  
  35.     Function Search_AVLTree(Var Root       : AVLPtr     ;
  36.                                 X          : AVLDataRec  ) : Boolean ;
  37.  
  38.     Function Search(    Root : AVLPtr     ;
  39.                     Var X    : AVLDataRec  ) : Boolean ;
  40.  
  41.     Procedure AVLSort_To_Array(Var Root  : AVLPtr       ;
  42.                                Var SortX : TreeRecArray ;
  43.                                Var Count : Word          ) ;
  44.  
  45.     Function Find_AVLNode(Var Root : AVLPtr     ;
  46.                               X    : AVLDataRec  ) : AVLPtr ;
  47.  
  48.     Procedure Delete_AVLTree(Var Root  : AVLPtr     ;
  49.                                  X     : AVLDataRec ;
  50.                              Var DelOK : Boolean     ) ;
  51.  
  52.  
  53.   Implementation
  54. {.PA}
  55. {***********************************************************************
  56. *                                                                      *
  57. *      Rotate_Right                                                    *
  58. *                                                                      *
  59. *      Re-arranges tree nodes by rotating them to the right.           *
  60. *                                                                      *
  61. *      Modifications                                                   *
  62. *      =============                                                   *
  63. *                                                                      *
  64. ***********************************************************************}
  65.  
  66.     Procedure Rotate_Right(Var Root : AVLPtr) ;
  67.       Var
  68.         Ptr2 ,
  69.         Ptr3   : AVLPtr ;
  70.  
  71.       Begin  { Rotate_Right }
  72.         Ptr2 := Root^.Right ;
  73.         If Ptr2^.Balance = Right_Tilt then
  74.           Begin
  75.             Root^.Right   := Ptr2^.Left ;
  76.             Ptr2^.Left    := Root       ;
  77.             Root^.Balance := Neutral    ;
  78.             Root          := Ptr2       ;
  79.           End
  80.         Else
  81.           Begin
  82.             Ptr3        := Ptr2^.Left  ;
  83.             Ptr2^.Left  := Ptr3^.Right ;
  84.             Ptr3^.Right := Ptr2        ;
  85.             Root^.Right := Ptr3^.Left  ;
  86.             Ptr3^.Left  := Root        ;
  87.  
  88.             If Ptr3^.Balance = Left_Tilt then
  89.               Ptr2^.Balance := Right_Tilt
  90.             Else
  91.               Ptr2^.Balance := Neutral ;
  92.  
  93.             If Ptr3^.Balance = Right_Tilt then
  94.               Root^.Balance := Left_Tilt
  95.             Else
  96.               Root^.Balance := Neutral ;
  97.  
  98.             Root := Ptr3 ;
  99.           End ;
  100.  
  101.         Root^.Balance := Neutral ;
  102.  
  103.       End ;  { Rotate_Right }
  104. {.PA}
  105. {***********************************************************************
  106. *                                                                      *
  107. *      Rotate_Left                                                     *
  108. *                                                                      *
  109. *      Re-arranges tree nodes by rotating them to the left.            *
  110. *                                                                      *
  111. *      Modifications                                                   *
  112. *      =============                                                   *
  113. *                                                                      *
  114. ***********************************************************************}
  115.  
  116.     Procedure Rotate_Left(Var Root : AVLPtr) ;
  117.       Var
  118.         Ptr2 ,
  119.         Ptr3   : AVLPtr ;
  120.  
  121.       Begin  { Rotate_Left }
  122.         Ptr2 := Root^.Left ;
  123.         If Ptr2^.Balance = Left_Tilt then
  124.           Begin
  125.             Root^.Left    := Ptr2^.Right;
  126.             Ptr2^.Right   := Root       ;
  127.             Root^.Balance := Neutral    ;
  128.             Root          := Ptr2       ;
  129.           End
  130.         Else
  131.           Begin
  132.             Ptr3        := Ptr2^.Right ;
  133.             Ptr2^.Right := Ptr3^.Left  ;
  134.             Ptr3^.Left  := Ptr2        ;
  135.             Root^.Left  := Ptr3^.Right ;
  136.             Ptr3^.Right := Root        ;
  137.  
  138.             If Ptr3^.Balance = Right_Tilt then
  139.               Ptr2^.Balance := Left_Tilt
  140.             Else
  141.               Ptr2^.Balance := Neutral ;
  142.  
  143.             If Ptr3^.Balance = Left_Tilt then
  144.               Root^.Balance := Right_Tilt
  145.             Else
  146.               Root^.Balance := Neutral ;
  147.  
  148.             Root := Ptr3 ;
  149.           End ;
  150.  
  151.         Root^.Balance := Neutral ;
  152.  
  153.       End ;  { Rotate_Left }
  154. {.PA}
  155. {***********************************************************************
  156. *                                                                      *
  157. *      Insert_AVL                                                      *
  158. *                                                                      *
  159. *      Workhouse routine to perform node insertion in an AVL tree.     *
  160. *                                                                      *
  161. *      Modifications                                                   *
  162. *      =============                                                   *
  163. *                                                                      *
  164. ***********************************************************************}
  165.  
  166.     Procedure Insert_AVL(Var Root       : AVLPtr     ;
  167.                              X          : AVLDataRec ;
  168.                          Var InsertedOK : Boolean     ) ;
  169.       Begin  { Insert_AVL }
  170.         If Root = Nil then
  171.           Begin
  172.             New(Root) ;
  173.             With Root^ do
  174.               Begin
  175.                 TreeData := X       ;
  176.                 Left     := Nil     ;
  177.                 Right    := Nil     ;
  178.                 Balance  := Neutral ;
  179.               End ;
  180.  
  181.             InsertedOK := True ;
  182.           End
  183.         Else
  184.           If X.Key = Root^.TreeData.Key then
  185.             Begin
  186.               InsertedOK := False ;
  187.               Exit ;
  188.             End
  189.           Else
  190.             If X.Key <= Root^.TreeData.Key then
  191.               Begin
  192.                 Insert_AVL(Root^.Left , X , InsertedOK) ;
  193.                 If InsertedOK then
  194.                   Case Root^.Balance of
  195.                     Left_Tilt  : Begin
  196.                                    Rotate_Left(Root) ;
  197.                                    InsertedOK := False ;
  198.                                  End ;
  199.  
  200.                     Neutral    : Root^.Balance := Left_Tilt ;
  201.  
  202.                     Right_Tilt : Begin
  203.                                    Root^.Balance := Neutral ;
  204.                                    InsertedOK    := False   ;
  205.                                  End ;
  206.                   End ; { Case Root^.Balance of }
  207.               End
  208.             Else
  209.               Begin
  210.                 Insert_AVL(Root^.Right , X , InsertedOK) ;
  211.                 If InsertedOK then
  212.                   Case Root^.Balance of
  213.                     Left_Tilt  : Begin
  214.                                    Root^.Balance := Neutral ;
  215.                                    InsertedOk    := False   ;
  216.                                  End ;
  217.  
  218.                     Neutral    : Root^.Balance := Right_Tilt ;
  219.  
  220.                     Right_Tilt : Begin
  221.                                    Rotate_Right(Root) ;
  222.                                    InsertedOK := False ;
  223.                                  End ;
  224.                   End ;  { Case Root^.Balance of }
  225.               End ;
  226.  
  227.       End ;  { Insert_AVL }
  228. {.PA}
  229. {***********************************************************************
  230. *                                                                      *
  231. *      Insert_AVLTree                                                  *
  232. *                                                                      *
  233. *      Insert a datum into the AVL tree.                               *
  234. *                                                                      *
  235. *      Modifications                                                   *
  236. *      =============                                                   *
  237. *                                                                      *
  238. ***********************************************************************}
  239.  
  240.     Procedure Insert_AVLTree(Var Root : AVLPtr     ;
  241.                                  X    : AVLDataRec  ) ;
  242.       Var
  243.         InsertedOK : Boolean ;
  244.  
  245.       Begin  { Insert_AVLTree }
  246.         InsertedOK := False ;
  247.         Insert_AVL(Root , X , InsertedOK) ;
  248.       End ;  { Insert_AVLTree }
  249.  
  250.  
  251. {***********************************************************************
  252. *                                                                      *
  253. *      Search                                                          *
  254. *                                                                      *
  255. *      Search for datum in the AVL tree.  This interface routine is    *
  256. *      needed because of the recursion involved in Search_AVLTree.     *
  257. *                                                                      *
  258. *      Modifications                                                   *
  259. *      =============                                                   *
  260. *                                                                      *
  261. ***********************************************************************}
  262.  
  263.     Function Search(    Root : AVLPtr     ;
  264.                     Var X    : AVLDataRec  ) : Boolean ;
  265.       Begin
  266.         If Search_AVLTree(Root , X) then
  267.           Begin
  268.             Move(Root^ , X , SizeOf(AVLDataRec)) ;
  269.             Search := True ;
  270.           End
  271.         Else
  272.           Search := False ;
  273.       End ;
  274.  
  275.  
  276. {***********************************************************************
  277. *                                                                      *
  278. *      Search_AVLTree                                                  *
  279. *                                                                      *
  280. *      Search for datum in the AVL tree.                               *
  281. *                                                                      *
  282. *      Modifications                                                   *
  283. *      =============                                                   *
  284. *                                                                      *
  285. ***********************************************************************}
  286.  
  287.     Function Search_AVLTree(Var Root       : AVLPtr     ;
  288.                                 X          : AVLDataRec  ) : Boolean ;
  289.       Begin  { Search_AVLTree }
  290.         Search_AVLTree := False ;
  291.  
  292.         While Root <> Nil do
  293.           Begin
  294.             If X.Key > Root^.TreeData.Key then
  295.               Root := Root^.Right
  296.             Else
  297.               Begin
  298.                 If X.Key < Root^.TreeData.Key then
  299.                   Root := Root^.Left
  300.                 Else
  301.                   {                         }
  302.                   { A match has been found. }
  303.                   {                         }
  304.                   Begin
  305.                     Search_AVLTree := True ;
  306.                     Exit ;
  307.                   End ;
  308.               End ;
  309.           End ;
  310.       End ;  { Search_AVLTree }
  311.  
  312.  
  313. {***********************************************************************
  314. *                                                                      *
  315. *      Traverse_Tree                                                   *
  316. *                                                                      *
  317. *        Local recursive routine used to traverse the AVL tree.        *
  318. *                                                                      *
  319. *      Modifications                                                   *
  320. *      =============                                                   *
  321. *                                                                      *
  322. ***********************************************************************}
  323.  
  324.     PROCEDURE Traverse_Tree(Var Root  : AVLPtr       ;
  325.                             Var SortX : TreeRecArray ;
  326.                             Var Count : Word          ) ;
  327.       Begin  { Traverse_Tree }
  328.         If Root <> Nil then
  329.           Begin
  330.             Traverse_Tree(Root^.Left , SortX , Count) ;
  331.             Inc(Count) ;
  332.             If Count <= Max_Tree_Nodes then
  333.               SortX[Count].Key := Root^.TreeData.Key ;
  334.             Traverse_Tree(Root^.Right , SortX , Count) ;
  335.           End ;
  336.       End ;  { Traverse_Tree }
  337.  
  338.  
  339. {***********************************************************************
  340. *                                                                      *
  341. *      AVLSort_To_Array                                                *
  342. *                                                                      *
  343. *      Return the tree data in a sorted vector (array).                *
  344. *                                                                      *
  345. *      Modifications                                                   *
  346. *      =============                                                   *
  347. *                                                                      *
  348. ***********************************************************************}
  349.  
  350.     Procedure AVLSort_To_Array(Var Root  : AVLPtr       ;
  351.                                Var SortX : TreeRecArray ;
  352.                                Var Count : Word          ) ;
  353.       Begin  { AVLSort_To_Array }
  354.         Count := 0 ;  { Initialize number of array members. }
  355.  
  356.         { In-order traverse of the tree. }
  357.         Traverse_Tree(Root , SortX , Count) ;
  358.       End ;  { AVLSort_To_Array }
  359. {.PA}
  360. {***********************************************************************
  361. *                                                                      *
  362. *      Find_AVLNode                                                    *
  363. *                                                                      *
  364. *      Return the pointer to a node in the AVL tree for a requested    *
  365. *      datum.                                                          *
  366. *                                                                      *
  367. *      Modifications                                                   *
  368. *      =============                                                   *
  369. *                                                                      *
  370. ***********************************************************************}
  371.  
  372.     Function Find_AVLNode(Var Root : AVLPtr     ;
  373.                               X    : AVLDataRec  ) : AVLPtr ;
  374.       Var
  375.         No_Match : Boolean ;
  376.  
  377.       Begin  { Find_AVLNode }
  378.         No_Match := True ;
  379.  
  380.         While (Root <> Nil) and No_Match do
  381.           If X.Key > Root^.TreeData.Key then
  382.             Root := Root^.Right
  383.           Else
  384.             If X.Key < Root^.TreeData.Key then
  385.               Root := Root^.Left
  386.             Else
  387.               No_Match := False ;
  388.  
  389.         Find_AVLNode := Root ;
  390.       End ;  { Find_AVLNode }
  391. {.PA}
  392. {***********************************************************************
  393. *                                                                      *
  394. *      Balance_Right                                                   *
  395. *                                                                      *
  396. *      Restores the balanced/near balanced state of an AVL tree by     *
  397. *      rebalancing a right subtree.                                    *
  398. *                                                                      *
  399. *      Modifications                                                   *
  400. *      =============                                                   *
  401. *                                                                      *
  402. ***********************************************************************}
  403.  
  404.     Procedure Balance_Right(Var Root  : AVLPtr  ;
  405.                             Var DelOK : Boolean  ) ;
  406.       Var
  407.         Ptr2 ,
  408.         Ptr3   : AVLPtr ;
  409.  
  410.         Balnc2 ,
  411.         Balnc3   : BalanceSet ;
  412.  
  413.       Begin  { Balance_Right }
  414.         Case Root^.Balance of
  415.           Left_Tilt  : Root^.Balance := Neutral ;
  416.  
  417.           Neutral    : Begin
  418.                          Root^.Balance := Right_Tilt ;
  419.                          DelOk := False ;
  420.                        End ;
  421.  
  422.           Right_Tilt : Begin
  423.                          Ptr2 := Root^.Right ;
  424.                          Balnc2 := Ptr2^.Balance ;
  425.                          If not (Balnc2 = Left_Tilt) then
  426.                            Begin
  427.                              Root^.Right := Ptr2^.Left ;
  428.                              Ptr2^.Left := Root ;
  429.                              If Balnc2 = Neutral then
  430.                                Begin
  431.                                  Root^.Balance := Right_Tilt ;
  432.                                  Ptr2^.Balance := Left_Tilt  ;
  433.                                  DelOk := False ;
  434.                                End
  435.                              Else
  436.                                Begin
  437.                                  Root^.Balance := Neutral ;
  438.                                  Ptr2^.Balance := Neutral ;
  439.                                End ;
  440.  
  441.                              Root := Ptr2 ;
  442.                            End
  443.                          Else
  444.                            Begin
  445.                              Ptr3        := Ptr2^.Left    ;
  446.                              Balnc3      := Ptr3^.Balance ;
  447.                              Ptr2^.Left  := Ptr3^.Right   ;
  448.                              Ptr3^.Right := Ptr2          ;
  449.                              Root^.Right := Ptr3^.Left    ;
  450.                              Ptr3^.Left  := Root          ;
  451.  
  452.                              If Balnc3 = Left_Tilt then
  453.                                Ptr2^.Balance := Right_Tilt
  454.                              Else
  455.                                Ptr2^.Balance := Neutral ;
  456.  
  457.                              If Balnc3 = Right_Tilt then
  458.                                Root^.Balance := Left_Tilt
  459.                              Else
  460.                                Root^.Balance := Neutral ;
  461.                              Root := Ptr3 ;
  462.                              Ptr3^.Balance := Neutral ;
  463.                            End ;
  464.                        End ;
  465.         End ;  { CAse Root^.Balance of }
  466.       End ;  { Balance_Right }
  467. {.PA}
  468. {***********************************************************************
  469. *                                                                      *
  470. *      Balance_Left                                                    *
  471. *                                                                      *
  472. *      Restores the balanced/near balanced state of an AVL tree by     *
  473. *      rebalancing a left subtree.                                     *
  474. *                                                                      *
  475. *      Modifications                                                   *
  476. *      =============                                                   *
  477. *                                                                      *
  478. ***********************************************************************}
  479.  
  480.     Procedure Balance_Left(Var Root  : AVLPtr  ;
  481.                            Var DelOK : Boolean  ) ;
  482.       Var
  483.         Ptr2 ,
  484.         Ptr3   : AVLPtr ;
  485.  
  486.         Balnc2 ,
  487.         Balnc3   : BalanceSet ;
  488.  
  489.       Begin  { Balance_Left }
  490.         Case Root^.Balance of
  491.           Left_Tilt  : Root^.Balance := Neutral ;
  492.  
  493.           Neutral    : Begin
  494.                          Root^.Balance := Left_Tilt ;
  495.                          DelOk := False ;
  496.                        End ;
  497.  
  498.           Right_Tilt : Begin  { Right_Tilt }
  499.                          Ptr2 := Root^.Left ;
  500.                          Balnc2 := Ptr2^.Balance ;
  501.                          If not (Balnc2 = Right_Tilt) then
  502.                            Begin
  503.                              Root^.Left  := Ptr2^.Right ;
  504.                              Ptr2^.Right := Root        ;
  505.                              If Balnc2 = Neutral then
  506.                                Begin
  507.                                  Root^.Balance := Left_Tilt  ;
  508.                                  Ptr2^.Balance := Right_Tilt ;
  509.                                  DelOk := False ;
  510.                                End
  511.                              Else
  512.                                Begin
  513.                                  Root^.Balance := Neutral ;
  514.                                  Ptr2^.Balance := Neutral ;
  515.                                End ;
  516.  
  517.                              Root := Ptr2 ;
  518.                            End
  519.                          Else
  520.                            Begin
  521.                              Ptr3        := Ptr2^.Right   ;
  522.                              Balnc3      := Ptr3^.Balance ;
  523.                              Ptr2^.Right := Ptr3^.Left    ;
  524.                              Ptr3^.Left  := Ptr2          ;
  525.                              Root^.Left  := Ptr3^.Right   ;
  526.                              Ptr3^.Right := Root          ;
  527.  
  528.                              If Balnc3 = Right_Tilt then
  529.                                Ptr2^.Balance := Left_Tilt
  530.                              Else
  531.                                Ptr2^.Balance := Neutral ;
  532.  
  533.                              If Balnc3 = Left_Tilt then
  534.                                Root^.Balance := Right_Tilt
  535.                              Else
  536.                                Root^.Balance := Neutral ;
  537.  
  538.                              Root := Ptr3 ;
  539.                              Ptr3^.Balance := Neutral ;
  540.                            End ;
  541.                        End ;  { Right_Tilt }
  542.         End ;  { Case Root^.Balance of }
  543.       End ;  { Balance_Left }
  544. {.PA}
  545. {***********************************************************************
  546. *                                                                      *
  547. *      Delete_Both_Children                                            *
  548. *                                                                      *
  549. *        Delete a node with two empty subtrees.                        *
  550. *                                                                      *
  551. *      Modifications                                                   *
  552. *      =============                                                   *
  553. *                                                                      *
  554. ***********************************************************************}
  555.  
  556.   Procedure Delete_Both_Children(Var Root ,
  557.                                      Ptr    : AVLPtr  ;
  558.                                  Var DelOK  : Boolean  ) ;
  559.     Begin  { Delete_Both_Children }
  560.       If Ptr^.Right = Nil then
  561.         Begin
  562.           Root^.TreeData := Ptr^.TreeData ;
  563.           Ptr   := Ptr^.Left ;
  564.           DelOk := True ;
  565.         End
  566.       Else
  567.         Begin
  568.           Delete_Both_Children(Root , Ptr^.Right , DelOK) ;
  569.           If DelOk then
  570.             Balance_Left(Ptr , DelOK) ;
  571.         End ;
  572.     End ;  { Delete_Both_Children }
  573. {.PA}
  574. {***********************************************************************
  575. *                                                                      *
  576. *      Delete_AVL                                                      *
  577. *                                                                      *
  578. *        Recursive routine used for node deletion.                     *
  579. *                                                                      *
  580. *      Modifications                                                   *
  581. *      =============                                                   *
  582. *                                                                      *
  583. ***********************************************************************}
  584.  
  585.     Procedure Delete_AVL(Var Root  : AVLPtr     ;
  586.                              X     : AVLDataRec ;
  587.                          Var DelOK : Boolean     ) ;
  588.       Var
  589.         Ptr : AVLPtr ;
  590.  
  591.       Begin  { Delete_AVL }
  592.         If Root = Nil then
  593.           DelOK := False
  594.         Else
  595.           If X.Key < Root^.TreeData.Key then
  596.             Begin
  597.               Delete_AVL(Root^.Left , X , DelOK) ;
  598.               If DelOK then
  599.                 Balance_Right(Root , DelOK) ;
  600.             End
  601.           Else
  602.             If X.Key > Root^.TreeData.Key then
  603.               Begin
  604.                 Delete_AVL(Root^.Right , X , DelOK) ;
  605.                 If DelOK then
  606.                   Balance_Left(Root , DelOK) ;
  607.               End
  608.             Else
  609.               Begin
  610.                 Ptr := Root ;
  611.                 If Root^.Right = Nil then
  612.                   Begin
  613.                     Root := Root^.Left ;
  614.                     DelOK := True ;
  615.                   End
  616.                 Else
  617.                   Begin
  618.                     Delete_Both_Children(Root , Root^.Left , DelOK) ;
  619.                     If DelOK then
  620.                       Balance_Right(Root , DelOK) ;
  621.                   End ;
  622.               End ;
  623.  
  624.               Dispose(Ptr) ;
  625.       End ;  { Delete_AVL }
  626. {.PA}
  627. {***********************************************************************
  628. *                                                                      *
  629. *      Delete_AVLTree                                                  *
  630. *                                                                      *
  631. *      Deletes the key of X if it is present in the AVL tree.          *
  632. *                                                                      *
  633. *      Modifications                                                   *
  634. *      =============                                                   *
  635. *                                                                      *
  636. ***********************************************************************}
  637.  
  638.     Procedure Delete_AVLTree(Var Root  : AVLPtr     ;
  639.                                  X     : AVLDataRec ;
  640.                              Var DelOK : Boolean     ) ;
  641.       Begin  { Delete_AVLTree }
  642.         DelOK := False ;
  643.  
  644.         Delete_AVL(Root , X , DelOK) ;
  645.       End ;  { Delete_AVLTree }
  646.  
  647.  
  648.   Begin  { AVL_Tree }
  649.   End.   { AVL_Tree }
  650.  


Si intr-o fereastra oarecare (unit1/form1)
  1.  
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. Var TempAVL:AVLPtr;
  4.     X:AVLDataRec;
  5.     b:Boolean;
  6. begin
  7.  TempAvl := Nil;
  8.  X.Key := '5';//Chiar daca am pus sa memoreze string-uri, se poate schimba usor implementarea sa salveze numere si alte tipuri
  9.  Insert_AVLTree(TempAvl,X);
  10.  X.Key := '6';
  11.  Insert_AVLTree(TempAvl,X);
  12.  X.Key := '7';
  13.  Insert_AVLTree(TempAvl,X);
  14.  X.Key := '3';
  15.  Insert_AVLTree(TempAvl,X);
  16.  X.Key := '4';
  17.  Insert_AVLTree(TempAvl,X);
  18.  X.Key := '2';
  19.  Insert_AVLTree(TempAvl,X);
  20.  X.Key := '9';
  21.  Insert_AVLTree(TempAvl,X);
  22.  X.Key := '1';
  23.  Insert_AVLTree(TempAvl,X);
  24.  X.Key := '7';
  25.  Delete_AVLTree(TempAvl,X,b);//Aici nu merge
  26.  X.Key := '2';
  27.  Delete_AVLTree(TempAvl,X,b);
  28.  X.Key := '5';
  29.  Delete_AVLTree(TempAvl,X,b);
  30.  X.Key := '3';
  31.  Delete_AVLTree(TempAvl,X,b);
  32. end;
  33.  
0,0p / 0 votes
User avatar
xlad
Bit
 
Joined: 06 Jan 2010
Status: 2


Return to Pascal / Delphi

Who is online

Users browsing this forum: Google [Bot] and 0 guests