IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

C++ Binary Search Tree Destructor/remove Methods

, howto?


Chez
no avatar
Advanced Member
*******
Group: Members
Posts: 130
Joined: 30-December 05
Member No.: 16,385



Post #1 post Jun 8 2006, 12:21 AM
I have a binary search tree and I need to put a destructor on it. Ideally it would be called when I exit the program. How would a destructor look if it should delete all the nodes of a tree (recursively prefered).

Also, how would I go about removing a node from my tree and then replacing it so the tree is still ordered inorder-wise?

replacing specs:
* If the node x is a leaf, then it can simply be clipped off.
* If x has a single child then it can be replaced by its child.
* Otherwise, if x has two children, find the leftmost child in x's right subtree. This node, which is x's in-order successor, will have either zero or one children and can be removed using the previous two cases. Then this node can be used to "replace" x in the tree, so that xcan safely be deleted.

Here's what i have so far:
QUOTE

//******************************************************************
// Main.C
//******************************************************************
#include "Tree.h"
#include <iostream>
using namespace std;

// ----Main Class----
//
// Calls the appropriate method based on user input.
// a = add
// r = remove
// s = size
// c = contains
// p = print
// q = quit
//

int main ( )
{
BSTree mytree; // creates new tree

// Creates a buffer array for the input line to be stored on
char buffer[200];
while ( !cin.eof() )
{

cin.getline ( buffer, 200 );

// Scans the input line for a certain character predefined
// in the assignment. Input is set as the location of the
// found character + 2 to increment position to the parameter
// variable. Subtracting '0' gives the value of the character.
for (int i = 0 ; buffer[i] != '\0' ; i++ )
{
//------ADD------
if ( buffer[i] == 'a' )
{
int * input = new int;
int k;
for (k = i ; buffer[k+2] != '\0' ; k++)
*input = (*input*10) + (buffer[k+2] - '0');

if(mytree.add( input ))
cout << *input << " successfully added to tree" << endl;
i = k+1;
}

//------REMOVE------
else if ( buffer[i] == 'r' )
{
int * input = new int;
for (int k = i ; buffer[k+2] != '\0' ; k++)
*input = (*input*10) + (buffer[k+2] - '0');

if (mytree.contains ( *input ) == 1)
{
mytree.remove( *input );
}
else
cout << *input << " is not located in the tree" << endl;
}

//------CONTAINS------(done)
else if ( buffer[i] == 'c' )
{
int * input = new int;
for (int k = i ; buffer[k+2] != '\0' ; k++)
*input = (*input*10) + (buffer[k+2] - '0');

if (mytree.contains( *input ) == 1)
cout << *input << " is in the tree" << endl;
else
cout << *input << " is not in the tree" << endl;
}

//------SIZE------
else if ( buffer[i] == 's' )
{
cout << "The tree contains " << mytree.size() << " elements" << endl;
}

//------PRINT------
else if ( buffer[i] == 'p' )
{
mytree.print( );
cout << "Tree done." << endl;
}

//------QUIT------
else if ( buffer[i] == 'q' )
{
~BSTree() { delete [] mytree; }
return 0;
}
}
}
}

QUOTE

//******************************************************************
// Node.h
//
//
//******************************************************************

#ifndef NODE_H
#define NODE_H

#include "Tree.h"
#include <iostream>
using namespace std;

//******************************************************************
// class NODE
//
// The layout of each node. Stores a left and right child pointer
// as well as the data of the current node.
//******************************************************************
class NODE
{
public:
NODE* left;
NODE* right;
int *data;
} ;

#endif

QUOTE

//******************************************************************
// Tree.C
//
//
//******************************************************************

#include "Tree.h"
#include <iostream>
using namespace std;

//******************************************************************
// BSTree()
// Creates a new tree
//
// Initially null
//******************************************************************
BSTree::BSTree()
{
root = 0;
}


//******************************************************************
// ~BSTree()
// Destroys the tree
//
// Seals any memory leaks upon exit
//******************************************************************
BSTree::~BSTree()
{
delete root;
}


//******************************************************************
// add(int)
// Adds a new node to the tree
// int <data> - the value being inserted
// Return - true or false, depending on if the node was inserted
// Calls a recursive method to do tree traversals
//******************************************************************
bool BSTree::add ( int * data )
{
if (root == 0)
{
NODE* newnode = new NODE;
newnode->data = data;
newnode->left = 0;
newnode->right = 0;
root = newnode;
return 1;
}
else
return (findNode(root, data) != NULL );
}


//******************************************************************
// findNode(NODE*, int)
// Recursive method called by add(int) to traverse the tree, searching
// for the correct location to add a node.
// NODE* <root> - the root pointer, int <data> - the value to be added
// Return - node pointer to be evaluated by add(int)
//******************************************************************
NODE* BSTree::findNode(NODE *root, int *data)
{
if (root == 0)
{
NODE* newnode = new NODE;
newnode->data = data;
newnode->left = 0;
newnode->right = 0;
root = newnode;
return root;
}
else if (*data < *(root->data))
root->left = this->findNode(root->left, data);
else if (*data > *(root->data))
root->right = this->findNode(root->right, data);
else // data already in tree
{
cout << *data << " is already in the tree" << endl;
return 0;
}
return root;
}


//******************************************************************
// print()
// Prints the tree on its side, with correct indentation.
//
//
// Calls a recursive method to print the tree during traversal
//******************************************************************
void BSTree::print( )
{
printTree( root, 1 );
}


//******************************************************************
// printTree(NODE*, int)
// Recursive method called by print() to traverse the tree and print
// the node values with correct indentation.
// NODE* <root> - the root pointer, int <level> - the current level
//******************************************************************
void BSTree::printTree( NODE *root, int level )
{
if ( root != 0 )
{
printTree(root->right, level+1);
for(int i=0 ; i<3*level ; i++)
cout << " ";
cout << *(root->data) << endl;
printTree(root->left, level+1);
}
}


//******************************************************************
// contains(int)
// Evaluates whether or not a given value is present in the tree
// int <target> - the value to be compared with each node in the tree
// Return - True if the value is found in the tree, false otherwise
// Calls a recursive method to do tree traversals and comparisons
//******************************************************************
bool BSTree::contains ( int target)
{
return hasValue(root, target);
}


//******************************************************************
// hasValue(NODE*, int)
// Recursive method called by contains(int) to traverse the tree and
// compare each apprpriate node with the given target value.
// NODE* <root> - the root pointer, int <target> - the target value
// Return - True is the value is found, false otherwise
//******************************************************************
bool BSTree::hasValue ( NODE *root, int target )
{
if ( root == 0 ) return 0;
else
{
if ( target == *(root->data)) return 1;
else
{
if ( target < *(root->data) )
return ( this->hasValue ( root->left, target ) );
else
return ( this->hasValue ( root->right, target ) );
}
}
}


//******************************************************************
// size()
// Method that returns the numbers of elements in the tree
//
// Return - number of elements in the tree
// Calls a recursive method to traverse the tree and count each node
//******************************************************************
int BSTree::size ( )
{
return hasSize( root);
}


//******************************************************************
// hasSize(NODE*)
// Recursive function called by size() to traverse the tree and count each
// node.
// NODE* < root> - the root pointer
// Return - number of elements in the tree
//******************************************************************
int BSTree::hasSize(NODE* root)
{
if (root==0)
return(0);
else
return(this->hasSize(root->left) + 1 + this->hasSize(root->right));
}


//******************************************************************
// Remove(int)
// Removes a value-specific node from the tree and organizes the result
// to maintain an ordered inorder print (ascending).
// int <target> - the value to be removed
// Return - the node pointer
// Calls two recursive methods. One to do the actual deleting, and the
// other to find the target node.
//******************************************************************
int* BSTree::remove( int target )
{
NODE* location = findIt(root, target);
DeleteNode( location );
}


//******************************************************************
// DeleteNode(NODE*)
// Recursive method called by the remove(int) method to delete the
// given node and organize the tree
// NODE* <root> - the root pointer
//
//******************************************************************
void BSTree::DeleteNode(NODE* root) {
NODE* temp = root;
if (root->left == NULL && root->right == NULL)
{
delete temp;
}
else if (root->left != NULL && root->right == NULL)
{
root = root->left;
delete temp;
}
else if (root->left == NULL && root->right != NULL)
{
root = root->right;
delete temp;
}
else {
temp = root->left;
while (temp->right != NULL)
temp = temp->right;

root->data = temp->data;
DeleteNode(temp);
}
}


//******************************************************************
// findIt(NODE*, int)
// Recursive method called by the remove(int) method to seek out and
// return the location of the target node.
// NODE* <root> - the root pointer, int <target> - the value to seek out
// Return - the node location
//******************************************************************
NODE* BSTree::findIt(NODE *root, int target)
{
if (root == 0)
{
cout << "** ERROR: NOT FOUND **" << endl;
return 0;
}
else if (target < *(root->data))
root->left = this->findIt(root->left, target);
else if (target > *(root->data))
root->right = this->findIt(root->right, target);
else // 'target data' = 'root data'
return root;

return root;

}
/*
~BSTree()
{
if(root->left)
delete root->left[0];
if(Childs[1])
delete Childs[1];
}
// ...
delete Tree.Root;
}

void BSTree::clear()
{
deleteAll(root);
}
void BSTree::deleteAll(NODE* root)
{
delete BSTree();

}*/


QUOTE

//******************************************************************
// Tree.h
//
//******************************************************************

#ifndef TREE_H
#define TREE_H

#include "Node.h"
#include <iostream>
using namespace std;

//******************************************************************
// class BSTree
//
// Defines the methods to be used by the program.
// add - adds a new node to the tree in the correct locale
// contains - is the given value in the tree?
// print - prints the tree on its side
// size - returns the number of elements in the tree
// remove - removes a node and sorts it again
//******************************************************************
class BSTree
{

public:
BSTree();
~BSTree();
void clear ( );
bool add ( int * data );
NODE* findNode ( NODE *root, int * data);
bool contains ( int target );
void print ( );
int size ( );
int *remove(int k);


private:
NODE* root;

void deleteAll( NODE *root );
bool hasValue ( NODE *root, int target );
void printTree ( NODE *root, int level );
int hasSize ( NODE *root );
NODE* findIt ( NODE *root, int target );
void DeleteNode(NODE* root);
};

#endif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   1 yomi 3,648 15th August 2004 - 10:01 PM
Last post by: football123213
No New Posts   1 contactskn 50 3rd November 2009 - 08:12 PM
Last post by: Nameless_
No New Posts   5 contactskn 63 5th November 2009 - 11:20 PM
Last post by: user681
No New Posts   0 deedee2003 3,744 5th September 2004 - 09:11 PM
Last post by: deedee2003
No New Posts   11 husker 2,091 23rd May 2009 - 10:22 PM
Last post by: iG-donna jane
No New Posts 5 atoz 3,336 24th August 2009 - 07:14 AM
Last post by: jamjamnorman
No New Posts   5 zak92 1,499 18th October 2007 - 12:02 AM
Last post by: Tramposch
No New Posts 5 serverph 3,383 16th October 2004 - 03:34 PM
Last post by: googlue
No new   22 googlue 15,694 30th October 2006 - 09:30 AM
Last post by: cbkr
No New Posts 0 odomike 3,704 22nd October 2004 - 02:17 AM
Last post by: odomike
No New Posts   0 odomike 4,786 21st October 2004 - 11:56 PM
Last post by: odomike
No New Posts 0 cutteyboy 2,767 3rd November 2004 - 07:37 PM
Last post by: cutteyboy
No new   20 Mario 18,652 25th February 2005 - 07:13 PM
Last post by: talbotda13
No New Posts   9 solankyno1 14,034 15th October 2008 - 01:05 AM
Last post by: Tran-Gate
No New Posts   7 no9t9 4,117 3rd December 2004 - 05:43 PM
Last post by: serverph


 



RSS Open Discussion Time is now: 22nd November 2009 - 02:36 AM

Web Hosting Powered by ComputingHost.com.