• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

二叉树

武飞扬头像
Brain King
帮助1

目录

二叉树

一、二叉树概念及结构
    1.概念
    2.数据结构中的二叉树
    3.特殊的二叉树
    4.二叉树的存储结构
        4.1顺序存储
        4.2链式存储
    5.二叉树的性质
二、二叉树顺序结构及概念
    1.二叉树的顺序结构
    2.堆的概念及结构
    3.堆的实现
三、二叉树链式结构及实现
    1.二叉树链式结构的遍历
    2.二叉树的链式实现



 

 一、 二叉树概念及结构

1.概念

一棵二叉树是结点的一个有限集合,该集合或者为空,或者是由一个根节点加上两棵别称为左子树和右子树的二叉树组成。
二叉树的特点

  1. 每个结点最多有两棵子树,即二叉树不存在度大于2的结点。
  2. 二叉树的子树有左右之分,其子树的次序不能颠倒。

2.数据结构中的二叉树 

学新通

 3.特殊的二叉树

a.满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。

学新通
b.完全二叉树:完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一个一个地对应时称之为完全二叉树。 要注意的是满二叉树是一种特殊的完全二叉树。

学新通

4.二叉树的存储结构

4.1顺序存储

顺序结构存储就是使用数组来存储,一般使用数组只适合表示完全二叉树,因为不是完全二叉树会有空间的浪费。而现实中使用中只有堆才会使用数组来存储。二叉树顺序存储在物理上是一个数组,在逻辑上是一颗二叉树。

学新通

 4.2链式存储

二叉树的链式存储结构是指:用链表来表示一棵二叉树,即用链来指示元素的逻辑关系。 通常的方法是链表中每个结点由三个域组成,数据域和左右指针域,左右指针分别用来给出该结点左孩子和右孩子所在的链结点的存储地址 。链式结构又分为二叉链和三叉链.

学新通

  1.  
    // 二叉链
  2.  
    struct BinaryTreeNode
  3.  
    {
  4.  
    struct BinTreeNode* _pLeft; // 指向当前节点左孩子
  5.  
    struct BinTreeNode* _pRight; // 指向当前节点右孩子
  6.  
    BTDataType _data; // 当前节点值域
  7.  
    }
  8.  
    // 三叉链
  9.  
    struct BinaryTreeNode
  10.  
    {
  11.  
    struct BinTreeNode* _pParent; // 指向当前节点的双亲
  12.  
    struct BinTreeNode* _pLeft; // 指向当前节点左孩子
  13.  
    struct BinTreeNode* _pRight; // 指向当前节点右孩子
  14.  
    BTDataType _data; // 当前节点值域
  15.  
    };
学新通

  5.二叉树的性质

a.若规定根节点的层数为1,则一棵非空二叉树的第i层上最多有2^(i-1) 个结点.

b.若规定根节点的层数为1,则深度为h的二叉树的最大结点数是2^h- 1.

c.对任何一棵二叉树, 如果度为0其叶结点个数为 n0, 度为2的分支结点个数为 n2,则有n0=n2+1

d.若规定根节点的层数为1,具有n个结点的满二叉树的深度,h=Log2(n 1). (ps:Log2(n 1)是log以2为
底,n 1为对数)

e.对于具有n个结点的完全二叉树,如果按照从上至下从左至右的数组顺序对所有节点从0开始编号,则对于序号为i的结点有:

  (1) 若i>0,i位置节点的双亲序号:(i-1)/2;i=0,i为根节点编号,无双亲节点

(2) 若2i 1<n,左孩子序号:2i 1,2i 1>=n否则无左孩子

(3) 若2i 2<n,右孩子序号:2i 2,2i 2>=n否则无右孩子

二、二叉树顺序结构及概念

1.二叉树的顺序结构

普通的二叉树是不适合用数组来存储的,因为可能会存在大量的空间浪费。而完全二叉树更适合使用顺序结构存储。现实中我们通常把堆(一种二叉树)使用顺序结构的数组来存储,需要注意的是这里的堆和操作系统虚拟进程地址空间中的堆是两回事,一个是数据结构,一个是操作系统中管理内存的一块区域分段。

2.堆的概念及结构

如果有一个关键码的集合K = {k0,k1, k2,…,kn-1},把它的所有元素按完全二叉树的顺序存储方式存储在一个一维数组中,并满足:Ki <= K2i 1 且 Ki<= K2i 2 (Ki >= K2i 1 且 Ki >= K2i 2) i = 0,1,2…,则称为小堆(或大堆)。将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。

学新通

注意:

  • 堆中某个节点的值总是不大于或不小于其父节点的值;
  • 堆总是一棵完全二叉树。

3.堆的实现

  1.  
    typedef int HPDataType;
  2.  
    typedef struct Heap
  3.  
    {
  4.  
    HPDataType* _a;
  5.  
    int _size;
  6.  
    int _capacity;
  7.  
    }Heap;
  8.  
     
  9.  
    void swap(int *a, int *b);
  10.  
    void AdjustDown(int *a, int parent, int n);
  11.  
    void AdjustUp(int *a, int child, int n);
  12.  
     
  13.  
    // 堆的构建
  14.  
    void HeapCreate(Heap* hp, HPDataType* a, int n);
  15.  
    // 堆的销毁
  16.  
    void HeapDestory(Heap* hp);
  17.  
    // 堆的插入
  18.  
    void HeapPush(Heap* hp, HPDataType x);
  19.  
    // 堆的删除
  20.  
    void HeapPop(Heap* hp);
  21.  
    // 取堆顶的数据
  22.  
    HPDataType HeapTop(Heap* hp);
  23.  
    // 堆的数据个数
  24.  
    int HeapSize(Heap* hp);
  25.  
    // 堆的判空
  26.  
    int HeapEmpty(Heap* hp);
  27.  
     
  28.  
    // 对数组进行堆排序
  29.  
    void HeapSort(int* a, int n);
  30.  
     
  31.  
    void swap(int *a, int *b)
  32.  
    {
  33.  
    int tmp = *a;
  34.  
    *a = *b;
  35.  
    *b = tmp;
  36.  
    }
  37.  
     
  38.  
    void AdjustUp(int *a, int child, int n)
  39.  
    {
  40.  
    int parent = (child - 1) / 2;
  41.  
    while (child > 0)
  42.  
    {
  43.  
    if (a[child] > a[parent])
  44.  
    {
  45.  
    swap(&a[child], &a[parent]);
  46.  
    child = parent;
  47.  
    parent = (child - 1) / 2;
  48.  
    }
  49.  
    else
  50.  
    {
  51.  
    break;
  52.  
    }
  53.  
    }
  54.  
    }
  55.  
     
  56.  
    void AdjustDown(int *a, int parent,int n)
  57.  
    {
  58.  
    int child = parent * 2 1;
  59.  
    while ( child < n)
  60.  
    {
  61.  
    if (child 1 < n && a[child]<a[child 1])
  62.  
    {
  63.  
    child;
  64.  
    }
  65.  
    if(a[child]>a[parent])
  66.  
    {
  67.  
    swap(&a[child], &a[parent]);
  68.  
    parent = child;
  69.  
    child = (parent * 2) 1;
  70.  
    }
  71.  
    else
  72.  
    {
  73.  
    break;
  74.  
    }
  75.  
    }
  76.  
    }
  77.  
    void HeapCreate(Heap* hp, HPDataType* a, int n)
  78.  
    {
  79.  
    assert(hp);
  80.  
    hp->_a = (HPDataType*)malloc(sizeof(HPDataType)*n);
  81.  
    if (hp->_a == NULL)
  82.  
    {
  83.  
    printf("malloc fail");
  84.  
    exit(-1);
  85.  
    }
  86.  
    for (int i = 0; i < n; i)
  87.  
    {
  88.  
    hp->_a[i] = a[i];
  89.  
    }
  90.  
    hp->_size = hp->_capacity = n;
  91.  
    for (int i = (n - 2) / 2; i >= 0; --i)
  92.  
    {
  93.  
    AdjustDown(hp->_a,i, hp->_size);
  94.  
    }
  95.  
    }
  96.  
     
  97.  
    // 堆的销毁
  98.  
    void HeapDestory(Heap* hp)
  99.  
    {
  100.  
    assert(hp);
  101.  
    hp->_size = hp->_capacity = 0;
  102.  
    free(hp);
  103.  
    }
  104.  
    // 堆的插入
  105.  
    void HeapPush(Heap* hp, HPDataType x)
  106.  
    {
  107.  
    assert(hp);
  108.  
    if (hp->_size == hp->_capacity)
  109.  
    {
  110.  
    HPDataType* tmp = (HPDataType*)realloc(hp->_a, sizeof(HPDataType)* 2 * hp->_capacity);
  111.  
    if (tmp == NULL)
  112.  
    {
  113.  
    printf("realloc fail");
  114.  
    exit(-1);
  115.  
    }
  116.  
    hp->_a = tmp;
  117.  
    hp->_a[hp->_size] = x;
  118.  
    hp->_size;
  119.  
    hp->_capacity *= 2;
  120.  
    }
  121.  
    else
  122.  
    {
  123.  
    hp->_a[hp->_size] = x;
  124.  
    hp->_size;
  125.  
    }
  126.  
    AdjustUp(hp->_a, hp->_size-1, hp->_size);
  127.  
     
  128.  
    }
  129.  
    // 堆的删除
  130.  
    void HeapPop(Heap* hp)
  131.  
    {
  132.  
    assert(hp);
  133.  
    assert(hp->_size>0);
  134.  
    swap(&hp->_a[hp->_size-1], &hp->_a[0]);
  135.  
    --hp->_size;
  136.  
    AdjustDown(hp->_a, 0, hp->_size);
  137.  
    }
  138.  
    // 取堆顶的数据
  139.  
    HPDataType HeapTop(Heap* hp)
  140.  
    {
  141.  
    assert(hp);
  142.  
    assert(hp->_size>0);
  143.  
    return hp->_a[0];
  144.  
    }
  145.  
    // 堆的数据个数
  146.  
    int HeapSize(Heap* hp)
  147.  
    {
  148.  
    assert(hp);
  149.  
    return hp->_size;
  150.  
    }
  151.  
    // 堆的判空
  152.  
    int HeapEmpty(Heap* hp)
  153.  
    {
  154.  
    assert(hp);
  155.  
    return hp->_size == 0 ? 1 : 0;
  156.  
     
  157.  
    for (int i = 0; i < 3; i)}
  158.  
     
  159.  
    // 对数组进行堆排序
  160.  
    void HeapSort(int* a, int n)
  161.  
    {
  162.  
    assert(a);
  163.  
    for (int i = (n - 2) / 2; i >= 0; --i)
  164.  
    {
  165.  
    AdjustDown(a, i, n);
  166.  
    }
  167.  
    int end = n - 1;
  168.  
    while (end > 0)
  169.  
    {
  170.  
    swap(&a[0], &a[end]);
  171.  
    AdjustDown(a, 0, end);
  172.  
    --end;
  173.  
    }
  174.  
    }
学新通

三、二叉树链式结构及实现


1.二叉树链式结构的遍历

所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问。访问结点所做的操作依赖于具体的应用问 题。 遍历是二叉树上最重要的运算之一,是二叉树上进行其它运算之基础。
前序/中序/后序的递归结构遍历:是根据访问结点操作发生位置命名

前序遍历(Preorder Traversal 亦称先序遍历)——访问根结点的操作发生在遍历其左右子树之前。
中序遍历(Inorder Traversal)——访问根结点的操作发生在遍历其左右子树之中(间)。
后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后。
由于被访问的结点必是某子树的根,所以N(Node)、L(Left subtree)和R(Right subtree)又可解释为根、根的左子树和根的右子树。NLR、LNR和LRN分别又称为先根遍历、中根遍历和后根遍历。
层序遍历:除了先序遍历、中序遍历、后序遍历外,还可以对二叉树进行层序遍历。设二叉树的根节点所在层数为1,层序遍历就是从所在二叉树的根节点出发,首先访问第一层的树根节点,然后从左到右访问第2层上的节点,接着是第三层的节点,以此类推,自上而下,自左至右逐层访问树的结点的过程就是层序遍历。

2.二叉树的链式实现

  1.  
    typedef char BTDataType;
  2.  
     
  3.  
    typedef struct BinaryTreeNode
  4.  
    {
  5.  
    BTDataType _data;
  6.  
    struct BinaryTreeNode* _left;
  7.  
    struct BinaryTreeNode* _right;
  8.  
    }BTNode;
  9.  
     
  10.  
     
  11.  
    typedef BTNode* QDataType;
  12.  
    // 链式结构:表示队列
  13.  
    typedef struct QListNode
  14.  
    {
  15.  
    struct QListNode* _next;
  16.  
    QDataType _data;
  17.  
    }QNode;
  18.  
     
  19.  
    // 队列的结构
  20.  
    typedef struct Queue
  21.  
    {
  22.  
    QNode* _front;
  23.  
    QNode* _rear;
  24.  
    }Queue;
  25.  
     
  26.  
     
  27.  
    BTNode* CreateBTNode(BTDataType x);
  28.  
    // 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
  29.  
    BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
  30.  
    // 二叉树销毁
  31.  
    void BinaryTreeDestory(BTNode** root);
  32.  
    // 二叉树节点个数
  33.  
    int BinaryTreeSize(BTNode* root);
  34.  
    // 二叉树叶子节点个数
  35.  
    int BinaryTreeLeafSize(BTNode* root);
  36.  
    // 二叉树第k层节点个数
  37.  
    int BinaryTreeLevelKSize(BTNode* root, int k);
  38.  
    // 二叉树查找值为x的节点
  39.  
    BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
  40.  
    // 二叉树前序遍历
  41.  
    void BinaryTreePrevOrder(BTNode* root);
  42.  
    // 二叉树中序遍历
  43.  
    void BinaryTreeInOrder(BTNode* root);
  44.  
    // 二叉树后序遍历
  45.  
    void BinaryTreePostOrder(BTNode* root);
  46.  
     
  47.  
     
  48.  
     
  49.  
     
  50.  
     
  51.  
     
  52.  
     
  53.  
    // 初始化队列
  54.  
    void QueueInit(Queue* q);
  55.  
    // 队尾入队列
  56.  
    void QueuePush(Queue* q, QDataType data);
  57.  
    // 队头出队列
  58.  
    void QueuePop(Queue* q);
  59.  
    // 获取队列头部元素
  60.  
    QDataType QueueFront(Queue* q);
  61.  
    // 获取队列队尾元素
  62.  
    QDataType QueueBack(Queue* q);
  63.  
    // 获取队列中有效元素个数
  64.  
    int QueueSize(Queue* q);
  65.  
    // 检测队列是否为空,如果为空返回非零结果,如果非空返回0
  66.  
    int QueueEmpty(Queue* q);
  67.  
    // 销毁队列
  68.  
    void QueueDestroy(Queue* q);
  69.  
     
  70.  
     
  71.  
     
  72.  
    // 层序遍历
  73.  
    void BinaryTreeLevelOrder(BTNode* root);
  74.  
    // 判断二叉树是否是完全二叉树
  75.  
    int BinaryTreeComplete(BTNode* root);
  76.  
     
  77.  
    // 初始化队列
  78.  
    void QueueInit(Queue* q)
  79.  
    {
  80.  
    assert(q);
  81.  
    q->_front = q->_rear = NULL;
  82.  
    }
  83.  
    // 队尾入队列
  84.  
    void QueuePush(Queue* q, QDataType data)
  85.  
    {
  86.  
    assert(q);
  87.  
    QNode *newnode = ((QNode*)malloc(sizeof(QNode)));
  88.  
    newnode->_data = data;
  89.  
    newnode->_next = NULL;
  90.  
    if (q->_rear == NULL)
  91.  
    {
  92.  
    q->_front = q->_rear = newnode;
  93.  
    }
  94.  
    else
  95.  
    {
  96.  
    q->_rear->_next = newnode;
  97.  
    //q->_rear = q->_rear->_next;
  98.  
    q->_rear = newnode;
  99.  
    }
  100.  
    }
  101.  
    // 队头出队列
  102.  
    void QueuePop(Queue* q)
  103.  
    {
  104.  
    assert(q);
  105.  
    assert(!QueueEmpty(q));
  106.  
    if (q->_front == q->_rear)
  107.  
    {
  108.  
    free(q->_front);
  109.  
    //free(q->_rear);
  110.  
    q->_front = q->_rear = NULL;
  111.  
    }
  112.  
    else
  113.  
    {
  114.  
    QNode *cur = q->_front->_next;
  115.  
    free(q->_front);
  116.  
    q->_front = cur;
  117.  
    }
  118.  
    }
  119.  
    // 获取队列头部元素
  120.  
    QDataType QueueFront(Queue* q)
  121.  
    {
  122.  
    assert(q);
  123.  
    assert(!QueueEmpty(q));
  124.  
    return q->_front->_data;
  125.  
    }
  126.  
    // 获取队列队尾元素
  127.  
    QDataType QueueBack(Queue* q)
  128.  
    {
  129.  
    assert(q);
  130.  
    assert(!QueueEmpty(q));
  131.  
    return q->_rear->_data;
  132.  
    }
  133.  
    // 获取队列中有效元素个数
  134.  
    int QueueSize(Queue* q)
  135.  
    {
  136.  
    assert(q);
  137.  
    int size = 0;
  138.  
    QNode* cur = q->_front;
  139.  
    while (cur)
  140.  
    {
  141.  
    size;
  142.  
    cur = cur->_next;
  143.  
    }
  144.  
    return size;
  145.  
    }
  146.  
    // 检测队列是否为空,如果为空返回非零结果,如果非空返回0
  147.  
    int QueueEmpty(Queue* q)
  148.  
    {
  149.  
    assert(q);
  150.  
    return q->_front == NULL ? 1 : 0;
  151.  
    }
  152.  
    // 销毁队列
  153.  
    void QueueDestroy(Queue* q)
  154.  
    {
  155.  
    assert(q);
  156.  
    QNode *cur = q->_front;
  157.  
    while (cur)
  158.  
    {
  159.  
    QNode *next = cur->_next;
  160.  
    free(cur);
  161.  
    cur = next;
  162.  
    }
  163.  
    q->_front = q->_rear = NULL;
  164.  
    }
  165.  
     
  166.  
    BTNode* CreateBTNode(BTDataType x)
  167.  
    {
  168.  
    BTNode *node = (BTNode*)malloc(sizeof(BTNode));
  169.  
    node->_data = x;
  170.  
    node->_left = NULL;
  171.  
    node->_right = NULL;
  172.  
    return node;
  173.  
    }
  174.  
     
  175.  
     
  176.  
    // 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
  177.  
    BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
  178.  
    {
  179.  
    if (a[*pi] == '#')
  180.  
    {
  181.  
    return NULL;
  182.  
    }
  183.  
    BTNode *node = (BTNode*)malloc(sizeof(BTNode));
  184.  
    node->_data = a[*pi];
  185.  
    *pi;
  186.  
    node->_left = BinaryTreeCreate(a, n, pi);
  187.  
    *pi;
  188.  
    node->_right = BinaryTreeCreate(a, n, pi);
  189.  
    return node;
  190.  
    }
  191.  
    // 二叉树销毁
  192.  
    void BinaryTreeDestory(BTNode** root)
  193.  
    {
  194.  
    if (*root != NULL)
  195.  
    {
  196.  
    if ((*root)->_left) // 有左孩子
  197.  
    BinaryTreeDestory(&(*root)->_left); // 销毁左孩子子树
  198.  
    if ((*root)->_right) // 有右孩子
  199.  
    BinaryTreeDestory(&(*root)->_right); // 销毁右孩子子树
  200.  
     
  201.  
    free(*root); // 释放根结点
  202.  
    *root = NULL; // 空指针赋NULL
  203.  
    }
  204.  
    }
  205.  
    // 二叉树节点个数
  206.  
    int BinaryTreeSize(BTNode* root)
  207.  
    {
  208.  
    if (root == NULL)
  209.  
    {
  210.  
    return 0;
  211.  
    }
  212.  
    return BinaryTreeSize(root->_left) BinaryTreeSize(root->_right) 1;
  213.  
    }
  214.  
    // 二叉树叶子节点个数
  215.  
    int BinaryTreeLeafSize(BTNode* root)
  216.  
    {
  217.  
    if (root == NULL)
  218.  
    {
  219.  
    return 0;
  220.  
    }
  221.  
    if (root->_left == NULL&&root->_right == NULL)
  222.  
    {
  223.  
    return 1;
  224.  
    }
  225.  
    return BinaryTreeLeafSize(root->_left) BinaryTreeLeafSize(root->_right);
  226.  
    }
  227.  
    // 二叉树第k层节点个数
  228.  
    int BinaryTreeLevelKSize(BTNode* root, int k)
  229.  
    {
  230.  
    if (root == NULL)
  231.  
    {
  232.  
    return 0;
  233.  
    }
  234.  
    if (k == 1)
  235.  
    {
  236.  
    return 1;
  237.  
    }
  238.  
    return BinaryTreeLevelKSize(root->_left, k - 1) BinaryTreeLevelKSize(root->_right, k - 1);
  239.  
    }
  240.  
    // 二叉树查找值为x的节点
  241.  
    BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
  242.  
    {
  243.  
    if (root == NULL)
  244.  
    {
  245.  
    return NULL;
  246.  
    }
  247.  
    if (root->_data == x)
  248.  
    {
  249.  
    return root;
  250.  
    }
  251.  
    BTNode* ret=BinaryTreeFind(root->_left,x);
  252.  
    if (ret != NULL)
  253.  
    {
  254.  
    return ret;
  255.  
    }
  256.  
    ret = BinaryTreeFind(root->_right, x);
  257.  
    if (ret != NULL)
  258.  
    {
  259.  
    return ret;
  260.  
    }
  261.  
    return NULL;
  262.  
    }
  263.  
    // 二叉树前序遍历
  264.  
    void BinaryTreePrevOrder(BTNode* root)
  265.  
    {
  266.  
    if (root == NULL)
  267.  
    {
  268.  
    //printf("NULL ");
  269.  
    return;
  270.  
    }
  271.  
    printf("%c ", root->_data);
  272.  
    BinaryTreePrevOrder(root->_left);
  273.  
    BinaryTreePrevOrder(root->_right);
  274.  
    }
  275.  
    // 二叉树中序遍历
  276.  
    void BinaryTreeInOrder(BTNode* root)
  277.  
    {
  278.  
    if (root == NULL)
  279.  
    {
  280.  
    //printf("NULL ");
  281.  
    return;
  282.  
    }
  283.  
    BinaryTreeInOrder(root->_left);
  284.  
    printf("%c ", root->_data);
  285.  
    BinaryTreeInOrder(root->_right);
  286.  
    }
  287.  
    // 二叉树后序遍历
  288.  
    void BinaryTreePostOrder(BTNode* root)
  289.  
    {
  290.  
    if (root == NULL)
  291.  
    {
  292.  
    //printf("NULL ");
  293.  
    return;
  294.  
    }
  295.  
    BinaryTreePostOrder(root->_left);
  296.  
    BinaryTreePostOrder(root->_right);
  297.  
    printf("%c ", root->_data);
  298.  
    }
  299.  
    // 层序遍历
  300.  
    void BinaryTreeLevelOrder(BTNode* root)
  301.  
    {
  302.  
    Queue q;
  303.  
    QueueInit(&q);
  304.  
    if (root)
  305.  
    {
  306.  
    QueuePush(&q, root);
  307.  
    }
  308.  
    while (!QueueEmpty(&q))
  309.  
    {
  310.  
    BTNode *front = QueueFront(&q);
  311.  
    QueuePop(&q);
  312.  
    printf("%c ", front->_data);
  313.  
    if (front->_left)
  314.  
    {
  315.  
    QueuePush(&q, front->_left);
  316.  
    }
  317.  
    if (front->_right)
  318.  
    {
  319.  
    QueuePush(&q, front->_right);
  320.  
    }
  321.  
    }
  322.  
    }
  323.  
    // 判断二叉树是否是完全二叉树
  324.  
    int BinaryTreeComplete(BTNode* root)
  325.  
    {
  326.  
    Queue q;
  327.  
    QueueInit(&q);
  328.  
    if (root)
  329.  
    {
  330.  
    QueuePush(&q, root);
  331.  
    }
  332.  
    while (!QueueEmpty(&q))
  333.  
    {
  334.  
    BTNode *front = QueueFront(&q);
  335.  
    QueuePop(&q);
  336.  
    if (front == NULL)
  337.  
    {
  338.  
    break;
  339.  
    }
  340.  
    printf("%s ", front->_data);
  341.  
    if (front->_left)
  342.  
    {
  343.  
    QueuePush(&q, front->_left);
  344.  
    }
  345.  
    if (front->_right)
  346.  
    {
  347.  
    QueuePush(&q, front->_right);
  348.  
    }
  349.  
    }
  350.  
    while (!QueueEmpty(&q))
  351.  
    {
  352.  
    BTNode *front = QueueFront(&q);
  353.  
    QueuePop(&q);
  354.  
    if (front != NULL)
  355.  
    {
  356.  
    return 0;
  357.  
    }
  358.  
    }
  359.  
    return 1;
  360.  
     
  361.  
    }
  362.  
     
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgekfci
系列文章
更多 icon
同类精品
更多 icon
继续加载