home *** CD-ROM | disk | FTP | other *** search
- *******************************************************************************
- * PROGRAM: Bintree.prg
- *
- * WRITTEN BY: Borland Samples Group
- *
- * DATE: 5/93
- *
- * UPDATED: 3/94
- *
- * VERSION: dBASE FOR WINDOWS 5.0
- *
- * DESCRIPTION: This program illustrates the creation and traversal of a
- * binary tree using dBASE for Windows object() class. It creates
- * a binary tree of client names in increasing alphabetical order,
- * left to right. It then traverses the tree in that order.
- *
- * PARAMETERS: None
- *
- * CALLS: None
- *
- * USAGE: Do Bintree
- *
- *
- *
- *******************************************************************************
- create session
- set talk off
- set ldCheck off
-
- use clients
-
- private t
- t = new Tree(contact,client_Id)
- ? "Scanning database and building binary tree.."
- ?
-
- skip
- scan rest
- ?? '.'
- t.AddTree(contact,client_Id)
- endscan
-
-
-
-
- wait "Press any key to begin in-order traversal of binary tree..."
-
- t.InOrder()
- wait
-
- *******************************************************************************
- *******************************************************************************
- class Tree(newKey,newValue)
- *******************************************************************************
-
-
- * Constructor
-
- this.key = newKey
- this.val = newValue
- this.left = .f.
- this.right = .f.
-
- *******************************************************************************
- function InOrder
- *******************************************************************************
-
- * left
- if .not. empty(this.left)
- this.left.InOrder()
- endif
-
- * itself
- ? this.key, this.val
-
- * right
- if .not. empty(this.right)
- this.right.InOrder()
- endif
- return .t.
-
- *******************************************************************************
- function AddTree(newKey,newValue)
- *******************************************************************************
-
- this.Insert(new Tree(newKey, newValue))
- return .t.
-
- *******************************************************************************
- function Insert(o)
- *******************************************************************************
-
- if o.key < this.key
-
- if (empty(this.left))
- this.left = o
- return(o)
- else
- return(this.left.Insert(o))
- endif
- else
- if (empty(this.right))
- this.right = o
- return(o)
- else
- return(this.right.Insert(o))
- endif
- endif
- return .t.
-
- endclass
-
-
-