Binary tree二叉树

二叉树定义、构建、先中后遍历、层次遍历、树深度

#include "stdafx.h"
#include <iostream>
using namespace std;
#define maxSize 100
typedef struct BTNode
{
    char data;
    struct BTNode *lchild;
    struct BTNode *rchild;
}BTNode;


BTNode* newNode(char value)
{
    // Allocate memory for new node 
    BTNode* node = (BTNode*)malloc(sizeof(BTNode));

    // Assign data to this node
    node->data = value;

    // Initialize left and right children as NULL
    node->lchild = NULL;
    node->rchild = NULL;
    return(node);
}

void visit(BTNode* node)
{
    printf("%c",node->data);// %c 输出字符型变量
}

void preorder(BTNode* p)
{
    if (p!= NULL)
    {
        visit(p);
        preorder(p->lchild);
        preorder(p->rchild);
    }
}


void inorder(BTNode *p)
{
    if (p != NULL)
    {
        inorder(p->lchild);
        visit(p);
        inorder(p->rchild); 
    }
}

void postorder(BTNode *p)
{
    if (p != NULL)
    {
        postorder(p->lchild);
        postorder(p->rchild);
        visit(p);
    }
}

void levelorder(BTNode *p)
{
    int front, rear;
    BTNode *que[maxSize];
    front = rear = 0;
    BTNode*q;
    if (p != NULL)
    {
        rear = (rear + 1) % maxSize;
        que[rear] = p;
        while (front!=rear)
        {
            front = (front + 1) % maxSize;
            q = que[front];
            visit(q);
            if (q->lchild != NULL)
            {
                rear = (rear + 1) % maxSize;
                que[rear] = q->lchild;
            }
            if (q->rchild != NULL)
            {
                rear = (rear + 1) % maxSize;
                que[rear] = q->rchild;
            }
        }
    }
}

int getDepth(BTNode *p)
{
    int LD, RD;
    if (p == NULL)
        return 0;
    else
    {
        LD = getDepth(p->lchild);
        RD = getDepth(p->rchild);
        return (LD > RD ? LD : RD) + 1;
    }
}

int main()
{
    /*create root*/
    BTNode* root = newNode('A');
    /* following is the tree after above statement

    A
    /   \
    NULL  NULL
    */


    root->lchild = newNode('B');
    root->rchild = newNode('C');
    /* 2 and 3 become left and right children of 1
    A
    /   \
    B      C
    /    \    /  \
    NULL NULL NULL NULL
    */


    root->lchild->lchild = newNode('D');
    root->rchild->lchild = newNode('E');
    root->rchild->rchild = newNode('F');
    /* 4 becomes left child of 2
          A
    /           \
    B               C
    /   \       /       \
    D  NULL     E         F
    / \         / \      / \
    NULL NULL NULL NULL NULL NULL
    */

    //getchar();
    preorder(root);     //先序遍历:ABDCEF
    printf("\n");
    inorder(root);      //中序遍历:DBAECF
    printf("\n");
    postorder(root);    //后序遍历:DBEFCA
    printf("\n");
    printf("%d", getDepth(root));//二叉树深度:3
    printf("\n");
    levelorder(root);   //层次遍历:ABCDEF
    system("pause");

    return 0;
}

【Tips】:字符型变量的负值应用单引号-> ‘ ’;
输出字符printf(“%c”,yourchar);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值