#include<iostream>
using namespace std;
struct BstNode
{
int data;
BstNode* left;
BstNode* right;
};
BstNode* GetNewNode(int data)
{
BstNode* newNode = new BstNode;
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
BstNode* Insert(BstNode* root, int data)
{
if(root == NULL)//数是空的
{
root = GetNewNode(data);
return root;
}
else if(data <= root->data)
root->left = Insert(root->left,data);
else root->right = Insert(root->right,data);
return root;
}
bool Search(BstNode* root,int data)
{
if(root == NULL) return false;
if(root->data == data) return true;
else if(data <= root->data) return Search(root->left,data);
else return Search(root->right,data);
}
int main()
{
BstNode* root = NULL;//表示树是空的
root = Insert(root,15);
root = Insert(root,10);
root = Insert(root,20);
root = Insert(root,25);
root = Insert(root,8);
root = Insert(root,12);
int number;
cout << "Enter number be searched\n";
cin >> number;
if(Search(root,number) == true) cout << "Found\n";
else cout << "Not Found\n";
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容