Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Challenge: Balanced Trees | Graphs
Algorithms and Data Structures Overview
セクション 4.  6
single

single

bookChallenge: Balanced Trees

メニューを表示するにはスワイプしてください

A balanced tree, specifically a balanced Binary Search Tree (BST), is a tree data structure in which the heights of any node's left and right subtrees differ by at most one. This balance ensures efficient searching, insertion, and deletion operations, maintaining a logarithmic time complexity for these operations. In a balanced BST, the tree's height is minimized, leading to optimal performance.
Let's look at the example of unbalanced tree:

In the provided BST, the left subtree of the root node has a height of 4, while the right subtree has a height of only 2. This significant difference in heights indicates that the tree is unbalanced, leading to inefficient search, insertion, and deletion operations compared to a balanced BST.

タスク

スワイプしてコーディングを開始

You are provided with a Binary Search Tree (BST) implemented in Python. Your task is to write a function to determine whether the BST is balanced or not.
You have to perform the following steps:

  • calculate the height of a subtree rooted at a given node recursively. It returns the maximum height between the left and right subtrees plus one (to account for the current node). This logic is implemented in calculate_height() function.
  • Recursively check if the subtree rooted at a given node is balanced. It is implemented using the is_balanced_recursive() function. It compares the heights of the left and right subtrees and returns False if the difference in heights is greater than 1, indicating an imbalance.
  • To check if the tree is balanced, you must call the is_balanced_recursive() with the tree root as an argument.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  6
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt