内容
建立二叉搜索树
treeNode* bulid_binarySearchTree(vector<int>&a)
{
treeNode* root = new treeNode (a[0]);
for (int i = 1; i < a.size(); i++)
{
treeNode* troot = root;
treeNode* tmp = new treeNode(a[i]);
while (troot) {
if (tmp->value < troot->value)
{
if (troot->lchild == NULL)
{
troot->lchild = tmp;
break;
}
else
{
troot = troot->lchild;
}
}
else if (tmp->value > troot->value)
{
if (troot->rchild == NULL)
{
troot->rchild = tmp;
break;
}
else
{
troot = troot->rchild;
}
}
}
}
return root;
}
中序遍历
void Inorder(treeNode* root) {
if (root == NULL) return;
Inorder(root->lchild);
cout << root->value << ' ';
Inorder(root->rchild);
}
前序遍历
void Preorder(treeNode* root) {
if (root == NULL) return;
cout << root->value<<' ';
Preorder(root->lchild);
Preorder(root->rchild);
}
后序遍历
void Postorder(treeNode* root) {
if (root == NULL) return;
Postorder(root->lchild);
Postorder(root->rchild);
cout << root->value << ' ';
}
层次遍历(基于广度优先搜索)
int h = 0;
void Layerorder(treeNode* root) {
queue<treeNode*>q;
treeNode* last=root; //last代表当前行最右结点
treeNode* nlast=NULL; //nlast代表下一行最右节点
q.push(root);
while (!q.empty())
{
treeNode* tmp = q.front();
cout << tmp->value << ' ';
q.pop();
if (tmp->lchild != NULL)
{
q.push(tmp->lchild);
nlast = tmp->lchild;
}
if (tmp->rchild != NULL)
{
q.push(tmp->rchild);
nlast = tmp->rchild;
}
if (tmp == last)
{
cout << endl;
last = nlast;
h++;
}
}
}
递归求树高
int treeHeight(treeNode* root)
{
if (root == NULL) return 0;
int lh = treeHeight(root->lchild);
int rh = treeHeight(root->rchild);
return lh > rh ? lh + 1 : rh + 1;
}
查找(时间复杂度:logn)
bool find(treeNode* root, int tar)
{
while(root)
{
if (tar == root->value)
return 1;
else if (tar > root->value)
root = root->rchild;
else if (tar < root->value)
root = root->lchild;
}
return 0;
}
完整代码
#include<bits/stdc++.h>
using namespace std;
struct treeNode {
int value;
treeNode* lchild;
treeNode* rchild;
treeNode(int v)
{
value = v;
lchild = NULL;
rchild = NULL;
}
};
treeNode* bulid_binarySearchTree(vector<int>&a)
{
treeNode* root = new treeNode (a[0]);
for (int i = 1; i < a.size(); i++)
{
treeNode* troot = root;
treeNode* tmp = new treeNode(a[i]);
while (troot) {
if (tmp->value < troot->value)
{
if (troot->lchild == NULL)
{
troot->lchild = tmp;
break;
}
else
{
troot = troot->lchild;
}
}
else if (tmp->value > troot->value)
{
if (troot->rchild == NULL)
{
troot->rchild = tmp;
break;
}
else
{
troot = troot->rchild;
}
}
}
}
return root;
}
//中序遍历
void Inorder(treeNode* root) {
if (root == NULL) return;
Inorder(root->lchild);
cout << root->value << ' ';
Inorder(root->rchild);
}
//前序遍历
void Preorder(treeNode* root) {
if (root == NULL) return;
cout << root->value<<' ';
Preorder(root->lchild);
Preorder(root->rchild);
}
//后序遍历
void Postorder(treeNode* root) {
if (root == NULL) return;
Postorder(root->lchild);
Postorder(root->rchild);
cout << root->value << ' ';
}
//基于广度优先搜索的层次遍历
int h = 0;
void Layerorder(treeNode* root) {
queue<treeNode*>q;
treeNode* last=root; //last代表当前行最右结点
treeNode* nlast=NULL; //nlast代表下一行最右节点
q.push(root);
while (!q.empty())
{
treeNode* tmp = q.front();
cout << tmp->value << ' ';
q.pop();
if (tmp->lchild != NULL)
{
q.push(tmp->lchild);
nlast = tmp->lchild;
}
if (tmp->rchild != NULL)
{
q.push(tmp->rchild);
nlast = tmp->rchild;
}
if (tmp == last)
{
cout << endl;
last = nlast;
h++;
}
}
}
//递归求树高
int treeHeight(treeNode* root)
{
if (root == NULL) return 0;
int lh = treeHeight(root->lchild);
int rh = treeHeight(root->rchild);
return lh > rh ? lh + 1 : rh + 1;
}
//查找 时间复杂度o(log n)
bool find(treeNode* root, int tar)
{
while(root)
{
if (tar == root->value)
return 1;
else if (tar > root->value)
root = root->rchild;
else if (tar < root->value)
root = root->lchild;
}
return 0;
}
int main()
{
vector<int>a{ 1,5,7,9,6,2,3 };
treeNode* root;
root=bulid_binarySearchTree(a);
Inorder(root);//中序遍历
cout << endl;
Preorder(root);//前序遍历
cout << endl;
Postorder(root);//后序遍历
cout << endl;
Layerorder(root);//层次遍历
cout << "树高为" << h<<endl;
int h1 = treeHeight(root);
cout << h1 << endl;
int tar;
cin >> tar;
cout << find(root, tar) << endl;
return 0;
}