Skip to content

Commit

Permalink
feat: menambahkan struktur data tree (#214)
Browse files Browse the repository at this point in the history
* feat: menambahkan binary search tree model rekursif

Signed-off-by: slowy07 <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: memperbaiki kelas yang duplicate

Signed-off-by: slowy07 <[email protected]>

* feat: menambahkan binary tree node sum

Signed-off-by: slowy07 <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* feat: menambahkan binary tree path sum

referensi diambil dari problem leet code https://leetcode.com/problems/path-sum-iii/

Signed-off-by: slowy07 <[email protected]>

* feat: menambahkan basic dari binary tree

Signed-off-by: slowy07 <[email protected]>

* fix: memperbaiki issue E0702

Signed-off-by: slowy07 <[email protected]>

* fix: menambahkan test problem pada binary search tree rekursif

Signed-off-by: slowy07 <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Signed-off-by: slowy07 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
slowy07 and pre-commit-ci[bot] authored Nov 6, 2022
1 parent a9b6d70 commit 74f119e
Show file tree
Hide file tree
Showing 4 changed files with 545 additions and 0 deletions.
86 changes: 86 additions & 0 deletions struktur_data/tree/binary_search_tree/basic_binary_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from __future__ import annotations


class Node:
# node memiliki variabel data dan pointer
# ke node di kiri dan kanannya

def __init__(self, data: int) -> None:
self.data = data
self.left: Node | None = None
self.right: Node | None = None


def display(tree: Node | None) -> None:
"""
>>> root = Node(1)
>>> root.left = Node(0)
>>> root.right = Node(2)
>>> display(root)
0
1
2
>>> display(root.right)
2
"""
if tree:
display(tree.left)
print(tree.data)
display(tree.right)


def depth_of_tree(tree: Node | None) -> int:
"""
fungsi rekursif yang mengembalikan kedalam
binary tree
>>> root = Node(0)
>>> depth_of_tree(root)
1
>>> root.left = Node(0)
>>> depth_of_tree(root)
2
>>> root.right = Node(0)
>>> depth_of_tree(root)
2
>>> root.left.right = Node(0)
>>> depth_of_tree(root)
3
>>> depth_of_tree(root.left)
2
"""
return 1 + max(depth_of_tree(tree.left), depth_of_tree(tree.right)) if tree else 0


def is_full_binary_tree(tree: Node) -> bool:
"""
mengembalikan True jika ini binary tree penuh
>>> root = Node(0)
>>> is_full_binary_tree(root)
True
>>> root.left = Node(0)
>>> is_full_binary_tree(root)
False
>>> root.right = Node(0)
>>> is_full_binary_tree(root)
True
>>> root.left.left = Node(0)
>>> is_full_binary_tree(root)
False
>>> root.right.right = Node(0)
>>> is_full_binary_tree(root)
False
"""
if not tree:
return True
if tree.left and tree.right:
return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)
else:
return not tree.left and not tree.right


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading

0 comments on commit 74f119e

Please sign in to comment.