二叉搜索树的各种操作

#include <iostream>
#include <malloc.h>
#include <queue>
#include <string>
#include <string.h>
#include <iomanip>
#include <stdio.h>
using namespace std;
int n;
char a[1005], b[1005];
typedef struct node
{
    int data;
    node *Left;
    node *Right;
} TreeNode, *BinTree;
BinTree INS( BinTree root, int n )
{
    if( root == NULL )
    {
        root = (BinTree)malloc(sizeof(TreeNode));
        root->data = n;
        root->Left = NULL;
        root->Right = NULL;
    }
    else
    {
        if( n < root->data )
            root->Left = INS( root->Left, n );
        else if( n > root->data )
            root->Right = INS( root->Right, n );
    }
    return root;
}
BinTree Found( BinTree root, int n)
{
    if( root == NULL )
        return NULL;
    else
    {
        while( root != NULL )
        {
            if( root->data == n )
                return root;
            else if( root->data < n )
            {
                root = root->Right;
            }
            else if( root->data > n )
            {
                root = root->Left;
            }
        }
        return root;
    }
}
void PreTrans( BinTree root )
{
    if( root == NULL )
        return;
    else
    {
        printf("%d ", root->data );
        PreTrans( root->Left );
        PreTrans( root->Right );
    }
}
BinTree FindMax( BinTree root )
{
    if( root != NULL )
    {
        while( root->Right != NULL )
        {
            root = root->Right;
        }
        return root;
    }
}
int main(int argc, char *argv[])
{
    int first = 1;
    BinTree root;
    while( cin >> n )
    {
        if( first )
        {
        root = (BinTree)malloc(sizeof(TreeNode));
        root->data = n;
        root->Left = NULL;
        root->Right = NULL;
        first = 0;
        }
        else
        {
            INS(root, n );
        }
        char tmp;
        tmp = cin.get();
        if( tmp == '\n' )
            break;
    }
    PreTrans(root);
    BinTree ans = FindMax(root);
    printf("%d\n", ans->data);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值