home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol085 / delete.pas < prev    next >
Pascal/Delphi Source File  |  1984-04-29  |  1KB  |  52 lines

  1. External btree::delete(1);
  2.  
  3. PROCEDURE DeleteLeftMost( VAR Employee : apointer;
  4.               VAR DeleteName : shorty );
  5. { delete the leftmost node in the tree and }
  6. {  returns its value in DeleteName       }
  7. BEGIN
  8.   IF Employee^.Left <> NIL THEN
  9.     DeleteLeftMost( Employee^.Left, DeleteName )
  10.   ELSE BEGIN
  11.     DeleteName := Employee^.details.Name;
  12.     Employee := Employee^.right
  13.   END
  14. END{of DeleteLeftMost};
  15.  
  16.  
  17. PROCEDURE DeleteRoot( VAR Employee : apointer );
  18. { deletes the root of the nonempty tree by replacing it  }
  19. { by its successor (or child) if it has only one, or     }
  20. { replacing its value by that of the leftmost descendant }
  21. { of the rightmost subtree.                 }
  22. VAR
  23.   DeletedName : shorty;
  24. BEGIN
  25.   IF Employee^.Left = NIL THEN
  26.     Employee := Employee^.right
  27.   ELSE IF Employee^.right = NIL THEN
  28.     Employee := Employee^.Left
  29.   ELSE BEGIN
  30.     DeleteLeftMost( Employee^.right, DeletedName );
  31.     Employee^.details.Name := DeletedName
  32.   END
  33. END{of DeleteRoot};
  34.  
  35.  
  36. PROCEDURE Delete( VAR Employee : apointer;
  37.               key : shorty );
  38. { delete key from the tree--if it is not }
  39. { in the tree, do nothing          }
  40. BEGIN
  41.   IF Employee = NIL THEN
  42.     WRITELN ( bell, key, ' not in data file' )
  43.   ELSE IF key = Employee^.details.Name THEN
  44.     DeleteRoot( Employee )
  45.   ELSE IF key < Employee^.details.Name THEN
  46.     Delete(Employee^.Left, key )
  47.   ELSE IF key > Employee^.details.Name THEN
  48.     Delete( Employee^.right, key )
  49. END;
  50.  
  51.  .
  52.