二叉搜索树(数组实现)

内容

建立二叉搜索树


bool buildtree(int k)
{
    tree[k].value = a[index++];
    int cur = 0;
    if (index == a.size())
        return true;
    while (1)
    {
        if (a[index] < tree[cur].value)
        {
            if (tree[cur].lchild == -1)
            {
                tree[cur].lchild = 2 * cur + 1;
                if (buildtree(tree[cur].lchild) == true)
                    return true;
            }
            else
                cur = cur * 2 + 1;
        }
        if (a[index] > tree[cur].value)
        {
            if (tree[cur].rchild == -1)
            {
                tree[cur].rchild = 2 * cur + 2;
                if (buildtree(tree[cur].rchild) == true)
                    return true;
            }
            else
                cur = cur * 2 + 2;
        }
    }
}

中序遍历


void inorder(int k)
{
    if (k ==-1) return;
    inorder(tree[k].lchild);
    cout << tree[k].value;
    inorder(tree[k].rchild);
}

前序遍历


void Preorder(int k)
{
    if (k == -1) return;
    cout << tree[k].value;
    Preorder(tree[k].lchild);
    Preorder(tree[k].rchild);
}

后序遍历


void Postorder(int k)
{
    if (k == -1)return;
    Postorder(tree[k].lchild);
    Postorder(tree[k].rchild);
    cout << tree[k].value;
}

层次遍历


void layerorder(int k)
{
    queueq;
    q.push(k);
    int last = 0, nlast = 0;
    while (!q.empty())
    {
        int tmp = q.front();
        cout << tree[tmp].value;
        q.pop();
        if (tree[tmp].lchild != -1)
        {
            q.push(tree[tmp].lchild);
            nlast = tree[tmp].lchild;
        }
        if (tree[tmp].rchild != -1)
        {
            q.push(tree[tmp].rchild);
            nlast = tree[tmp].rchild;
        }
        if (tmp == last)
        {
            cout << endl;
            last = nlast;
        }
    }
}

递归求树高


int treeheight(int k)
{
    if (k == -1) return 0;
    int lh = treeheight(tree[k].lchild);
    int rh = treeheight(tree[k].rchild);
    return lh > rh ? lh + 1 : rh + 1;
}

查找(时间复杂度:logn)


bool find(int k,int target)
{
    while (k!=-1)
    {
        if (target == tree[k].value)
            return 1;
        else if (target < tree[k].value)
            k = tree[k].lchild;
        else if (target > tree[k].value)
            k = tree[k].rchild;
    }
    return 0;
}

完整代码


#include
using namespace std;
struct treenode {
    int value;
    int lchild, rchild;
}tree[10001];
vectora{ 9,12,3,5,8,7 };
int index = 0;
bool buildtree(int k)
{
    tree[k].value = a[index++];
    int cur = 0;
    if (index == a.size())
        return true;
    while (1)
    {
        if (a[index] < tree[cur].value)
        {
            if (tree[cur].lchild == -1)
            {
                tree[cur].lchild = 2 * cur + 1;
                if (buildtree(tree[cur].lchild) == true)
                    return true;
            }
            else
                cur = cur * 2 + 1;
        }
        if (a[index] > tree[cur].value)
        {
            if (tree[cur].rchild == -1)
            {
                tree[cur].rchild = 2 * cur + 2;
                if (buildtree(tree[cur].rchild) == true)
                    return true;
            }
            else
                cur = cur * 2 + 2;
        }
    }
}
void inorder(int k)
{
    if (k == -1) return;
    inorder(tree[k].lchild);
    cout << tree[k].value;
    inorder(tree[k].rchild);
}
void Preorder(int k)
{
    if (k == -1) return;
    cout << tree[k].value;
    Preorder(tree[k].lchild);
    Preorder(tree[k].rchild);
}
void Postorder(int k)
{
    if (k == -1)return;
    Postorder(tree[k].lchild);
    Postorder(tree[k].rchild);
    cout << tree[k].value;
}
void layerorder(int k)
{
    queueq;
    q.push(k);
    int last = 0, nlast = 0;
    while (!q.empty())
    {
        int tmp = q.front();
        cout << tree[tmp].value;
        q.pop();
        if (tree[tmp].lchild != -1)
        {
            q.push(tree[tmp].lchild);
            nlast = tree[tmp].lchild;
        }
        if (tree[tmp].rchild != -1)
        {
            q.push(tree[tmp].rchild);
            nlast = tree[tmp].rchild;
        }
        if (tmp == last)
        {
            cout << endl;
            last = nlast;
        }
    }
}
int treeheight(int k)
{
    if (k == -1) return 0;
    int lh = treeheight(tree[k].lchild);
    int rh = treeheight(tree[k].rchild);
    return lh > rh ? lh + 1 : rh + 1;
}
bool find(int k,int target)
{
    while (k!=-1)
    {
        if (target == tree[k].value)
            return 1;
        else if (target < tree[k].value)
            k = tree[k].lchild;
        else if (target > tree[k].value)
            k = tree[k].rchild;
    }
    return 0;
}
int main()
{
    for (int i = 0; i < 10001; i++)
        tree[i].value = -1, tree[i].lchild = -1, tree[i].rchild = -1;
    buildtree(0);
    inorder(0);
    cout << endl;
    Preorder(0);
    cout << endl;
    Postorder(0);
    cout << endl;
    layerorder(0);
    cout << endl;
    cout << treeheight(0) << endl;
    int tar;
    while (cin >> tar)
        cout << find(0, tar) << endl;
    return 0;
}

你可能感兴趣的:(数据结构与算法,算法,数据结构,c++)