Post

Day 29 of 100 Days of Python

Practising DSA

Today I practised the Binary Search Tree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Binary Search Tree

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right= None
def insert(root, key):
    if root is None:
        return Node(key)
    if key<root.value:
        root.left = insert(root.left, key)
    if key>root.value:
        root.right = insert(root.right, key)
    return root
root = None
root = insert(root, 50)
root = insert(root, 30)
root = insert(root, 70)

print("Root value:", root.value)
print("Left value:", root.left.value)
print("Right value:", root.right.value)

Output:

1
2
3
Root value: 50
Left value: 30
Right value: 70
This post is licensed under CC BY 4.0 by the author.

Trending Tags