#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;
}
二叉搜索树的各种操作
最新推荐文章于 2024-09-12 23:49:00 发布