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

在本教程中,您将了解平衡的二叉树及其不同类型。 此外,您还将在 C,C++ ,Java 和 Python 中找到平衡二叉树的工作示例。

平衡二叉树(也称为高度平衡二叉树)定义为二叉树,其中任何节点的左和右子树的高度相差不超过 1。

要了解有关树/节点的高度的更多信息,请访问树数据结构。以下是高度平衡的二叉树的条件:

  1. 任何节点的左和右子树之间的差异不超过一个
  2. 左子树是平衡的
  3. 右子树是平衡的

平衡二叉树 - 图1

每个级别都有深度的平衡二叉树

平衡二叉树 - 图2

每个级别都有深度的不平衡二叉树


Python,Java 和 C/C++ 示例

以下代码用于检查树是否高度平衡。

  1. # Checking if a binary tree is CalculateHeight balanced in Python
  2. # CreateNode creation
  3. class CreateNode:
  4. def __init__(self, item):
  5. self.item = item
  6. self.left = self.right = None
  7. # Calculate height
  8. class CalculateHeight:
  9. def __init__(self):
  10. self.CalculateHeight = 0
  11. # Check height balance
  12. def is_height_balanced(root, CalculateHeight):
  13. left_height = CalculateHeight()
  14. right_height = CalculateHeight()
  15. if root is None:
  16. return True
  17. l = is_height_balanced(root.left, left_height)
  18. r = is_height_balanced(root.right, right_height)
  19. CalculateHeight.CalculateHeight = max(
  20. left_height.CalculateHeight, right_height.CalculateHeight) + 1
  21. if abs(left_height.CalculateHeight - right_height.CalculateHeight) <= 1:
  22. return l and r
  23. return False
  24. CalculateHeight = CalculateHeight()
  25. root = CreateNode(1)
  26. root.left = CreateNode(2)
  27. root.right = CreateNode(3)
  28. root.left.left = CreateNode(4)
  29. root.left.right = CreateNode(5)
  30. if is_height_balanced(root, CalculateHeight):
  31. print('The tree is balanced')
  32. else:
  33. print('The tree is not balanced')
  1. // Checking if a binary tree is height balanced in Java
  2. // Node creation
  3. class Node {
  4. int data;
  5. Node left, right;
  6. Node(int d) {
  7. data = d;
  8. left = right = null;
  9. }
  10. }
  11. // Calculate height
  12. class Height {
  13. int height = 0;
  14. }
  15. class BinaryTree {
  16. Node root;
  17. // Check height balance
  18. boolean checkHeightBalance(Node root, Height height) {
  19. // Check for emptiness
  20. if (root == null) {
  21. height.height = 0;
  22. return true;
  23. }
  24. Height leftHeighteight = new Height(), rightHeighteight = new Height();
  25. boolean l = checkHeightBalance(root.left, leftHeighteight);
  26. boolean r = checkHeightBalance(root.right, rightHeighteight);
  27. int leftHeight = leftHeighteight.height, rightHeight = rightHeighteight.height;
  28. height.height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
  29. if ((leftHeight - rightHeight >= 2) || (rightHeight - leftHeight >= 2))
  30. return false;
  31. else
  32. return l && r;
  33. }
  34. public static void main(String args[]) {
  35. Height height = new Height();
  36. BinaryTree tree = new BinaryTree();
  37. tree.root = new Node(1);
  38. tree.root.left = new Node(2);
  39. tree.root.right = new Node(3);
  40. tree.root.left.left = new Node(4);
  41. tree.root.left.right = new Node(5);
  42. if (tree.checkHeightBalance(tree.root, height))
  43. System.out.println("The tree is balanced");
  44. else
  45. System.out.println("The tree is not balanced");
  46. }
  47. }
  1. // Checking if a binary tree is height balanced in C
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define bool int
  5. // Node creation
  6. struct node {
  7. int item;
  8. struct node *left;
  9. struct node *right;
  10. };
  11. // Create a new node
  12. struct node *newNode(int item) {
  13. struct node *node = (struct node *)malloc(sizeof(struct node));
  14. node->item = item;
  15. node->left = NULL;
  16. node->right = NULL;
  17. return (node);
  18. }
  19. // Check for height balance
  20. bool checkHeightBalance(struct node *root, int *height) {
  21. // Check for emptiness
  22. int leftHeight = 0, rightHeight = 0;
  23. int l = 0, r = 0;
  24. if (root == NULL) {
  25. *height = 0;
  26. return 1;
  27. }
  28. l = checkHeightBalance(root->left, &leftHeight);
  29. r = checkHeightBalance(root->right, &rightHeight);
  30. *height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
  31. if ((leftHeight - rightHeight >= 2) || (rightHeight - leftHeight >= 2))
  32. return 0;
  33. else
  34. return l && r;
  35. }
  36. int main() {
  37. int height = 0;
  38. struct node *root = newNode(1);
  39. root->left = newNode(2);
  40. root->right = newNode(3);
  41. root->left->left = newNode(4);
  42. root->left->right = newNode(5);
  43. if (checkHeightBalance(root, &height))
  44. printf("The tree is balanced");
  45. else
  46. printf("The tree is not balanced");
  47. }
  1. // Checking if a binary tree is height balanced in C++
  2. #include <iostream>
  3. using namespace std;
  4. #define bool int
  5. class node {
  6. public:
  7. int item;
  8. node *left;
  9. node *right;
  10. };
  11. // Create anew node
  12. node *newNode(int item) {
  13. node *Node = new node();
  14. Node->item = item;
  15. Node->left = NULL;
  16. Node->right = NULL;
  17. return (Node);
  18. }
  19. // Check height balance
  20. bool checkHeightBalance(node *root, int *height) {
  21. // Check for emptiness
  22. int leftHeight = 0, rightHeight = 0;
  23. int l = 0, r = 0;
  24. if (root == NULL) {
  25. *height = 0;
  26. return 1;
  27. }
  28. l = checkHeightBalance(root->left, &leftHeight);
  29. r = checkHeightBalance(root->right, &rightHeight);
  30. *height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
  31. if ((leftHeight - rightHeight >= 2) || (rightHeight - leftHeight >= 2))
  32. return 0;
  33. else
  34. return l && r;
  35. }
  36. int main() {
  37. int height = 0;
  38. node *root = newNode(1);
  39. root->left = newNode(2);
  40. root->right = newNode(3);
  41. root->left->left = newNode(4);
  42. root->left->right = newNode(5);
  43. if (checkHeightBalance(root, &height))
  44. cout << "The tree is balanced";
  45. else
  46. cout << "The tree is not balanced";
  47. }

平衡二叉树应用