-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,60 @@ | ||
""" | ||
Problem: | ||
Given a binary tree, determine whether or not it is height-balanced. | ||
A height-balanced binary tree can be defined as one in which the heights of the two subtrees of any node never differ by more than one. | ||
Given a binary tree, determine whether or not it is height-balanced. A height-balanced | ||
binary tree can be defined as one in which the heights of the two subtrees of any node | ||
never differ by more than one. | ||
""" | ||
|
||
from DataStructures.Tree import Node, Binary_Tree | ||
from typing import Tuple | ||
|
||
from .DataStructures.Tree import Node, BinaryTree | ||
|
||
def height_helper(self): | ||
# generating the left and right heights and the balance | ||
if self.left == None: | ||
|
||
def height_helper(node: Node) -> Tuple[int, bool]: | ||
if node.left is None: | ||
left_height, balance_left = 0, True | ||
else: | ||
left_height, balance_left = self.left.height_helper() | ||
if self.right == None: | ||
left_height, balance_left = height_helper(node.left) | ||
if node.right is None: | ||
right_height, balance_right = 0, True | ||
else: | ||
right_height, balance_right = self.right.height_helper() | ||
# returning the current balance factor and the balance check | ||
right_height, balance_right = height_helper(node.right) | ||
|
||
balance = balance_left and balance_right | ||
current_balance = -1 <= (right_height - left_height) <= 1 | ||
height = max(left_height, right_height) + 1 | ||
return height, balance and current_balance | ||
|
||
|
||
def check_balance(self): | ||
_, balance = self.root.height_helper() | ||
def check_balance(tree: BinaryTree) -> bool: | ||
if tree.root is None: | ||
return True | ||
_, balance = height_helper(tree.root) | ||
return balance | ||
|
||
|
||
# adding required methods to the classes | ||
setattr(Node, "height_helper", height_helper) | ||
setattr(Binary_Tree, "check_balance", check_balance) | ||
if __name__ == "__main__": | ||
tree = BinaryTree() | ||
|
||
tree.root = Node(0) | ||
|
||
tree.root.left = Node(1) | ||
tree.root.right = Node(2) | ||
|
||
tree.root.left.left = Node(3) | ||
tree.root.left.right = Node(4) | ||
|
||
# DRIVER CODE | ||
tree = Binary_Tree() | ||
print(check_balance(tree)) | ||
|
||
tree.root = Node(0) | ||
tree.root.left = Node(1) | ||
tree.root.right = Node(2) | ||
tree.root.left.left = Node(3) | ||
tree.root.left.right = Node(4) | ||
tree.root.left.right.left = Node(5) | ||
|
||
print(tree.check_balance()) | ||
print(check_balance(tree)) | ||
|
||
tree.root.left.right.left = Node(5) | ||
|
||
print(tree.check_balance()) | ||
""" | ||
SPECS: | ||
TIME COMPLEXITY: O(n) | ||
SPACE COMPLEXITY: O(log(n)) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
""" | ||
Problem: | ||
Find the maximum of two numbers without using any if-else statements, branching, or direct comparisons. | ||
Find the maximum of two numbers without using any if-else statements, branching, or | ||
direct comparisons. | ||
""" | ||
|
||
# max using bit-wise operations | ||
def get_max(num1, num2): | ||
|
||
def get_max(num1: int, num2: int) -> int: | ||
return num1 ^ ((num1 ^ num2) & -(num1 < num2)) | ||
|
||
|
||
# DRIVER CODE | ||
print(get_max(1, 5)) | ||
print(get_max(4, 3)) | ||
print(get_max(-3, 6)) | ||
print(get_max(5, -4)) | ||
print(get_max(-4, -2)) | ||
print(get_max(-3, -6)) | ||
if __name__ == "__main__": | ||
print(get_max(1, 5)) | ||
print(get_max(4, 3)) | ||
print(get_max(-3, 6)) | ||
print(get_max(5, -4)) | ||
print(get_max(-4, -2)) | ||
print(get_max(-3, -6)) | ||
|
||
|
||
""" | ||
SPECS: | ||
TIME COMPLEXITY: O(1) | ||
SPACE COMPLEXITY: O(1) | ||
""" |