-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockBST.py
39 lines (38 loc) · 1.91 KB
/
LockBST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
##DailyCoding
class BiNode():
"""define the binary search tree Node"""
def __init__(self, val, leftChild, rightChild):
self.val = val ##node val
self.leftChild = leftChild ##left child of node
self.rightChild = rightChild ##right child of node
def insert(self, newVal):
"""Insert Value to the right or left of a BiNode"""
if newVal < self.val:
if self.leftChild!=None: self.leftChild.insert(newVal) ##if less than root insert in left subTree
else: self.leftChild = BiNode(newVal, None, None) #if None object in left subtree creat new BiNode there
elif newVal > self.val:
if self.rightChild != None: self.rightChild.insert(newVal)##else insert in right subtree
else: self.rightChild = BiNode(newVal, None, None)#if None object in right subtree creat new BiNode there
def isLocked(self, Node):
"""return a boolean val whether a node is locked or not"""
##this methode is for TreeExpression Class
def eval(self): ###Debug this
if type(self.leftChild) != str: ##
return eval(str(self.leftChild) + self.val + str(self.rightChild))
else:
self.leftChild.eval()
self.rightChild.eval()
class Tree:
"""Simple Binary search Tree"""
root = None ##instantiate Root of binary Tree
def inSert(self, val):
"""Add value to Tree"""
if self.root == None: root = BiNode(val, None, None) ##Add the first element of Tree with null children
else: ##if tree is not empty
self.root.insert(val) ##This implements insert Methode in BiNode class
def isLocked(self):
"""return a bolean val on wether Node is locked"""
##if all node in its subtree is locked then The node is Locked
def unLock(self):
"""Unlocks a BiNode"""
##unloced if all the node in its subtree are unlocked