home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April B / Pcwk4b98.iso / Borland / Dbase50w / SAMPLES1.PAK / BINTREE.PRG next >
Text File  |  1994-08-02  |  3KB  |  114 lines

  1. *******************************************************************************
  2. *  PROGRAM:      Bintree.prg
  3. *
  4. *  WRITTEN BY:   Borland Samples Group
  5. *
  6. *  DATE:         5/93
  7. *
  8. *  UPDATED:      3/94
  9. *
  10. *  VERSION:      dBASE FOR WINDOWS 5.0
  11. *
  12. *  DESCRIPTION:  This program illustrates the creation and traversal of a
  13. *                binary tree using dBASE for Windows object() class.  It creates
  14. *                a binary tree of client names in increasing alphabetical order,
  15. *                left to right.  It then traverses the tree in that order.
  16. *
  17. *  PARAMETERS:   None
  18. *
  19. *  CALLS:        None
  20. *
  21. *  USAGE:        Do Bintree
  22. *
  23. *
  24. *
  25. *******************************************************************************
  26. create session
  27. set talk off
  28. set ldCheck off
  29.  
  30. use clients
  31.  
  32. private t
  33. t = new Tree(contact,client_Id)
  34. ? "Scanning database and building binary tree.."
  35. ?
  36.  
  37. skip
  38. scan rest
  39.   ?? '.'
  40.   t.AddTree(contact,client_Id)
  41. endscan
  42.  
  43.  
  44.  
  45.  
  46. wait "Press any key to begin in-order traversal of binary tree..."
  47.  
  48. t.InOrder()
  49. wait
  50.  
  51. *******************************************************************************
  52. *******************************************************************************
  53. class Tree(newKey,newValue)
  54. *******************************************************************************
  55.  
  56.  
  57. * Constructor
  58.  
  59. this.key = newKey
  60. this.val = newValue
  61. this.left = .f.
  62. this.right = .f.
  63.  
  64. *******************************************************************************
  65. function InOrder
  66. *******************************************************************************
  67.  
  68. * left
  69. if .not. empty(this.left)
  70.    this.left.InOrder()
  71. endif
  72.  
  73. * itself
  74. ?  this.key, this.val
  75.  
  76. * right
  77. if .not. empty(this.right)
  78.    this.right.InOrder()
  79. endif
  80. return .t.
  81.  
  82. *******************************************************************************
  83. function AddTree(newKey,newValue)
  84. *******************************************************************************
  85.  
  86. this.Insert(new Tree(newKey, newValue))
  87. return .t.
  88.  
  89. *******************************************************************************
  90. function Insert(o)
  91. *******************************************************************************
  92.  
  93. if o.key < this.key
  94.  
  95.    if (empty(this.left))
  96.       this.left = o
  97.       return(o)
  98.    else
  99.       return(this.left.Insert(o))
  100.    endif
  101. else
  102.    if (empty(this.right))
  103.       this.right = o
  104.       return(o)
  105.    else
  106.       return(this.right.Insert(o))
  107.    endif
  108. endif
  109. return .t.
  110.  
  111. endclass
  112.  
  113.  
  114.