Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
210CT-/Exercises1_and_2.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
123 lines (92 sloc)
4.54 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file=open("BTtext.txt","r") | |
string = "" #empty string to handle data | |
for line in file: | |
string=string+line #add another value with the variable's value and assign the new value to the variable | |
word_stri = string.split() #splits string and converts it into list | |
file.close() | |
class BinTreeNode(object): | |
def __init__(self,value): #Access to node value and left/right nodes assignment | |
self.value=value | |
self.left=None | |
self.right=None | |
def insertValue(self,value): | |
if self.value: #comparing the new value with the parent | |
if value < self.value: | |
if self.left is None: | |
self.left=BinTreeNode(value) #if left node empty, insterting the value | |
else: | |
self.left.insertValue(value) # goes to next left node | |
elif value>self.value: | |
if self.right is None: | |
self.right=BinTreeNode(value) #if right node empty,assign the value | |
else: | |
self.right.insertValue(value) #goes to next right node | |
else: | |
self.value=value | |
def preOrderPrint(self): | |
print(self.value) | |
if self.left: #checking if left node has value | |
self.left.preOrderPrint() | |
if self.right: #checking if right node has value | |
self.right.preOrderPrint() | |
def search(self,word): | |
print(self.value) | |
if word < self.value: #check if searching word is less than root value | |
if self.left is None: #check if left node is empty | |
return False | |
return self.left.search(word) #recurse to left node | |
elif word > self.value: | |
if self.right is None: #check if right node is empty | |
return False | |
return self.right.search(word) #recurse to right node | |
else: | |
return True #word found return True | |
def minimuValue(self): | |
minm = self | |
while(minm.left is not None): # going to most left node | |
minm = minm.left | |
return self.value | |
def deletion(self,value): # delete the word and returns the new root | |
if self == None: #check if root is empty | |
return self | |
if value < self.value : | |
self.left = self.left.deletion(value) #goes to left in the tree | |
elif value > self.value : | |
self.right = self.right.deletion(value) #goes to right in tree | |
else: # if the word is same as the node | |
# if one or no child | |
if self.left == None : #check if left node is empty | |
hold = self.right | |
self = None | |
return hold | |
elif self.right == None : #check if right node is empty | |
hold = self.left | |
self = None | |
return hold | |
# if two children | |
hold = self.right.minimuValue() #goes to smallest in right tree | |
self.value = hold # hold the next node in order to this node | |
self.right = self.right.deletion(hold) # Delete's it | |
return self | |
root=BinTreeNode(None) | |
for i in range(0,len(word_stri)): #take from list and insert in Bin Tree | |
root.insertValue(word_stri[i]) | |
root.preOrderPrint() #print tree testing | |
SearchWord=input("Search a word: ") #input word for search | |
print("::Searching::") | |
if root.search(SearchWord)==True: #check output of searching if True | |
print("Finished Traversal") | |
print("Yes,Found") | |
else: # > printing | |
print("Finished Traversal") | |
print("No,Not Found") | |
textDelete=input("Word to delete: ") | |
print("::Searching::") | |
#check if word exists in tree | |
if root.search(textDelete)==False: | |
print("The word is not in the list..") | |
else: | |
print("Word is Found and Approved for deletion. ") | |
root.deletion(textDelete) | |
print(textDelete," has been deleted succesfuly") | |
root.preOrderPrint() #Showing the tree after deletion |