I have been studying intermediate or more advanced data structures in Python, one of which I've decided to try out is the Tree Data Structure. I've been studying the theories as well as the implementations from Youtube courses and I might say that I've grasped quite a bit of it.
I decided to make my own original algorithm to append and access a data in a Tree without copying from the videos:
class TreeNode:
def __init__(self,data = None):
self.data = data
self.left = None
self.right = None
class Tree:
def __init__(self):
self.head = TreeNode()
def append(self, data):
cur = self.head
new_node = TreeNode(data)
while True:
if cur.data == None:
cur.data = data
return
if data < cur.data:
if cur.left == None:
cur.left = new_node
return
if cur.left != None:
cur = cur.right
if data > cur.data:
if cur.right == None:
cur.right = new_node
return
if cur.right != None:
cur = cur.right
def find_data(self, data):
cur = self.head
while cur.right != None and cur.left != None:
if data > cur.data:
cur = cur.right
if data < cur.data:
cur = cur.left
if data == cur.data:
return print(f'Data {data} has been found.')
return print("Im sorry, your data is not found!")
my_tree = Tree()
my_tree.append(50)
my_tree.append(100)
my_tree.append(30)
my_tree.find_data(30)
my_tree.find_data(20)
I would like to know if this is considered an optimal and efficient algorithm since I've been writing it only using the theories and logics from the videos. I would like to know if my understanding's clear enough.
If there are any, in what ways could the algorithms be improved?