- #1
FallArk
- 127
- 0
My instructor asked us to create a binary search tree in python that supports variables. i.e. the tree will have variables inside, and the user can get the values for those variables by looking for the name of that variable. I don't really know how to implement this.
What I have so far:
One hint was given to us:
some of the exact requirements:
What I have so far:
Code:
class VarTree:
class Node:
__slots__ = "_value", "_left", "_right", "_var"
def __init__(self, l, val, r, v):
self._left = l;
self._value = val;
self._right = r;
self._var = v;
def __init__(self):
self._root = None
def is_empty(self):
if self._root is None:
return True
else:
return False
def _insert(self, here, var, value): #insert a variable with value, if the var already exists, replace with the new value
if here is None:
return self.Node(None, value, None, var)
elif var < here._var:
return self.Node(self._insert(here._left, var, value), here._value, here._right, here._var)
elif var == here._var:
return self.Node(here._left, value, here._right, here._var)
elif var > here._var:
return self.Node(here._left, here._value, self._insert(here._right, var, value), here._var)
else:
return here
def _search(self, here, var): #find the variable and return the node
if var > here._var:
return self.Node(here._left, here, self._search(here._right, var), )
elif var < here._var:
return self.Node(self._search(here._left, var), here, here._right)
else:
return here
def assign(var, value): #Should "self" be included in the parameters
return None #Should I call _insert
def lookup(var):
return None #Should I call _search
I know from the hint that instead of comparing values when inserting or searching, i should use variable names instead. But it is really confusing.strings can be compared
some of the exact requirements:
This VarTree will be used to look up variable values, so when an infix expression like :Defining a Binary Tree
There is no need for any self-balancing features in this assignment -- just the a simple binary tree supporting the ability to store new variables, to associate values with those variables, and to retrieve the value of a variable.
For consistency, call your file vartree.py
Required Features in Implementation
Nested Node class definition for binary tree node
with __slots__ to save list memory
and __init__ a constructor
__init__() a constructor for an empty tree
_search(here, var) recursively search tree rooted 'here' for variable 'var'
returning correct Node, or None if not found
_insert(here, var, value) return a search tree with variable inserted or updated
this should be a recursive implementation for immutable behavior
assign(var, value) public method to assign to a variable
lookup(var) public method retrieve value of variableif the variable does not yet exist, assign it zero
Good functions for Completeness / Practice
is_empty() Identify whether tree is empty
__len__() returns number of elements in list
__iter__() inorder iterator generator(with yield statements)
__str__() represents entire tree as a string
For full credit, this must be no worse than log-linear time and not quadratic
can be turned into postfix :x = y = 4
, this tree will bring up and store the correct values.xy4==