二叉搜索树(链表实现)

内容

建立二叉搜索树


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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

另一个人。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值