二叉树

二叉树的代码,循环遍历,先序遍历,中序遍历,按照二叉树的层来遍历;
其中比较麻烦一点的是循环中序遍历,需要用到两个栈来存储变量,有些难以理解,建议画图。回头还需要好好想想循环中序遍历的代码。

头文件代码如下,函数的申明等


#ifndef  __BINARYTREE_H_
#define  __BINARYTREE_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stack>
#include <queue>
#define N 10
typedef int Data;
typedef struct _Node
{
    struct _Node *left;
    struct _Node *right;
    Data data;
} Node;
void createTree(Node **pRoot);
void freeTree(Node *pRoot);
void midPrintTree(Node *pRoot);
void prePrintTree(Node *pRoot);
void insertNode(Node **ppRoot, Data &val);
void midPrintTree_loop(Node *pRoot);
void prePrintTree_loop(Node *pRoot);
void layerPrintTree(Node *pRoot);
Data varr[N] = {10, 38, 100, -3, 80, -20, 91, 27, 50, 350};
#endif  //__BINARYTREE_H_

主要的main函数代码如下:
请注意一下循环的树遍历代码

#include "binaryTree1.h"

void createTree(Node **ppRoot)
{
    Node *pRoot = *ppRoot;
    //*ppRoot = NULL;
    int i = 0;
    Node *pNode = NULL;
    Node *pNode1 = NULL;
    for(i = 0; i < 10; ++ i)
    {
        pNode = (Node *)malloc(sizeof(Node));
        pNode -> data = varr[i];
        pNode -> left = NULL;
        pNode -> right = NULL;

        if(NULL == *ppRoot)
        {
            *ppRoot = pNode;
            pNode1 = *ppRoot;
        }
        else
        {
            pNode1 = *ppRoot;
            while(pNode1 != NULL)
            {
                if(pNode -> data < pNode1 -> data)
                {
                    if(pNode1 -> left != NULL)
                    {
                        pNode1 = pNode1 -> left;
                        continue;
                    }else{
                        pNode1 -> left = pNode;
                        break;
                    }
                }
                if(pNode -> data >= pNode1 -> data)
                {
                    if(pNode1 -> right != NULL)
                    {
                        pNode1 = pNode1 -> right;
                        continue;
                    }else{
                        pNode1 -> right = pNode;
                        break;
                    }
                }
            }
        }
    }
}

void freeTree(Node *pRoot)
{
    if(NULL == pRoot)
        return;
    freeTree(pRoot -> left);
    freeTree(pRoot -> right);
    pRoot -> left = NULL;
    pRoot -> right = NULL;
    free(pRoot);
    pRoot = NULL;
}

void midPrintTree(Node *pRoot)
{
    if(NULL == pRoot)
        return;
    //printf("%d\t", pRoot -> data);
    midPrintTree(pRoot -> left);
    printf("%d\t", pRoot -> data);
    midPrintTree(pRoot -> right);

}

void prePrintTree(Node *pRoot)
{
    if(NULL == pRoot)
        return;
    printf("%d\t", pRoot -> data);
    prePrintTree(pRoot -> left);
    //printf("%d\t", pRoot -> data);
    prePrintTree(pRoot -> right);
}

void insertNode(Node **ppRoot, Data &val)
{
    //Node *pRoot = *ppRoot;
    Node *pNode = (Node *)malloc(sizeof(Node));
    //pNode = (Node *)malloc(sizeof(Node));
    pNode -> data = val;
    pNode -> left = NULL;
    pNode -> right = NULL;
    if(NULL == *ppRoot)
    {
        //printf("Insert function\n");
        *ppRoot = pNode;
        return;
    }
    //Node *pRoot = *ppRoot;
    Node *pNode_tmp = *ppRoot;
    Node *pNode_tmp1 = *ppRoot;
    while(pNode_tmp != NULL)
    {
        if(val < pNode_tmp -> data)
        {
            pNode_tmp1 = pNode_tmp;
            pNode_tmp = pNode_tmp -> left;
            continue;
        }
        if(val >= pNode_tmp -> data)
        {
            pNode_tmp1 = pNode_tmp;
            pNode_tmp = pNode_tmp -> right;
            continue;
        }
    }
    if(pNode_tmp1 -> data < val)
        pNode_tmp1 -> right = pNode;
    else
        pNode_tmp1 -> left = pNode;
    //pNode_tmp = pNode;
}

void midPrintTree_loop(Node *pRoot)
{
    if(NULL == pRoot)
        return ;
    std::stack<Node *> pstack;
    Node *pNode = NULL;
    Node *pNode_mid = NULL;
    pstack.push(pRoot);
    pNode_mid = pRoot -> left;

    while(!pstack.empty() || pNode_mid != NULL)
    {
        if(NULL != pNode_mid)
        {
            pstack.push(pNode_mid);
            pNode_mid = pNode_mid -> left;
            continue;
        }

        if(pNode != NULL)
        {
            printf("%d\t", pNode -> data);
            pNode = NULL;
        }

        if(pNode_mid == NULL)
        {
            pNode = pstack.top();
            pstack.pop();
            if(NULL != pNode -> right)
                pNode_mid = pNode -> right;
        }

    }

    if(NULL != pNode)
    {
        printf("%d\t", pNode -> data);
        pNode = NULL;
    }
}

void prePrintTree_loop(Node *pRoot)
{
    if(NULL == pRoot)
        return ;
    std::stack<Node *> pstack;
    Node *pNode = NULL;
    pstack.push(pRoot);
    while(!pstack.empty())
    {
        pNode = pstack.top();
        pstack.pop();
        printf("%d\t", pNode -> data);
        if(NULL != pNode -> right)
            pstack.push(pNode -> right);
        if(NULL != pNode -> left)
            pstack.push(pNode -> left);
    }
    /*
    if(pNode != NULL)
    {
        printf("%d\t", pNode -> data);
        pNode = NULL;
    }
    */
}

void layerPrintTree(Node *pRoot)
{
    if(NULL == pRoot)
        return ;
    std::queue<Node *> pqueue;
    pqueue.push(pRoot);
    Node *pNode = NULL;
    while(!pqueue.empty())
    {
        pNode = pqueue.front();
        printf("%d\t", pNode -> data);
        pqueue.pop();
        if(NULL != pNode -> left)
            pqueue.push(pNode -> left);
        if(NULL != pNode -> right)
            pqueue.push(pNode -> right);
    }
}

int main()
{
    Node *pRoot = NULL;
    createTree(&pRoot);
    if(pRoot == NULL)
        printf("pRoot NULL\n");
    midPrintTree(pRoot);
    printf("\n");
    midPrintTree_loop(pRoot);
    printf("\n");
    //
    prePrintTree(pRoot);
    printf("\n");
    prePrintTree_loop(pRoot);
    printf("\n");
    layerPrintTree(pRoot);
    printf("\n");
    freeTree(pRoot);
    return 0;
}

接下来要学习的是图的遍历创建等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值