C++二叉排序树之查找结点

本文详细探讨了如何使用C++编程语言实现二叉排序树,并专注于查找节点的过程,同时关注计算查找节点的具体次数,以优化算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
using namespace std;
class TreeNode {
public:
    int data;
    TreeNode *LeftChild;
    TreeNode *RightChild;
    TreeNode() :LeftChild(NULL), RightChild(NULL) {}
};
class BiSortTree {
private:
    TreeNode *root;
    int *info;
    int size;
    int loc;
    TreeNode *CreateTree(TreeNode *p);
    TreeNode* InsertNode(TreeNode *p, int d);
    TreeNode* Search(TreeNode *p, int d);
    void Delete(int d);

public:
    BiSortTree(int s, int arr[]);
    void CreateBiSortTree();
    void InsertBiSortTree(int d);
    void InOder(TreeNode *p);
    void InOderTree();
    void SearchData(int d);
    void DeleteData(int d);
};
BiSortTree::BiSortTree(int s, int arr[]) {
    loc = 1;
    size = s;
    info = new int[size];
    for (int i = 0; i < size; i++)
        info[i] = arr[i];
}
void BiSortTree::CreateBiSortTree() {
    root = new TreeNode();
    if (size)
        root->data = info[0];
    CreateTree(root);
}
TreeNode *BiSortTree::CreateTree(TreeNode *p) {
    if (loc < size) {
        if (p->data > info[loc]) {
            if (!p->LeftChild) {
                p->LeftChild = new TreeNode();
                p->LeftChild->data = info[loc++];
            }
            else
                CreateTree(p->LeftChild);
        }
        else
            if (p->data < info[loc]) {
                if (!p->RightChild) {
                    p->RightChild = new TreeNode();
                    p->RightChild->data = info[loc++];
                }
                else
                    CreateTree(p->RightChild);
            }
        CreateTree(root);
    }
    return NULL;
}
void BiSortTree::InOderTree() {
    InOder(root);
}
void BiSortTree::InOder(TreeNode *p) {
    if (p) {
        InOder(p->LeftChild);
        cout << p->data << ' ';
        InOder(p->RightChild);
    }
}
void BiSortTree::SearchData(int d) {
    TreeNode *t = Search(root, d);
    if (t)
        cout << t->data;
    else
        cout << "NOT FOUND";
}
TreeNode* BiSortTree::Search(TreeNode *p, int d) {
    if (p) {
        if (p->data == d)
            return p;
        if (p->data > d)
            return Search(p->LeftChild, d);
        else
            return Search(p->RightChild, d);
    }
    return NULL;
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        int s;
        cin >> s;
        int *arr = new int[s];
        for (int i = 0; i < s; i++)
            cin >> arr[i];
        BiSortTree sTree(s, arr);
        sTree.CreateBiSortTree();
        sTree.InOderTree();
        cout << endl;
        int m;
        cin >> m;
        while (m--) {
            int d;
            cin >> d;
            sTree.SearchData(d);
            cout<<endl;
        }
    }
    return 0;
}

计算查找次数

#include <iostream>
using namespace std;
class TreeNode {
public:
    int data;
    TreeNode *LeftChild;
    TreeNode *RightChild;
    TreeNode() :LeftChild(NULL), RightChild(NULL) {}
};
class BiSortTree {
private:
    TreeNode *root;
    int *info;
    int size;
    int loc;
    int count;
    TreeNode *CreateTree(TreeNode *p);
    TreeNode* InsertNode(TreeNode *p, int d);
    int Search(TreeNode *p, int d);
    void Delete(int d);

public:
    BiSortTree(int s, int arr[]);
    void CreateBiSortTree();
    void InsertBiSortTree(int d);
    void InOder(TreeNode *p);
    void InOderTree();
    void SearchData(int d);
    void DeleteData(int d);
};
BiSortTree::BiSortTree(int s, int arr[]) {
    loc = 1;
    count = 0;
    size = s;
    info = new int[size];
    for (int i = 0; i < size; i++)
        info[i] = arr[i];
}
void BiSortTree::CreateBiSortTree() {
    root = new TreeNode();
    if (size)
        root->data = info[0];
    CreateTree(root);
}
TreeNode *BiSortTree::CreateTree(TreeNode *p) {
    if (loc < size) {
        if (p->data > info[loc]) {
            if (!p->LeftChild) {
                p->LeftChild = new TreeNode();
                p->LeftChild->data = info[loc++];
            }
            else
                CreateTree(p->LeftChild);
        }
        else
            if (p->data < info[loc]) {
                if (!p->RightChild) {
                    p->RightChild = new TreeNode();
                    p->RightChild->data = info[loc++];
                }
                else
                    CreateTree(p->RightChild);
            }
        CreateTree(root);
    }
    return NULL;
}
void BiSortTree::InOderTree() {
    InOder(root);
}
void BiSortTree::InOder(TreeNode *p) {
    if (p) {
        InOder(p->LeftChild);
        cout << p->data << ' ';
        InOder(p->RightChild);
    }
}
void BiSortTree::SearchData(int d) {
    count = 0;
    cout << Search(root, d);
}
int BiSortTree::Search(TreeNode *p, int d) {
    if (!p) {
        count = -1;
        return count;
    }
    if (p->data == d)
        return ++count;
    if (p->data > d)
        Search(p->LeftChild, d);
    else
        Search(p->RightChild, d);
    if (count > 0)
        return ++count;
    return count;
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        int s;
        cin >> s;
        int *arr = new int[s];
        for (int i = 0; i < s; i++)
            cin >> arr[i];
        BiSortTree sTree(s, arr);
        sTree.CreateBiSortTree();
        sTree.InOderTree();
        cout << endl;
        int m;
        cin >> m;
        while (m--) {
            int d;
            cin >> d;
            sTree.SearchData(d);
            cout<<endl;
        }
    }
    //system("pause");
    return 0;
}

/*InPut
1
6
22 33 55 66 11 44
7
11
22
33
44
55
66
77
*/

/*OutPut 查找次数
11 22 33 44 55 66 
2
1
2
4
3
4
-1
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值