I am trying to binary create a tree from a binary sequence like "100101" then i want the tree to be created like this. (Basically 1 means go to the right and 0 means go to the left)
<Root node>
|
1
/
0
/
0
\
1
/
0
\
1
so i did it here is the code: where the value would be the string sequence (ex value = "1001")
def _inserto(root,value):
for i in value:
if root == None:
root = BSTNode(i)
current = root
elif i == "1":
if current.right is not None:
current.right = BSTNode(i)
current = current.right
else:
current.right = BSTNode(i)
current = current.right
elif i == "0":
if (current.left is not None):
current.left = BSTNode(i)
current = current.left
else:
current.left =BSTNode(i)
current = current.left
return root
Now the problem is that if i want to input another sequence like "01", the tree should look like this
<Root Node>
|
1
/
0
/ \
0 1
\
1
/
0
\
1
, but i am really having a hard time, since my function is going to over-write the old tree.
_insert- Start with an intial root of 1, next is 0 < 1 so it goes left, but then 0 is not < 0, so it should go right (but is shown as going left)... I'm hoping the OP can clarify which of the three possible results is actually desired.