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

在本教程中,您将学习完美二叉树。 此外,您还将找到一些用于检查 C,C++ ,Java 和 Python 中完美二叉树的示例。

完美二叉树是一种二叉树,其中每个内部节点恰好有两个子节点,而所有叶节点都处于同一级别。

完美二叉树 - 图1

完美二叉树

所有内部节点的度数均为 2。

递归地,可以将完美二叉树定义为:

  1. 如果单个节点没有子节点,则它是高度为h = 0的完美二叉树,
  2. 如果节点具有h > 0,则如果其两个子树的高度均为h - 1并且不重叠,则它是理想的二叉树。

完美二叉树 - 图2

完美二叉树(递归表示)


Python,Java 和 C/C++ 示例

以下代码用于检查树是否是完美二叉树。

  1. # Checking if a binary tree is a perfect binary tree in Python
  2. class newNode:
  3. def __init__(self, k):
  4. self.key = k
  5. self.right = self.left = None
  6. # Calculate the depth
  7. def calculateDepth(node):
  8. d = 0
  9. while (node is not None):
  10. d += 1
  11. node = node.left
  12. return d
  13. # Check if the tree is perfect binary tree
  14. def is_perfect(root, d, level=0):
  15. # Check if the tree is empty
  16. if (root is None):
  17. return True
  18. # Check the presence of trees
  19. if (root.left is None and root.right is None):
  20. return (d == level + 1)
  21. if (root.left is None or root.right is None):
  22. return False
  23. return (is_perfect(root.left, d, level + 1) and
  24. is_perfect(root.right, d, level + 1))
  25. root = None
  26. root = newNode(1)
  27. root.left = newNode(2)
  28. root.right = newNode(3)
  29. root.left.left = newNode(4)
  30. root.left.right = newNode(5)
  31. if (is_perfect(root, calculateDepth(root))):
  32. print("The tree is a perfect binary tree")
  33. else:
  34. print("The tree is not a perfect binary tree")
  1. // Checking if a binary tree is a perfect binary tree in Java
  2. class PerfectBinaryTree {
  3. static class Node {
  4. int key;
  5. Node left, right;
  6. }
  7. // Calculate the depth
  8. static int depth(Node node) {
  9. int d = 0;
  10. while (node != null) {
  11. d++;
  12. node = node.left;
  13. }
  14. return d;
  15. }
  16. // Check if the tree is perfect binary tree
  17. static boolean is_perfect(Node root, int d, int level) {
  18. // Check if the tree is empty
  19. if (root == null)
  20. return true;
  21. // If for children
  22. if (root.left == null && root.right == null)
  23. return (d == level + 1);
  24. if (root.left == null || root.right == null)
  25. return false;
  26. return is_perfect(root.left, d, level + 1) && is_perfect(root.right, d, level + 1);
  27. }
  28. // Wrapper function
  29. static boolean is_Perfect(Node root) {
  30. int d = depth(root);
  31. return is_perfect(root, d, 0);
  32. }
  33. // Create a new node
  34. static Node newNode(int k) {
  35. Node node = new Node();
  36. node.key = k;
  37. node.right = null;
  38. node.left = null;
  39. return node;
  40. }
  41. public static void main(String args[]) {
  42. Node root = null;
  43. root = newNode(1);
  44. root.left = newNode(2);
  45. root.right = newNode(3);
  46. root.left.left = newNode(4);
  47. root.left.right = newNode(5);
  48. if (is_Perfect(root) == true)
  49. System.out.println("The tree is a perfect binary tree");
  50. else
  51. System.out.println("The tree is not a perfect binary tree");
  52. }
  53. }
  1. // Checking if a binary tree is a perfect binary tree in C
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. struct node {
  6. int data;
  7. struct node *left;
  8. struct node *right;
  9. };
  10. // Creating a new node
  11. struct node *newnode(int data) {
  12. struct node *node = (struct node *)malloc(sizeof(struct node));
  13. node->data = data;
  14. node->left = NULL;
  15. node->right = NULL;
  16. return (node);
  17. }
  18. // Calculate the depth
  19. int depth(struct node *node) {
  20. int d = 0;
  21. while (node != NULL) {
  22. d++;
  23. node = node->left;
  24. }
  25. return d;
  26. }
  27. // Check if the tree is perfect
  28. bool is_perfect(struct node *root, int d, int level) {
  29. // Check if the tree is empty
  30. if (root == NULL)
  31. return true;
  32. // Check the presence of children
  33. if (root->left == NULL && root->right == NULL)
  34. return (d == level + 1);
  35. if (root->left == NULL || root->right == NULL)
  36. return false;
  37. return is_perfect(root->left, d, level + 1) &&
  38. is_perfect(root->right, d, level + 1);
  39. }
  40. // Wrapper function
  41. bool is_Perfect(struct node *root) {
  42. int d = depth(root);
  43. return is_perfect(root, d, 0);
  44. }
  45. int main() {
  46. struct node *root = NULL;
  47. root = newnode(1);
  48. root->left = newnode(2);
  49. root->right = newnode(3);
  50. root->left->left = newnode(4);
  51. root->left->right = newnode(5);
  52. root->right->left = newnode(6);
  53. if (is_Perfect(root))
  54. printf("The tree is a perfect binary tree\n");
  55. else
  56. printf("The tree is not a perfect binary tree\n");
  57. }
  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->left->left->left = newNode(6);
  34. root->left->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. }

完美二叉树定理

  1. 高度为h的完美二叉树具有2^(h+1) – 1节点。
  2. 具有n个节点的理想二叉树的高度为log(n + 1) – 1 = Θ(ln(n))
  3. 高度为h的理想二叉树具有2^h叶节点。
  4. 完美二叉树中节点的平均深度为Θ(ln(n))