Saturday, May 22, 2010

Binary Search Tree?

i need the algorithm and source code(c++) of Binary Search Tree. any one help

Binary Search Tree?
#ifndef BINARY_SEARCH_TREE_H_


#define BINARY_SEARCH_TREE_H_





#include "dsexceptions.h"


#include %26lt;iostream.h%26gt; // For NULL





// Binary node and forward declaration because g++ does


// not understand nested classes.


template %26lt;class Comparable%26gt;


class BinarySearchTree;





template %26lt;class Comparable%26gt;


class BinaryNode


{


Comparable element;


BinaryNode *left;


BinaryNode *right;





BinaryNode( const Comparable %26amp; theElement, BinaryNode *lt, BinaryNode *rt )


: element( theElement ), left( lt ), right( rt ) { }


friend class BinarySearchTree%26lt;Comparable%26gt;;


};








// BinarySearchTree class


//


// CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds


//


// ******************PUBLIC OPERATIONS*********************


// void insert( x ) --%26gt; Insert x


// void remove( x ) --%26gt; Remove x


// Comparable find( x ) --%26gt; Return item that matches x


// Comparable findMin( ) --%26gt; Return smallest item


// Comparable findMax( ) --%26gt; Return largest item


// boolean isEmpty( ) --%26gt; Return true if empty; else false


// void makeEmpty( ) --%26gt; Remove all items


// void printTree( ) --%26gt; Print tree in sorted order





template %26lt;class Comparable%26gt;


class BinarySearchTree


{


public:


explicit BinarySearchTree( const Comparable %26amp; notFound );


BinarySearchTree( const BinarySearchTree %26amp; rhs );


~BinarySearchTree( );





const Comparable %26amp; findMin( ) const;


const Comparable %26amp; findMax( ) const;


const Comparable %26amp; find( const Comparable %26amp; x ) const;


bool isEmpty( ) const;


void printTree( ) const;





void makeEmpty( );


void insert( const Comparable %26amp; x );


void remove( const Comparable %26amp; x );





const BinarySearchTree %26amp; operator=( const BinarySearchTree %26amp; rhs );





private:


BinaryNode%26lt;Comparable%26gt; *root;


const Comparable ITEM_NOT_FOUND;





const Comparable %26amp; elementAt( BinaryNode%26lt;Comparable%26gt; *t ) const;





void insert( const Comparable %26amp; x, BinaryNode%26lt;Comparable%26gt; * %26amp; t ) const;


void remove( const Comparable %26amp; x, BinaryNode%26lt;Comparable%26gt; * %26amp; t ) const;


BinaryNode%26lt;Comparable%26gt; * findMin( BinaryNode%26lt;Comparable%26gt; *t ) const;


BinaryNode%26lt;Comparable%26gt; * findMax( BinaryNode%26lt;Comparable%26gt; *t ) const;


BinaryNode%26lt;Comparable%26gt; * find( const Comparable %26amp; x, BinaryNode%26lt;Comparable%26gt; *t ) const;


void makeEmpty( BinaryNode%26lt;Comparable%26gt; * %26amp; t ) const;


void printTree( BinaryNode%26lt;Comparable%26gt; *t ) const;


BinaryNode%26lt;Comparable%26gt; * clone( BinaryNode%26lt;Comparable%26gt; *t ) const;


};





#include "BinarySearchTree.cpp"


#endif
Reply:be more specific
Reply:What can you do with a binary tree? You can add an item to it,


you can delete an item from it, and you can search for an item


in it. There are more sophisticated "tree" operations, but those arise in


applications using binary trees.





An easy reference to cite for algorithms dealing with trees and other data


structures is Donald Knuth's books. Any decent textbook on data structures will give the algorithms for insertion, deletion and search.


It is fairly obvious to work out for yourself on paper, as long as you account for all the cases where a given node does or does not have a left child and a right child.





You can also find the algorithms published in many places online.


No comments:

Post a Comment