二叉树函数

时间:2012-04-26 21:24:05

标签: binary-tree

我如何清理这些代码,以便所有二叉树操作都在自己的函数套件中,这段代码很乱,我想清理它

#include <iostream>
#include "binarytree.h"

using namespace std ;

node *head = 0;

node *create_node( double x )
{
    node *temp = new node();
    temp->left = 0 ;
    temp->right = 0 ;
    temp->value = x ;

    return temp ;
}

void insert_node( node *newnode )
{
    node *current = head ;
    node *last = current;

    while(current != 0) // while(current)
    {
        last = current ;
        if( current->value < newnode->value )
        {
                current = current->right ;
        }
        else
        {
            current = current->left ;
        }
    }

    if( last->value < newnode->value )
    {
        last->right = newnode ;
    }
    else
    {
        last->left = newnode ;
    }
}

void list_nodes( node *mynode )
{
    if(mynode->left)
    {
        list_nodes(mynode->left);
    }

    cout << "\t\t" << mynode->value << endl ;

    if(mynode->right)
    {
        list_nodes(mynode->right);
    }
}

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** /

void do_insert()
{
    double d ;

    cout << "\tInserting\n" ;
    cout << "\t\tEnter a double to insert: " ;
    cin >> d ;

    node *newnode = create_node( d ) ;

    if(head == 0) // if( !head )
    {
        head = newnode ;
    }
    else
    {
        insert_node( newnode );
    }

    return ;
}

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** /

void do_lookup()
{
    double d;

    cout << "\tLookup\n" ;
    cout << "\t\tEnter a double to look up: " ;
    cin >> d ;

    node *current = head ;

    while(current)
    {
        if( current->value == d )
        {
            cout << "\t\tValue found in tree" << endl;
            return ;
        }
        else
        {
            if( current->value < d )
            {
                current = current->right;
            }
            else
            {
                current = current->left;
            }
        }
    }

    cout << "\t\tValue not found in tree" << endl;
    return ;
}

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** /

void do_delete()
{
    double d;
    node *old_node, *left, *right, *current, *last;

    cout << "\tDeleting\n" ;
    cout << "\t\tEnter a double to delete: " ;
    cin >> d ;

    if(!head)
    {
        return ;
    }

    if( head->value == d )
    {
        old_node = head ;
        left = head->left ;
        right = head->right ;

        if( left )
        {
            head = left ;

            if(right)
            {
                insert_node( right );
            }
        }
        else if( right )
        {
            head = right ;
        }
        else
        {
            head = 0;
        }

        //delete old_node ;

        return;
    }
    else
    {
        current = head ;
        last = current;

        while(current)
        {
            if( current->value == d )
            {
                old_node = current ;
                left = current->left ;
                right = current->right ;

                if( last->value < d )
                {
                    last->right = 0;
                }
                else
                {
                    last->left = 0 ;
                }

                if(left)
                {
                    insert_node(left);
                }

                if(right)
                {
                    insert_node(right);
                }

                //delete old_node ;

                return ;
            }
            else
            {
                last = current;

                if( current->value < d )
                {
                    current = current->right;
                }
                else
                {
                    current = current->left;
                }
            }
        }
    }

    cout << "\t\tValue not found in tree" << endl;
    return ;
}

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** /

void do_list()
{ 
    cout << "\tListing\n" ;

    if(head)
    {
        list_nodes( head );
    }
    else
    {
        cout << "\t\tEmpty\n";
    }

    return ;
}

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** /

1 个答案:

答案 0 :(得分:0)

do_xxx()个功能复制到xxx()个功能。

xxx()函数中删除IO。

do_xxx()函数中的二叉树操作替换为xxx()函数的相应调用。