原文: https://www.programiz.com/dsa/full-binary-tree

在本教程中,您将学习满二叉树及其不同的定理。 此外,您还将找到一些工作示例,以检查 C,C++ ,Java 和 Python 中的完整二进制树。

满二叉树是二叉树的一种特殊类型,其中每个父节点/内部节点都有两个或没有子节点。

也称为适当的二叉树。

满二叉树 - 图1

满二叉树


满二叉树定理

  1. Let, i = the number of internal nodes
  2. n = be the total number of nodes
  3. l = number of leaves
  4. λ = number of levels
  1. 叶子数为i + 1
  2. 节点总数为2i + 1
  3. 内部节点数为(n – 1) / 2
  4. 叶子数为(n + 1) / 2
  5. 节点总数为2l – 1
  6. 内部节点数为l – 1
  7. 叶子数最多为2^(λ - 1)

Python,Java 和 C/C++ 示例

以下代码用于检查树是否为满二叉树。

  1. # Checking if a binary tree is a full binary tree in Python
  2. # Creating a node
  3. class Node:
  4. def __init__(self, item):
  5. self.item = item
  6. self.leftChild = None
  7. self.rightChild = None
  8. # Checking full binary tree
  9. def isFullTree(root):
  10. # Tree empty case
  11. if root is None:
  12. return True
  13. # Checking whether child is present
  14. if root.leftChild is None and root.rightChild is None:
  15. return True
  16. if root.leftChild is not None and root.rightChild is not None:
  17. return (isFullTree(root.leftChild) and isFullTree(root.rightChild))
  18. return False
  19. root = Node(1)
  20. root.rightChild = Node(3)
  21. root.leftChild = Node(2)
  22. root.leftChild.leftChild = Node(4)
  23. root.leftChild.rightChild = Node(5)
  24. root.rightChild.leftChild.leftChild = Node(6)
  25. root.rightChild.leftChild.rightChild = Node(7)
  26. if isFullTree(root):
  27. print("The tree is a full binary tree")
  28. else:
  29. print("The tree is not a full binary full")
  1. // Checking if a binary tree is a full binary tree in Java
  2. class Node {
  3. int data;
  4. Node leftChild, rightChild;
  5. Node(int item) {
  6. data = item;
  7. leftChild = rightChild = null;
  8. }
  9. }
  10. class BinaryTree {
  11. Node root;
  12. // Check for Full Binary Tree
  13. boolean isFullBinaryTree(Node node) {
  14. // Checking tree emptiness
  15. if (node == null)
  16. return true;
  17. // Checking the children
  18. if (node.leftChild == null && node.rightChild == null)
  19. return true;
  20. if ((node.leftChild != null) && (node.rightChild != null))
  21. return (isFullBinaryTree(node.leftChild) && isFullBinaryTree(node.rightChild));
  22. return false;
  23. }
  24. public static void main(String args[]) {
  25. BinaryTree tree = new BinaryTree();
  26. tree.root = new Node(1);
  27. tree.root.leftChild = new Node(2);
  28. tree.root.rightChild = new Node(3);
  29. tree.root.leftChild.leftChild = new Node(4);
  30. tree.root.leftChild.rightChild = new Node(5);
  31. tree.root.rightChild.leftChild = new Node(6);
  32. tree.root.rightChild.rightChild = new Node(7);
  33. if (tree.isFullBinaryTree(tree.root))
  34. System.out.print("The tree is a full binary tree");
  35. else
  36. System.out.print("The tree is not a full binary tree");
  37. }
  38. }
  1. // Checking if a binary tree is a full binary tree in C
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. struct Node {
  6. int item;
  7. struct Node *left, *right;
  8. };
  9. // Creation of new Node
  10. struct Node *createNewNode(char k) {
  11. struct Node *node = (struct Node *)malloc(sizeof(struct Node));
  12. node->item = k;
  13. node->right = node->left = NULL;
  14. return node;
  15. }
  16. bool isFullBinaryTree(struct Node *root) {
  17. // Checking tree emptiness
  18. if (root == NULL)
  19. return true;
  20. // Checking the presence of children
  21. if (root->left == NULL && root->right == NULL)
  22. return true;
  23. if ((root->left) && (root->right))
  24. return (isFullBinaryTree(root->left) && isFullBinaryTree(root->right));
  25. return false;
  26. }
  27. int main() {
  28. struct Node *root = NULL;
  29. root = createNewNode(1);
  30. root->left = createNewNode(2);
  31. root->right = createNewNode(3);
  32. root->left->left = createNewNode(4);
  33. root->left->right = createNewNode(5);
  34. root->right->left->left = createNewNode(6);
  35. root->right->left->right = createNewNode(7);
  36. if (isFullBinaryTree(root))
  37. printf("The tree is a full binary tree\n");
  38. else
  39. printf("The tree is not a full binary full\n");
  40. }
  1. // Checking if a binary tree is a full binary tree in C++
  2. #include <iostream>
  3. using namespace std;
  4. struct Node {
  5. int key;
  6. struct Node *left, *right;
  7. };
  8. // New node creation
  9. struct Node *newNode(char k) {
  10. struct Node *node = (struct Node *)malloc(sizeof(struct Node));
  11. node->key = k;
  12. node->right = node->left = NULL;
  13. return node;
  14. }
  15. bool isFullBinaryTree(struct Node *root) {
  16. // Checking for emptiness
  17. if (root == NULL)
  18. return true;
  19. // Checking for the presence of children
  20. if (root->left == NULL && root->right == NULL)
  21. return true;
  22. if ((root->left) && (root->right))
  23. return (isFullBinaryTree(root->left) && isFullBinaryTree(root->right));
  24. return false;
  25. }
  26. int main() {
  27. struct Node *root = NULL;
  28. root = newNode(1);
  29. root->left = newNode(2);
  30. root->right = newNode(3);
  31. root->left->left = newNode(4);
  32. root->left->right = newNode(5);
  33. root->right->left->left = newNode(6);
  34. root->right->left->right = newNode(7);
  35. if (isFullBinaryTree(root))
  36. cout << "The tree is a full binary tree\n";
  37. else
  38. cout << "The tree is not a full binary full\n";
  39. }