PHP Classes

File: AVL_Test.php

Recommend this page to a friend!
  Classes of Raimund Neumann   AVLTree   AVL_Test.php   Download  
File: AVL_Test.php
Role: Unit test script
Content type: text/plain
Description: Test program with sample subclass NumTree
Class: AVLTree
AVL tree class
Author: By
Last change:
Date: 21 years ago
Size: 1,413 bytes
 

Contents

Class file image Download
<?php
/// ---- AVL_test.php ---- Raimund Neumann 18.12.02 16:19
require_once('AVLTree.php');

class
NumTree extends AVLTree {
    function
NumTree( $val ) {
       
$this->AVLTree();
       
$this->data = $val;
       
$this->depth = 1;
    }
   
    function
add( $val ) {
        if(
$val == $this->data ) {
            echo
"$val already in tree<BR>\n";
            return ;
        }

        if(
$val < $this->data ) {
            if(
$this->left === NULL )
               
$this->left =& new NumTree($val);
            else {
               
$this->left->add( $val );
               
$this->balance();
            }
        } else {
           
assert( $val > $this->data );
           
            if(
$this->right === NULL )
               
$this->right =& new NumTree($val);
            else {
               
$this->right->add( $val );
               
$this->balance();
            }
        }
       
          
$this->getDepthFromChildren();
    }
}

$n =& new NumTree( 8 );
echo
$n->toString();
$n->add( 9 );
echo
$n->toString();
$n->add( 10);
echo
$n->toString();
$n->add( 2 );
echo
$n->toString();
$n->add( 1 );
echo
$n->toString();
$n->add( 5 );
echo
$n->toString();
$n->add( 3 );
echo
$n->toString();
$n->add( 6 );
//$noisy=TRUE;
echo $n->toString();
$n->add( 4 );
echo
$n->toString();
$n->add( 7 );
echo
$n->toString();
$n->add( 11);
echo
$n->toString();
$n->add( 12);
echo
$n->toString();
?>