二叉树的操作

1.在二叉树中查询含有val的结点

BTNode* FindValue(BTNode* ptr, ElemType val)   //在二叉树中查询含有val的结点
{
    if (ptr == nullptr || ptr->data == val)
    {
        return ptr;
    }
    else
    {
        BTNode * p =  FindValue(ptr->leftchild, val);   //左边找不到 就找右边
        if (nullptr == p)
        {
            p = FindValue(ptr->rightchild, val);
        }
        return p;
    }
}

2.二叉树找双亲结点

BTNode* FindParent(BTNode* ptr, BTNode* child) //找双亲
{
    if (nullptr == ptr || ptr-leftchild==child || ptr->rightchild == child)
    {
        return ptr;
    }
    else
    {
        BTNode* p = FindParent(ptr->leftchild, child);
        if (nullptr == p)
        {
            p = FindParent(ptr->rightchild, child);
        }
        return p;
    }
}

3.计算二叉树中结点的个数

int GetSize(BTNode* ptr)    //计算二叉树中结点的个数
{
    if (nullptr == ptr) 
        return 0;
    else 
        return GetSize(ptr->leftchild) + GetSize(ptr->rightchild) + 1; 
}  //左孩子结点的个数+右孩子结点的个数+1

4.计算二叉树的高度

int Max(int a, int b)     //找高度大的加一
{
    return a > b ? a : b;  
}
int GetDepth(BTNode* ptr)      //二叉树的高度
{
    if (nullptr == ptr) 
        return 0;
    else 
        return Max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;
}

 5.前序非递归遍历

void NicePreOrder(BTNode* ptr)  //前序非递归遍历
{
    if (nullptr == ptr) return; //空树
    std::stack st;     //将栈的地址压入进去  
    st.push(ptr);               //将ptr入栈
    while (!st.empty())
    {
        ptr = st.top(); st.pop();
        printf("%c ", ptr->data);
        if (ptr->rightchild != nullptr)   //
        {
            st.push(ptr->rightchild);
        }
        if (ptr->leftchild != nullptr)
        {
            st.push(ptr->leftchild);
        }
    }
    printf("\n");
}

6.二叉树层次遍历

void NiceLevelOrder(BTNode* ptr)    //层次遍历
{
    if (nullptr == ptr) return;
    std::queue qu;
    qu.push(ptr);
    while (!qu.empty())
    {
        ptr = qu.front; qu.pop();
        printf("%c", ptr->data);
        if (ptr->leftchild != nullptr)
        {
            qu.push(ptr->leftchild);
        }
        if (ptr->rightchild != nullptr)
        {
            qu.push(ptr->rightchild);
        }
    }
}

7.判断是否为满二叉树

bool IsFullBinarTree(BTNode* ptr)      //判断是否满二叉树
{
    bool res = true;
    if (nullptr = ptr) return res;
    int sum = 1;
    std::queuequ;
    qu.push(ptr);
    while (!qu.empty())
    {
        for (int i = 0; i < sum; ++i)
        {
            ptr = qu.front; qu.pop();
            if (ptr->leftchild != nullptr)qu.push(ptr->leftchild);
            if (ptr->rightchild != nullptr)qu.push(ptr->rightchild);
        }
        sum += sum;
        if (qu.size() == 0) break;
        if (qu.size() != sum)
        {
            res = false;
            break;
        }
    }
    return res;
}

 总代码如下:

#include
#include
#include
#include
#include  //  using  namespace std;
#include
typedef char ElemType;

typedef struct BTNode //BinaryTreeNode
{
    struct BTNode* leftchild;
    struct BTNode* rightchild;
    ElemType data;
}BTNode, * BinaryTree;


typedef struct
{
    BTNode* pnode;
    int popnum;
}StkNode;

BTNode* Buynode(ElemType val, BTNode* Larg = nullptr, BTNode* Rarg = nullptr)
{
    BTNode* s = (BTNode*)malloc(sizeof(BTNode));
    if (nullptr == s) exit(EXIT_FAILURE);
    s->data = val;
    s->leftchild = Larg;
    s->rightchild = Rarg;
    return s;
}
void Freenode(BTNode* p)
{
    free(p);
}
void PreOrder(BTNode* ptr)
{
    if (ptr != nullptr)
    {
        printf("%c ", ptr->data);
        PreOrder(ptr->leftchild);
        PreOrder(ptr->rightchild);
    }
}
void InOrder(BTNode* ptr)
{
    if (ptr != nullptr)
    {
        InOrder(ptr->leftchild);
        printf("%c ", ptr->data);
        InOrder(ptr->rightchild);
    }
}

void PastOrder(BTNode* ptr)
{
    if (ptr != nullptr)
    {
        PastOrder(ptr->leftchild);
        PastOrder(ptr->rightchild);
        printf("%c ", ptr->data);
    }
}
BTNode* CreateBinaryTree()
{
    ElemType ch;
    BTNode* s = nullptr;
    scanf_s("%c", &ch);
    if (ch != '#')
    {
        s = Buynode(ch);
        s->leftchild = CreateBinaryTree();
        s->rightchild = CreateBinaryTree();
    }
    return s;
}

BTNode* CreateBinaryTree(const char** p)
{
    BTNode* s = nullptr;
    if (p != nullptr && *p != nullptr && **p != '#')
    {
        s = Buynode(**p);
        s->leftchild = CreateBinaryTree(&++ * p);
        s->rightchild = CreateBinaryTree(&++ * p);
    }
    return s;
}

int FindIs(const char* is, const char ch, int n)
{
    int pos = -1;
    for (int i = 0; i < n; ++i)
    {
        if (is[i] == ch)
        {
            pos = i;
            break;
        }
    }
    return pos;
}
BTNode* CreatePI(const char* ps, const char* is, int n)
{
    BTNode* s = nullptr;
    if (n > 0)
    {
        s = Buynode(ps[0]);
        int pos = FindIs(is, ps[0], n);
        if (-1 == pos) exit(EXIT_FAILURE);
        s->leftchild = CreatePI(ps + 1, is, pos);
        s->rightchild = CreatePI(ps + pos + 1, is + pos + 1, n - pos - 1);
    }
    return s;
}
BTNode* CreateBinaryTreePI(const char* ps, const char* is)
{
    assert(ps != nullptr && is != nullptr);
    int n = strlen(ps);

    return CreatePI(ps, is, n);
}

BTNode* FindValue(BTNode* ptr, ElemType val)   //在二叉树中查询含有val的结点
{
    if (ptr == nullptr || ptr->data == val)
    {
        return ptr;
    }
    else
    {
        BTNode * p =  FindValue(ptr->leftchild, val);   //左边找不到 就找右边
        if (nullptr == p)
        {
            p = FindValue(ptr->rightchild, val);
        }
        return p;
    }
}

BTNode* Parent(BTNode* ptr, BTNode* child)
{
    if (nullptr == ptr || ptr->leftchild == child || ptr->rightchild == child)
    {
        return ptr;
    }
    else
    {
        BTNode* p = Parent(ptr->leftchild, child);
        if (nullptr == p)
        {
            p = Parent(ptr->rightchild, child);
        }
        return p;
    }
}

BTNode* FindParent(BTNode* ptr, BTNode* child) //找双亲
{
    if (nullptr == ptr || ptr-leftchild==child || ptr->rightchild == child)
    {
        return ptr;
    }
    else
    {
        BTNode* p = FindParent(ptr->leftchild, child);
        if (nullptr == p)
        {
            p = FindParent(ptr->rightchild, child);
        }
        return p;
    }
}


int GetSize(BTNode* ptr)    //计算二叉树中结点的个数
{
    if (nullptr == ptr) 
        return 0;
    else 
        return GetSize(ptr->leftchild) + GetSize(ptr->rightchild) + 1; 
}  //左孩子结点的个数+右孩子结点的个数+1

int Max(int a, int b)     //找高度大的加一
{
    return a > b ? a : b;  
}
int GetDepth(BTNode* ptr)      //二叉树的高度
{
    if (nullptr == ptr) 
        return 0;
    else 
        return Max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;
}

void NicePreOrder(BTNode* ptr)  //前序非递归遍历
{
    if (nullptr == ptr) return; //空树
    std::stack st;     //将栈的地址压入进去  
    st.push(ptr);               //将ptr入栈
    while (!st.empty())
    {
        ptr = st.top(); st.pop();
        printf("%c ", ptr->data);
        if (ptr->rightchild != nullptr)   //
        {
            st.push(ptr->rightchild);
        }
        if (ptr->leftchild != nullptr)
        {
            st.push(ptr->leftchild);
        }
    }
    printf("\n");
}

void NiceLevelOrder(BTNode* ptr)    //层次遍历
{
    if (nullptr == ptr) return;
    std::queue qu;
    qu.push(ptr);
    while (!qu.empty())
    {
        ptr = qu.front; qu.pop();
        printf("%c", ptr->data);
        if (ptr->leftchild != nullptr)
        {
            qu.push(ptr->leftchild);
        }
        if (ptr->rightchild != nullptr)
        {
            qu.push(ptr->rightchild);
        }
    }
}
 
bool IsFullBinarTree(BTNode* ptr)      //判断是否满二叉树
{
    bool res = true;
    if (nullptr = ptr) return res;
    int sum = 1;
    std::queuequ;
    qu.push(ptr);
    while (!qu.empty())
    {
        for (int i = 0; i < sum; ++i)
        {
            ptr = qu.front; qu.pop();
            if (ptr->leftchild != nullptr)qu.push(ptr->leftchild);
            if (ptr->rightchild != nullptr)qu.push(ptr->rightchild);
        }
        sum += sum;
        if (qu.size() == 0) break;
        if (qu.size() != sum)
        {
            res = false;
            break;
        }
    }
    return res;
}

int main()
{

    BinaryTree root = nullptr;
    const char* ps = "ABCDEFGH";
    const char* is = "CBEDFAGH";
    const char* ls = "CEFDBHGA";
    root = CreateBinaryTreePI(ps, is);
    InOrder(root);
    printf("\n");
    BTNode* p = FindValue(root, 'D');
    if (p != nullptr)
    {
        printf("Node addr : %p  val: %c \n", p, p->data);
    }
    else
    {
        printf("find not value \n");
    }
    printf("node size: %d \n", GetSize(root));

    NicePreOrder(root);
    NiceLevelOrder(root);
    return 0;
}

 

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