了解满二叉树

    在本教程中,您将学习满二叉树及其不同的定理。此外,您还可以找到C中满二叉树的实例。
    满二叉树是一种特殊的二叉树,其中每个父节点/内部节点都有两个子节点或没有子节点。
    它也被称为真正的二叉树。
在这里插入图片描述

满二叉树理论
假设,
i = 内部节点的数量
n = 全部节点的数量
l = 叶子数量
λ = 级别数量
  1. 叶子数量是 i + 1。
  2. 节点总数是 2i + 1。
  3. 内部节点的数量是 ( n - 1 ) / 2。
  4. 叶子数是 ( n + 1 ) / 2。
  5. 节点总数为 2l - 1。
  6. 内部节点数为 l - 1。
  7. 叶子数量最多为 2λ - 1。
C示例
// Checking if a binary tree is a full binary tree in C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

struct Node {
    
    
  int item;
  struct Node *left, *right;
};

// Creation of new Node
struct Node *createNewNode(char k) {
    
    
  struct Node *node = (struct Node *)malloc(sizeof(struct Node));
  node->item = k;
  node->right = node->left = NULL;
  return node;
}

bool isFullBinaryTree(struct Node *root) {
    
    
  // Checking tree emptiness
  if (root == NULL)
    return true;

  // Checking the presence of children
  if (root->left == NULL && root->right == NULL)
    return true;

  if ((root->left) && (root->right))
    return (isFullBinaryTree(root->left) && isFullBinaryTree(root->right));

  return false;
}

int main() {
    
    
  struct Node *root = NULL;
  root = createNewNode(1);
  root->left = createNewNode(2);
  root->right = createNewNode(3);

  root->left->left = createNewNode(4);
  root->left->right = createNewNode(5);
  root->left->right->left = createNewNode(6);
  root->left->right->right = createNewNode(7);

  if (isFullBinaryTree(root))
    printf("The tree is a full binary tree\n");
  else
    printf("The tree is not a full binary full\n");
}
参考文档

[1]Parewa Labs Pvt. Ltd.Full Binary Tree[EB/OL].https://www.programiz.com/dsa/full-binary-tree,2020-01-01.

猜你喜欢

转载自blog.csdn.net/zsx0728/article/details/114234481