Adding Student Records to Binary Search Tree in Python - python

Sorry for all of the code, but for this assignment I am working on, it is necessary due to different references.
This chapter we are working with Binary Trees and Binary Search Trees.
I tested the BinaryTree() and BinarySearchTree() classes with no issue.
My problem is this: we are adding records to a binary search tree as seen in the Student class and the main() function to test the class. According to the assignment:
The Student class has an id and name, getters and setters, a str() function so that you can print a student record, and the operations ==, !=, <=, <, >=, > defined. The comparison operators compare student id's which are intended to be unique. Since we can compare student records, we can now put them into a Binary Search Tree.
My question is, how do I add the records a binary tree if all of the initialized variables are completely different than ones in the BinaryTree() class and the BinarySearchTree() class?
Everytime I run the code, I get errors such as:
AttributeError: 'Student' object has no attribute 'insert'
or
isEmpty() = True
None
isEmpty() = False
None
101(Betty Smith)
101(Betty Smith)
Traceback (most recent call last):
File "/Users/phillipward/Desktop/Lab 10/StudentRecordsTest.py", line 50, in <module>
main()
File "/Users/phillipward/Desktop/Lab 10/StudentRecordsTest.py", line 22, in main
BST.insert(Student(42, "Amy Winchester"))
File "/Users/phillipward/Desktop/Lab 10/BinarySearchTree.py", line 13, in insert
self.getleftChild().insert(data)
File "/Users/phillipward/Desktop/Lab 10/BinarySearchTree.py", line 7, in insert
if(self.isEmpty()):
File "/Users/phillipward/Desktop/Lab 10/binaryTree.py", line 33, in isEmpty
if(self is None or self.data is None):
AttributeError: 'Student' object has no attribute 'data'
I'm stuck on how to tell the program that I the student class is a type of BinarySearchTree.
class BinaryTree():
def __init__(self, data = None, leftChild = None, rightChild = None):
self.data = data
self.leftChild = leftChild
self.rightChild = rightChild
def getData(self):
return self.data
def setData(self, x):
self.data = x
return self.data
def getleftChild(self):
return self.leftChild
def setleftChild(self, x):
self.leftChild = x
return self.leftChild
def getrightChild(self):
return self.rightChild
def setrightChild(self, x):
self.rightChild = x
return self.rightChild
def isEmpty(self):
if(self is None or self.data is None):
return True
else:
return False
def __str__ (self):
return "{0}".format(self.data)
def inOrderTraversal(self):
if (self is None):
return "Empty"
else:
result = ""
if(self.getleftChild() is not None): # When we put payload in, we need equality
result += BinaryTree.inOrderTraversal(self.getleftChild()) + " "
result += str(self.getData())
if (self.getrightChild() is not None):
result += " " + BinaryTree.inOrderTraversal(self.getrightChild()) + " "
return result
def preOrderTraversal(self):
if (self.isEmpty()):
return "Empty"
else:
result = ""
result += str(self.getData())
if (self.getleftChild() is not None):
result += " " + BinaryTree.preOrderTraversal(self.getleftChild()) + " "
if (self.getrightChild() is not None):
result += BinaryTree.preOrderTraversal(self.getrightChild())
return result
def postOrderTraversal(self):
if (self.isEmpty()):
return "Empty"
else:
result = ""
if (self.getleftChild() is not None):
result += BinaryTree.postOrderTraversal(self.getleftChild()) + " "
if (self.getrightChild() is not None):
result += BinaryTree.postOrderTraversal(self.getrightChild()) + " "
result += str(self.getData())
return result
def insert(self, data):
if (self.isEmpty()):
self.setData(data)
elif (data < self.getData()):
if (self.getleftChild() is None):
self.setleftChild(BinarySearchTree(data))
else:
self.getleftChild().insert(data)
else: # data >= self.getData()
if (self.getrightChild() is None):
self.setrightChild(BinarySearchTree(data))
else:
self.getrightChild().insert(data)
def retrieve(self, data):
if self.isEmpty():
return None
elif data == self.getData():
return self.getData()
elif data <= self.getData():
if self.getleftChild() is None:
return None
else:
return self.getleftChild().retrieve(data)
else:
if self.getrightChild() is None:
return None
else:
return self.getrightChild().retrieve(data)
from binaryTree import BinaryTree
class BinarySearchTree(BinaryTree):
def insert(self, data):
if(self.isEmpty()):
self.setData(data)
elif(data < self.getData()):
if(self.getleftChild() is None):
self.setleftChild(data)
else:
self.getleftChild().insert(data)
else: #data >= self.getData()
if(self.getrightChild() is None):
self.setrightChild(data)
else:
self.getrightChild().insert(data)
def retrieve(self, data):
if self.isEmpty():
return None
elif data == self.getData():
return self.getData()
elif data <= self.getData():
if self.getleftChild() is None:
return None
else:
return self.getleftChild().retrieve(data)
else:
if self.getrightChild() is None:
return None
else:
return self.getrightChild().retrieve(data)
def minValue(self):
current = self
while current.leftChild is not None:
current = current.leftChild
return current.data
def maxValue(self):
current = self
while current.rightChild is not None:
current = current.rightChild
return current.data
def isBST(self):
current = self
if current == None:
return True
if current.leftChild.data <= current.data and
current.rightChild.data >= current.data:
return True
else:
return False
class Student():
def __init__(self, id = None, name = ""):
self.__id = id
self.__name = name
def getId(self):
return self.__id
def setId(self, id):
self.__id = id
def getName(self):
return self.__name
def setName(self, name):
self.__id = name
def __str__(self):
if (self is None):
return ""
else:
return str(self.__id) + "(" + self.__name + ")"
def __cmp__(self, other):
if (self is None or other is None):
return 0
else:
return self.__id - other.__id
def __eq__(self, other):
return self.__cmp__(other) == 0
def __ne__(self, other):
return self.__cmp__(other) != 0
def __lt__(self, other):
return self.__cmp__(other) < 0
def __le__(self, other):
return self.__cmp__(other) <= 0
def __gt__(self, other):
return self.__cmp__(other) > 0
def __ge__(self, other):
return self.__cmp__(other) >= 0
from BinarySearchTree import BinarySearchTree
from Student import Student
def main():
BST = BinarySearchTree()
print("isEmpty() = " + str(BST.isEmpty()))
print(BST)
BST.setData(Student(101, "Betty Smith"))
print("isEmpty() = " + str(BST.isEmpty()))
print(BinarySearchTree())
BST.insert(Student(50, "John Adams"))
print(BST)
BST.insert(Student(250, "Mike Jones"))
print(BST)
BST.insert(Student(42, "Amy Winchester"))
print(BST)
BST.insert(Student(31, "Jill Ranger"))
print(BST)
BST.insert(Student(315, "Bob Crachet"))
print(BST)
BST.insert(Student(200, "Karen Wilkins"))
print(BST)
print("\n")
print()
print("Inorder traversal: " + str(BST))
print()
print("Preorder traversal: \n" + BST.preOrderTraversal())
print()
print("Postorder traversal: " + BST.postOrderTraversal())
print()
print("minValue: " + str(BST.minValue()))
print("maxValue: " + str(BST.maxValue()))
print()
print("isBST = " + str(BST.isBST()))
for id in [101, 200, 31, 50, 315, 250, 42]:
print(BST.retrieve(Student(id)))
main()

Related

I'm getting a error on an AVL tree search problem

I'm trying to do a search function for an AVL-tree, when i try to search for a number that is in the tree the code works fine, but i'm getting the error
AttributeError: 'NoneType' object has no attribute 'search'
when a try to search for a number that isn't in the tree
This is the search function
def search(self, data):
if self is None:
return "the key doesn't exist"
elif data < self.data:
return self.left.busca(data)
elif data > self.data:
return self.right.busca(data)
else:
return "the key exist"
and the whole code(the name of the variables are in pt-br)
class No:
def __init__(self, data):
self.data = data
self.setaFilhos(None, None)
def setaFilhos(self, esquerda, direita):
self.esquerda = esquerda
self.direita = direita
def balanco(self):
prof_esq = 0
if self.esquerda:
prof_esq = self.esquerda.profundidade()
prof_dir = 0
if self.direita:
prof_dir = self.direita.profundidade()
return prof_esq - prof_dir
def profundidade(self):
prof_esq = 0
if self.esquerda:
prof_esq = self.esquerda.profundidade()
prof_dir = 0
if self.direita:
prof_dir = self.direita.profundidade()
return 1 + max(prof_esq, prof_dir)
def rotacaoEsquerda(self):
self.data, self.direita.data = self.direita.data, self.data
old_esquerda = self.esquerda
self.setaFilhos(self.direita, self.direita.direita)
self.esquerda.setaFilhos(old_esquerda, self.esquerda.esquerda)
def rotacaoDireita(self):
self.data, self.esquerda.data = self.esquerda.data, self.data
old_direita = self.direita
self.setaFilhos(self.esquerda.esquerda, self.esquerda)
self.direita.setaFilhos(self.direita.direita, old_direita)
def rotacaoEsquerdaDireita(self):
self.esquerda.rotacaoEsquerda()
self.rotacaoDireita()
def rotacaoDireitaEsquerda(self):
self.direita.rotacaoDireita()
self.rotacaoEsquerda()
def executaBalanco(self):
bal = self.balanco()
if bal > 1:
if self.esquerda.balanco() > 0:
self.rotacaoDireita()
else:
self.rotacaoEsquerdaDireita()
elif bal < -1:
if self.direita.balanco() < 0:
self.rotacaoEsquerda()
else:
self.rotacaoDireitaEsquerda()
def insere(self, data):
if data <= self.data:
if not self.esquerda:
self.esquerda = No(data)
else:
self.esquerda.insere(data)
else:
if not self.direita:
self.direita = No(data)
else:
self.direita.insere(data)
self.executaBalanco()
def remove(self, data):
if self.direita is None and self.esquerda is None:
return None
if data < self.data:
self.esquerda = self.esquerda.remove(data)
elif data > self.data:
self.direita = self.direita.remove(data)
else:
if self.direita is None:
return self.esquerda
if self.esquerda is None:
return self.direita
tmp = self.direita._min()
self.data = tmp.data
self.direita = self.direita.remove(tmp.data)
self.executaBalanco()
return self
def busca(self, data):
if self is None:
return "A chave não existe"
elif data < self.data:
return self.esquerda.busca(data)
elif data > self.data:
return self.direita.busca(data)
else:
return "A chave existe"
def _min(self):
if self.esquerda is None:
return self
else:
return self.esquerda._min()
def imprimeArvore(self, indent = 0):
print ( "-" * indent + "|" + str(self.data))
if self.esquerda:
self.esquerda.imprimeArvore(indent + 2)
if self.direita:
self.direita.imprimeArvore(indent + 2)
arvore = None
for data in [15, 27, 49, 10, 8, 67, 59, 9, 13, 20, 14]:
if arvore:
arvore.insere(data)
else:
arvore = No(data)
arvore.imprimeArvore()
arvore.busca(1)
I think the problem is the returning when the key is not found, but i not sure, none of the similar question that i tried to read helped me and i tried to look the code of other people and at least for me my code should work . What did i do wrong?There is another way to do it?
def search(self, data):
if self is None:
return "the key doesn't exist"
elif data < self.data:
return self.left.busca(data)
elif data > self.data:
return self.right.busca(data)
else:
return "the key exist"
If the search method is called successfully, self is not None.
You should check whether self.left is None or self.right is None before making the recursive call to search.
def search(self, data):
if data == self.data:
return "the key exists"
if data < self.data and self.left is not None:
return self.left.busca(data)
elif data > self.data and self.right is not None:
return self.right.busca(data)
else:
return "the key exist"

Creating a class with push and pop methods in Python

I am trying to create a Python object from the class below, but when I call the method popit(), I am getting a None. Any help as to what I miss or do wrong is appreciated.
class Stack:
def __init__(self, *item):
self.stack = []
def pushit(self, item):
if len(self.stack) == 9:
print("Stack is full: " + self.stack)
else:
print(self.stack.append(self.item))
def popit(self):
if self.stack == []:
print("Stack underflow: " + self.stack)
else:
print(self.stack.pop())
Your methods -- both of them -- don't return anything. What I would do to sort this is:
class Stack:
def __init__(self):
self.stack = []
def pushit(self, item):
if len(self.stack) == 9:
print("Stack is full: " + self.stack)
return None
else:
print(self.stack.push(self.item))
return self.item
def popit(self):
if self.stack == []:
print("Stack underflow: " + self.stack)
return None
else:
ret = self.stack.pop()
print(ret)
return ret

deleting the head node in a linked list

How can I fix the delete method so that it can also delete the head node?
class LinkedListNode():
def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node
def data(self):
return self.data
def next_node(self):
return self.next_node
def set_next_node(self, next_node):
self.next_node = next_node
def search(self, data):
index = 0
while self.next_node != None and self != None:
if self.data == data:
return index
else:
self = self.next_node
index += 1
return -1
def delete(self, data):
head = self
if self.data == data:
head = self.next_node
while self.next_node != None and self != None:
if self.next_node.data == data:
if self.next_node.next_node != None:
self.next_node = self.next_node.next_node
else:
self.next_node = None
else:
self = self.next_node
return head
def print(self):
while self.next_node != None:
print("%s: %s" % ("current node data is", self.data))
self = self.next_node
if self.next_node == None and self != None:
print("%s: %s" % ("current node data is", self.data))
and in the test file I have:
from LinkedListNode import *
head = LinkedListNode(3)
node1 = LinkedListNode('cat')
node2 = LinkedListNode('dog')
node3 = LinkedListNode(4)
head.set_next_node(node1)
node1.set_next_node(node2)
node2.set_next_node(node3)
print(head.search('dog'))
head.delete(3)
head.delete(4)
head.print()
I get:
2
current node data is: 3
current node data is: cat
current node data is: dog
Process finished with exit code 0
First of all, I believe that you are not supposed to reassign the 'self' in search method:
def search(self, data):
index = 0
while self.next_node != None and self != None:
if self.data == data:
return index
else:
self = self.next_node
index += 1
return -1
Replace it with a simple recursive version and try again:
def search(self, data):
if self.data == data:
return 0
elif self.next_node == None:
return -1
else:
idx = self.next_node.search(data)
if idx == -1:
return -1
else:
return 1 + idx

Reverse Class Method Not Working

So I created a simple class method to reverse a string and yet it returns to me the original? The Method that I created works outside of the class but for some reason it does not when I try to implement it.
See my code below:
class Stack:
def __init__(self):
self.__items = []
def push(self, item):
self.__items.append(item)
def pop(self):
return self.__items.pop()
def peek(self):
return self.__items[len(self.__items)-1]
def is_empty(self):
return len(self.__items) == 0
def size(self):
return len(self.__items)
def reverse(self):
if len(self.__items) <= 1:
return self.__items
return self.__items.reverse(self.__items[1:]) + self.__items[0]
s=Stack()
rev=input("Enter string to reverse; ")
s.push(rev)
print(s.reverse())
You need to reverse each string in self.__items not self.__items itself:
def reverse(self):
if not self.__items: # if items is empty return empty string
return ""
return " ".join(s[::-1] for s in self.__items)
self.__items[::-1] will reverse the list items not the string/strings inside.
If you wanted to do it without slicing:
def reverse(self):
if not self.__items:
return ""
out = ""
for item in self.__items:
temp = ""
for i in range(len(item)-1, -1, -1):
temp += item[i]
out += temp
return out
Or recursively:
def reverse(self):
if not self.__items:
return ""
def recur(s):
if not s:
return ""
return s[-1] + recur(s[:-1])
return " ".join(recur(w) for w in self.__items)

Recursion with large text file

I am trying to read in a large text file (1.08 MB with 116253 words) and I am running into a problem with the program stopping about 1/20th of the way into the text file (Stopping in the Read function and not even executing the print statement).
Here is the function as I have it now:
from binary_search_tree import BinarySearchTree
from tree_node import TreeNode
import sys
sys.setrecursionlimit(5000000)
MovieTree = BinarySearchTree()
def Read(filename):
file = open('movieData.txt')
for line in file:
MovieTree[line.strip()] = line
print(line)
file.close()
Read('movieData.txt')
print(MovieTree)
The binary_search_tree and tree_node where given to me as this is a homework assignment so I am under the assumption that is works because it has been used before.
Does anyone have any idea as to what the problem is?
Binary Search Tree:
from tree_node import TreeNode
class BinarySearchTree:
def __init__(self):
self.root = None
self.size = 0
def length(self):
return self.size
def __len__(self):
return self.size
def __iter__(self):
return self.root.__iter__()
def __str__(self):
"""Returns a string representation of the tree
rotated 90 degrees counter-clockwise"""
def strHelper(root, level):
resultStr = ""
if root:
resultStr += strHelper(root.rightChild, level+1)
resultStr += "| " * level
resultStr += str(root.key) + "\n"
resultStr += strHelper(root.leftChild, level+1)
return resultStr
return strHelper(self.root, 0)
def __contains__(self,key):
if self._get(key,self.root):
return True
else:
return False
def get(self,key):
if self.root:
res = self._get(key,self.root)
if res:
return res.payload
else:
return None
else:
return None
def _get(self,key,currentNode):
if not currentNode:
return None
elif currentNode.key == key:
return currentNode
elif key < currentNode.key:
return self._get(key,currentNode.leftChild)
else:
return self._get(key,currentNode.rightChild)
def __getitem__(self,key):
return self.get(key)
def __setitem__(self,k,v):
self.put(k,v)
def put(self,key,val):
if self.root:
self._put(key,val,self.root)
else:
self.root = TreeNode(key,val)
self.size = self.size + 1
def _put(self,key,val,currentNode):
if key < currentNode.key:
if currentNode.hasLeftChild():
self._put(key,val,currentNode.leftChild)
else:
currentNode.leftChild = TreeNode(key,val,
parent=currentNode)
else:
if currentNode.hasRightChild():
self._put(key,val,currentNode.rightChild)
else:
currentNode.rightChild = TreeNode(key,val,
parent=currentNode)
def delete(self,key):
if self.size > 1:
nodeToRemove = self._get(key,self.root)
if nodeToRemove:
self.remove(nodeToRemove)
self.size = self.size-1
else:
raise KeyError('Error, key not in tree')
elif self.size == 1 and self.root.key == key:
self.root = None
self.size = self.size - 1
else:
raise KeyError('Error, key not in tree')
def __delitem__(self,key):
self.delete(key)
def remove(self,currentNode):
if currentNode.isLeaf(): #leaf
if currentNode == currentNode.parent.leftChild:
currentNode.parent.leftChild = None
else:
currentNode.parent.rightChild = None
elif currentNode.hasBothChildren(): #interior
succ = currentNode.findSuccessor()
succ.spliceOut()
currentNode.key = succ.key
currentNode.payload = succ.payload
else: # this node has one child
if currentNode.hasLeftChild():
if currentNode.isLeftChild():
currentNode.leftChild.parent = currentNode.parent
currentNode.parent.leftChild = currentNode.leftChild
elif currentNode.isRightChild():
currentNode.leftChild.parent = currentNode.parent
currentNode.parent.rightChild = currentNode.leftChild
else:
currentNode.replaceNodeData(currentNode.leftChild.key,
currentNode.leftChild.payload,
currentNode.leftChild.leftChild,
currentNode.leftChild.rightChild)
else:
if currentNode.isLeftChild():
currentNode.rightChild.parent = currentNode.parent
currentNode.parent.leftChild = currentNode.rightChild
elif currentNode.isRightChild():
currentNode.rightChild.parent = currentNode.parent
currentNode.parent.rightChild = currentNode.rightChild
else:
currentNode.replaceNodeData(currentNode.rightChild.key,
currentNode.rightChild.payload,
currentNode.rightChild.leftChild,
currentNode.rightChild.rightChild)
TreeNode:
class TreeNode:
def __init__(self,key,val,left=None,right=None,
parent=None):
self.key = key
self.payload = val
self.leftChild = left
self.rightChild = right
self.parent = parent
def hasLeftChild(self):
return self.leftChild
def hasRightChild(self):
return self.rightChild
def isLeftChild(self):
return self.parent and \
self.parent.leftChild == self
def isRightChild(self):
return self.parent and \
self.parent.rightChild == self
def isRoot(self):
return not self.parent
def isLeaf(self):
return not (self.rightChild or self.leftChild)
def hasAnyChildren(self):
return self.rightChild or self.leftChild
def hasBothChildren(self):
return self.rightChild and self.leftChild
def replaceNodeData(self,key,value,lc,rc):
self.key = key
self.payload = value
self.leftChild = lc
self.rightChild = rc
if self.hasLeftChild():
self.leftChild.parent = self
if self.hasRightChild():
self.rightChild.parent = self
def __iter__(self):
if self:
if self.hasLeftChild():
for elem in self.leftChild:
yield elem
yield self.key
if self.hasRightChild():
for elem in self.rightChild:
yield elem
def findSuccessor(self):
succ = None
if self.hasRightChild():
succ = self.rightChild.findMin()
else:
if self.parent:
if self.isLeftChild():
succ = self.parent
else:
self.parent.rightChild = None
succ = self.parent.findSuccessor()
self.parent.rightChild = self
return succ
def findMin(self):
current = self
while current.hasLeftChild():
current = current.leftChild
return current
def spliceOut(self):
if self.isLeaf():
if self.isLeftChild():
self.parent.leftChild = None
else:
self.parent.rightChild = None
elif self.hasAnyChildren():
if self.hasLeftChild():
if self.isLeftChild():
self.parent.leftChild = self.leftChild
else:
self.parent.rightChild = self.leftChild
self.leftChild.parent = self.parent
else:
if self.isLeftChild():
self.parent.leftChild = self.rightChild
else:
self.parent.rightChild = self.rightChild
self.rightChild.parent = self.parent
1.08 MB is a small file. Why do you want to use BinarySearchTree? You can easily handle this by dumping the data into a dictionary.
If BinarySearchTree is part of the assignment, it looks to me it has bug. I'd start tracing from its _put() method.
By the way you should not use sys.setrecursionlimit(5000000) to get around the problem. A decent binary search will not hit this limit unless the size of your data is in the order of magnitude of 2^5000000. For the 116253 words that you have a balanced binary tree should only need 12 level of recursion.
Crashes here too with a stack-overflow.
Since your code doesn't contain recursion it must be within the classes your teacher gave you. I think the overflow happens in the put(self, key, val) method.
I'm afraid I barely know Python so I can't give you any futher help.

Categories

Resources