User Tools

Site Tools


notes:data:bintree

binary tree class

This class currently implements a binary search tree (BST). Not all binary trees are BSTs, but this one is. That means that values (that is, numbers) are stored in the tree in a position based upon the relationship of their values to the values in the tree.

PATH: ../backgammon/bintree/

FILES:

  • README
  • bintree.h
  • Makefile
  • treeOps.cc | -insert| -remove| -getValue|
  • makeTree.cc
  • searchTree.cc
  • showTree.cc
  • treetest.cc

The BinNode class is a variation on the Node class, except, rather than next or prev, BinNode has left and right.

BinNode()

Default BinNode constructor will set the node's value to 0, and instantiate both the left and right nodes, setting them to NULL.

PATH: ../bintree/makeTree.cc

Function Parameter(s) Return value
BinNode() none void
/*
 *
 * default constructor
 *  set value = 0, left & right = NULL
 *
 */ 

BinNode *myTree = new BinNode();

BinNode(int)

Overloaded constructor will set the new node's value to the integer passed to the method, and instantiate both the left and right nodes, setting them to NULL

The overloaded constructor is called from the insert() function

PATH: ../bintree/makeTree.cc

Function Parameter(s) Return value
BinNode(int) int void
/*
 *
 * overloaded constructor 
 *   set value = i, left & right = NULL
 *
 */ 

int i = 10;
BinNode *myTree = new BinNode(i);

BinNode :: insert()

The insert() method will insert values into the list in an ordered fashion (I.E. small values to the left, large values to the right)

PATH: ../bintree/treeOps.cc

Function Parameter(s) Return value
insert(int) int int
/*
 *
 * insert function
 *    given a value, find its position
 *
 *    create a new BinNode() at said position
 *    with the given value
 *
 *    set new BinNode's left & right to NULL
 *
 */

int i = 10;
BinNode *myTree = new BinNode(i);

myTree -> insert(i);

int BinNode :: search(int)

This method will search the tree, looking for the value pass into it.

The search(int) function is called from the remove function (… and possibly from the insert() function) to place the temporary node at the desired position.

PATH: ../bintree/searchTree.cc

Function Parameter(s) Return value
search(int) int *BinNode
/*
 *
 * search returns a pointer to a BinNode
 *    given a value, find its position
 *    return a pointer to said position
 *
 */

BinNode *myTree = new BinNode(); //default constructor
BinNode *tmp;
... //add something

int i = 11;
tmp = myTree -> search(i);

BinNode :: remove(int)

PATH: ../bintree/treeOps.cc

Function Parameter(s) Return value
remove(int) int void
/*
 * remove
 *
 * read the comment @ ../bintree/treeOps.cc
 *
 */

BinNode *myTree = new BinNode(); //default constructor

... //add something

int i = 11;
myTree -> remove(i);

BinNode :: showTree()

PATH: ../bintree/showTree.cc

Function Parameter(s) Return value
showTree() none void
BinNode *myTree = new BinNode(); //default constructor

... //add something

myTree -> showTree();

BinNode :: showTree(BinNode *start)

PATH: ../bintree/showTree.cc

Function Parameter(s) Return value
showTree() BinNode *start void
BinNode *myTree = new BinNode(); //default constructor

... //add something

myTree -> showTree(current);

int BinNode :: getValue(int)

PATH: ../bintree/treeOps.cc

Function Parameter(s) Return value
getValue(int) int int
BinNode *myTree = new BinNode(); //default constructor

... //add something

int i = myTree -> getValue();
notes/data/bintree.txt · Last modified: 2010/11/17 21:15 by bwilson3