新手入门——二叉树

满二叉树:每个结点都满

完全二叉树:具有满二叉树的部分性质,满足至少有左边的结点

 

 

某一层的结点数:Nlevel - 1(N表示N叉树)

某一深度结点数:Nh - 1

 

 

一般二叉树性质

1、在非空二叉树的i层上,至多有2i-1个节点(i>=1)。通过归纳法论证。

2、在深度为K的二叉树上最多有2k-1个结点(k>=1)。通过归纳法论证。

3、对于任何一棵非空的二叉树,如果叶节点个数为n0,度数为2的节点个数为n2,则有: n0 = n2 + 1

在一棵二叉树中,除了叶子结点(度为0)之外,就剩下度为2(n2)和1(n1)的结点了。则树的结点总数为T = n0+n1+n2;在二叉树中结点总数为T,而连线数为T-1.所以有:n0+n1+n2-1 = 2*n2 +n1;最后得到n0 = n2+1;

 

完全二叉树性质

如果有一颗有n个节点的完全二叉树的节点按层次序编号,对任一层的节点i(1<=i<=n)有

    1.如果i=1,则节点是二叉树的根,无双亲,如果i>1,则其双亲节点为[i/2],向下取整

    2.如果2i>n那么节点i没有左孩子,否则其左孩子为2i

    3.如果2i+1>n那么节点没有右孩子,否则右孩子为2i+1

 

代码部分:

1.二叉树的构建:

typedef struct node
{
    char key;
    struct node *left;
    struct node *right;
}tree;

void CreatTree(tree *&t)
{
    char ch;
    cin >> ch;
    if(ch != '#')
    {
        t = new tree;
        t->key = ch;
        CreatTree(t->left);
        CreatTree(t->right);
    }
    else
    {
        t = nullptr;
    }
}

2.二叉树的前序遍历

void PerOrder(tree *p)
{
    if(p != nullptr)
    {
        cout << p->key << " ";
        PerOrder(p->left);
        PerOrder(p->right);
    }
}

3.二叉树的中序遍历

void InOrder(tree *p)
{
    if(p != nullptr)
    {
        InOrder(p->left);
        cout << p->key << " ";
        InOrder(p->right);
    }
}

4.二叉树的后序遍历

void PosOrder(tree *p)
{
    if(p != nullptr)
    {
        PosOrder(p->left);
        PosOrder(p->right);
        cout << p->key << " ";
    }
}

5.二叉树的层序遍历

void LevelOrder(tree *p)
{
    queue<tree*> q;
    q.push(p);
    while(!q.empty())
    {
        tree *t = q.front();
        cout << t->key << " ";
        q.pop();
        if(t->left)
        {
            q.push(t->left);
        }
        if(t->right)
        {
            q.push(t->right);
        }
    }
}

6.二叉树的深度优先遍历

void DFS(tree *p)
{
    stack<tree*> q;
    q.push(p);
    while(!q.empty())
    {
        tree *t = q.top();
        cout << t->key << " ";
        q.pop();
        if(t->right)
        {
            q.push(t->right);
        }
        if(t->left)
        {
            q.push(t->left);
        }
    }
}

完整代码:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1005;


typedef struct node
{
    char key;
    struct node *left;
    struct node *right;
}tree;

void CreatTree(tree *&t)
{
    char ch;
    cin >> ch;
    if(ch != '#')
    {
        t = new tree;
        t->key = ch;
        CreatTree(t->left);
        CreatTree(t->right);
    }
    else
    {
        t = nullptr;
    }
}


void PerOrder(tree *p)
{
    if(p != nullptr)
    {
        cout << p->key << " ";
        PerOrder(p->left);
        PerOrder(p->right);
    }
}

void InOrder(tree *p)
{
    if(p != nullptr)
    {
        InOrder(p->left);
        cout << p->key << " ";
        InOrder(p->right);
    }
}

void PosOrder(tree *p)
{
    if(p != nullptr)
    {
        PosOrder(p->left);
        PosOrder(p->right);
        cout << p->key << " ";
    }
}

void LevelOrder(tree *p)
{
    queue<tree*> q;
    q.push(p);
    while(!q.empty())
    {
        tree *t = q.front();
        cout << t->key << " ";
        q.pop();
        if(t->left)
        {
            q.push(t->left);
        }
        if(t->right)
        {
            q.push(t->right);
        }
    }
}


void DFS(tree *p)
{
    stack<tree*> q;
    q.push(p);
    while(!q.empty())
    {
        tree *t = q.top();
        cout << t->key << " ";
        q.pop();
        if(t->right)
        {
            q.push(t->right);
        }
        if(t->left)
        {
            q.push(t->left);
        }
    }
}

//测试样例
//ABCD###EF##G###

int main()
{
    tree *r;
    CreatTree(r);
    cout << "先序遍历的结果:" << endl;
    PerOrder(r);
    cout << endl;
    cout << "中序遍历的结果:" << endl;
    InOrder(r);
    cout << endl;
    cout << "后序遍历的结果:" << endl;
    PosOrder(r);
    cout << endl;
    cout << "层序遍历的结果:" << endl;
    LevelOrder(r);
    cout << endl;
    cout << "深度优先遍历的结果:" << endl;
    DFS(r);
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值