Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
673 additions
and
351 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
|
||
'''SELECTION SORT | ||
input: unsorted list | ||
output: sorted list | ||
Implements selection sort with swaps | ||
''' | ||
|
||
|
||
BIN
+6 KB
lab5/.DS_Store
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import math | ||
|
||
class Node: #intialising a node for bst tree | ||
def __init__(self, data = None): | ||
self.data = data #data part on center node | ||
self.left = None #data on right side of the node | ||
self.right = None #data on right side of the node | ||
|
||
""" BST class with insert and display methods. display pretty prints the tree | ||
""" | ||
class BinaryTree: #class for the binary tree | ||
def __init__(self): | ||
self.root = None #declaring root as empty | ||
|
||
def insert(self, data): #funtion for checking if root is empty | ||
if self.root is None: #checking if funtion is empty | ||
self.root = Node(data) | ||
else: | ||
self._insert(data, self.root) #else insering the data to root node | ||
|
||
def _insert(self, data, cur_node): #funtion for inserion of data to current node | ||
if data < cur_node.data: #if data is smaller than the current node | ||
if cur_node.left is None: # checks if its empty | ||
cur_node.left = Node(data)#it is them appended left side of the list | ||
else: | ||
self._insert(data, cur_node.left) #its recursed through the loop | ||
elif data > cur_node.data: #data of current node is greater than current node | ||
if cur_node.right is None: #checking if node is empty | ||
cur_node.right = Node(data) #Node is appended to the | ||
else: | ||
self._insert(data, cur_node.right) #it is them appended right side of the list | ||
else: | ||
print("Value already present in tree") #print value is present | ||
|
||
def display(self, cur_node): #funtion for displaying the current node | ||
lines, _, _, _ = self._display(cur_node) | ||
for line in lines: | ||
print(line) | ||
|
||
|
||
def _display(self, cur_node): #funtion for printing tree | ||
|
||
if cur_node.right is None and cur_node.left is None: | ||
line = '%s' % cur_node.data | ||
width = len(line) | ||
height = 1 | ||
middle = width // 2 | ||
return [line], width, height, middle | ||
|
||
if cur_node.right is None: | ||
lines, n, p, x = self._display(cur_node.left) | ||
s = '%s' % cur_node.data | ||
u = len(s) | ||
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s | ||
second_line = x * ' ' + '/' + (n - x - 1 + u) * ' ' | ||
shifted_lines = [line + u * ' ' for line in lines] | ||
return [first_line, second_line] + shifted_lines, n + u, p + 2, n + u // 2 | ||
|
||
if cur_node.left is None: | ||
lines, n, p, x = self._display(cur_node.right) | ||
s = '%s' % cur_node.data | ||
u = len(s) | ||
first_line = s + x * '_' + (n - x) * ' ' | ||
second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' ' | ||
shifted_lines = [u * ' ' + line for line in lines] | ||
return [first_line, second_line] + shifted_lines, n + u, p + 2, u // 2 | ||
|
||
left, n, p, x = self._display(cur_node.left) | ||
right, m, q, y = self._display(cur_node.right) | ||
s = '%s' % cur_node.data | ||
u = len(s) | ||
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s + y * '_' + (m - y) * ' ' | ||
second_line = x * ' ' + '/' + (n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' ' | ||
if p < q: | ||
left += [n * ' '] * (q - p) | ||
elif q < p: | ||
right += [m * ' '] * (p - q) | ||
zipped_lines = zip(left, right) | ||
lines = [first_line, second_line] + [a + u * ' ' + b for a, b in zipped_lines] | ||
return lines, n + m + u, max(p, q) + 2, n + u // 2 | ||
|
||
|
||
def find_i(self,target): #implements binary for node in the graph | ||
cur_node = self.root #delcaing the current node | ||
while cur_node!=None: | ||
if cur_node.data==target: #setting cur_node and target | ||
return True | ||
elif cur_node.data>target: #checking if target greter than current node | ||
cur_node = cur_node.left #assigng the current node as the left of the node | ||
else: | ||
cur_node=cur_node.right #assigng the current node as the right of the node | ||
return False | ||
def find_r(self,target): #finding the node to remove node | ||
if self.root: | ||
if self._find_r(target,self.root): #funtion for node in recursively | ||
return True | ||
return False | ||
else: | ||
return None | ||
def _find_r(self,target,cur_node): # funtion binar search recursion | ||
if target > cur_node.data and cur_node.right: #if target is right of the node | ||
return self._find_r(target,cur_node.right) #recursive funtion is called target is right of the node | ||
elif target < cur_node.data and cur_node.left: #if the target is left of the nodec | ||
return self._find_r(target,cur_node.left) #recursive funtion is called target is right of the node | ||
if target ==cur_node.data: #values of data equl target | ||
return True #its returned | ||
def if_left_and_right(self,node): #funtion for deleting the node | ||
delete_node_parent=node | ||
delete_node=node.right | ||
while delete_node.left: | ||
delete_node_parent=delete_node | ||
delete_node=delete_node.left | ||
node.data = delete_node.data | ||
if delete_node.right: | ||
if delete_node_parent.data > delete_node.data: | ||
delete_node.left = delete_node.right | ||
else: | ||
delete_node_parent.right=delete_node.right | ||
else: | ||
if delete_node.data < delete_node_parent.data: | ||
delete_node_parent.left=None | ||
else: | ||
delete_node_parent.right=None | ||
|
||
def remove(self,target): #funtion forremoving the target node | ||
if self.root ==None: | ||
return False | ||
|
||
elif self.root.data==target: #checking data of the not to be target | ||
if self.root.left == None and self.root.right == None:#if the both side of root is none | ||
self.root = None | ||
elif self.root.left and self.root.right == None: #if one of them is nono | ||
self.root = self.root.left | ||
elif self.root.left == None and self.root.right: #if the other one is nono | ||
self.root = self.root.right | ||
elif self.root.left and self.root.right : | ||
bst.if_left_and_right(self.root) #intialting recursive loop | ||
parent = None #setting parent none | ||
node= self.root | ||
while node and node.data != target: | ||
parent=node | ||
if target < node.data: #if target less than target | ||
node = node. left | ||
elif target > node.data: #if target greater than target | ||
node = node.right | ||
if node == None or node.data != target: #if one is nono other is not target | ||
return False | ||
elif node.left== None and node.right==None: #bith of them are None | ||
if target < parent.data: #if target less than parent | ||
parent.left =None | ||
else: | ||
parent.right=None | ||
return True | ||
elif node.left and node.right==None: # | ||
if target< parent.data: | ||
parent.left =node.right | ||
else: | ||
parent.right = node.right | ||
return True | ||
else: | ||
bst.if_left_and_right(node) #funtion for deleting the code | ||
|
||
#example calls, which construct and display the tree | ||
bst = BinaryTree() | ||
bst.insert(4) | ||
bst.insert(2) | ||
bst.insert(6) | ||
bst.insert(1) | ||
bst.insert(3) | ||
bst.insert(5) | ||
bst.insert(7) | ||
bst.insert(8) | ||
bst.insert(9) | ||
bst.insert(10) | ||
bst.insert(11) | ||
bst.insert(12) | ||
bst.insert(13) | ||
bst.insert(14) | ||
bst.insert(15) | ||
bst.insert(100) | ||
bst.insert(200) | ||
|
||
bst.display(bst.root) | ||
|
||
print(bst.find_i(8)) | ||
print(bst.find_r(17)) | ||
print (bst.remove(12)) | ||
bst.display(bst.root) | ||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import math | ||
|
||
class Node: #intialising a node for bst tree | ||
def __init__(self, data = None): | ||
self.data = data #data part on center node | ||
self.left = None #data on right side of the node | ||
self.right = None #data on right side of the node | ||
|
||
""" BST class with insert and display methods. display pretty prints the tree | ||
""" | ||
class BinaryTree: #class for the binary tree | ||
def __init__(self): | ||
self.root = None #declaring root as empty | ||
|
||
def insert(self, data): #funtion for checking if root is empty | ||
if self.root is None: #checking if funtion is empty | ||
self.root = Node(data) | ||
else: | ||
self._insert(data, self.root) #else insering the data to root node | ||
|
||
def _insert(self, data, cur_node): #funtion for inserion of data to current node | ||
if data < cur_node.data: #if data is smaller than the current node | ||
if cur_node.left is None: # checks if its empty | ||
cur_node.left = Node(data)#it is them appended left side of the list | ||
else: | ||
self._insert(data, cur_node.left) #its recursed through the loop | ||
elif data > cur_node.data: #data of current node is greater than current node | ||
if cur_node.right is None: #checking if node is empty | ||
cur_node.right = Node(data) #Node is appended to the | ||
else: | ||
self._insert(data, cur_node.right) #it is them appended right side of the list | ||
else: | ||
print("Value already present in tree") #print value is present | ||
|
||
def display(self, cur_node): #funtion for displaying the current node | ||
lines, _, _, _ = self._display(cur_node) | ||
for line in lines: | ||
print(line) | ||
|
||
|
||
def _display(self, cur_node): #funtion for printing tree | ||
|
||
if cur_node.right is None and cur_node.left is None: | ||
line = '%s' % cur_node.data | ||
width = len(line) | ||
height = 1 | ||
middle = width // 2 | ||
return [line], width, height, middle | ||
|
||
if cur_node.right is None: | ||
lines, n, p, x = self._display(cur_node.left) | ||
s = '%s' % cur_node.data | ||
u = len(s) | ||
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s | ||
second_line = x * ' ' + '/' + (n - x - 1 + u) * ' ' | ||
shifted_lines = [line + u * ' ' for line in lines] | ||
return [first_line, second_line] + shifted_lines, n + u, p + 2, n + u // 2 | ||
|
||
if cur_node.left is None: | ||
lines, n, p, x = self._display(cur_node.right) | ||
s = '%s' % cur_node.data | ||
u = len(s) | ||
first_line = s + x * '_' + (n - x) * ' ' | ||
second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' ' | ||
shifted_lines = [u * ' ' + line for line in lines] | ||
return [first_line, second_line] + shifted_lines, n + u, p + 2, u // 2 | ||
|
||
left, n, p, x = self._display(cur_node.left) | ||
right, m, q, y = self._display(cur_node.right) | ||
s = '%s' % cur_node.data | ||
u = len(s) | ||
first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s + y * '_' + (m - y) * ' ' | ||
second_line = x * ' ' + '/' + (n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' ' | ||
if p < q: | ||
left += [n * ' '] * (q - p) | ||
elif q < p: | ||
right += [m * ' '] * (p - q) | ||
zipped_lines = zip(left, right) | ||
lines = [first_line, second_line] + [a + u * ' ' + b for a, b in zipped_lines] | ||
return lines, n + m + u, max(p, q) + 2, n + u // 2 | ||
|
||
|
||
def find_i(self,target): #implements binary for node in the graph | ||
cur_node = self.root #delcaing the current node | ||
while cur_node!=None: | ||
if cur_node.data==target: #setting cur_node and target | ||
return True | ||
elif cur_node.data>target: #checking if target greter than current node | ||
cur_node = cur_node.left #assigng the current node as the left of the node | ||
else: | ||
cur_node=cur_node.right #assigng the current node as the right of the node | ||
return False | ||
def find_r(self,target): #finding the node to remove node | ||
if self.root: | ||
if self._find_r(target,self.root): #funtion for node in recursively | ||
return True | ||
return False | ||
else: | ||
return None | ||
def _find_r(self,target,cur_node): # funtion binar search recursion | ||
if target > cur_node.data and cur_node.right: #if target is right of the node | ||
return self._find_r(target,cur_node.right) #recursive funtion is called target is right of the node | ||
elif target < cur_node.data and cur_node.left: #if the target is left of the nodec | ||
return self._find_r(target,cur_node.left) #recursive funtion is called target is right of the node | ||
if target ==cur_node.data: #values of data equl target | ||
return True #its returned | ||
def if_left_and_right(self,node): #funtion for deleting the node | ||
delete_node_parent=node | ||
delete_node=node.right | ||
while delete_node.left: | ||
delete_node_parent=delete_node | ||
delete_node=delete_node.left | ||
node.data = delete_node.data | ||
if delete_node.right: | ||
if delete_node_parent.data > delete_node.data: | ||
delete_node.left = delete_node.right | ||
else: | ||
delete_node_parent.right=delete_node.right | ||
else: | ||
if delete_node.data < delete_node_parent.data: | ||
delete_node_parent.left=None | ||
else: | ||
delete_node_parent.right=None | ||
|
||
def remove(self,target): #funtion forremoving the target node | ||
if self.root ==None: | ||
return False | ||
|
||
elif self.root.data==target: #checking data of the not to be target | ||
if self.root.left == None and self.root.right == None:#if the both side of root is none | ||
self.root = None | ||
elif self.root.left and self.root.right == None: #if one of them is nono | ||
self.root = self.root.left | ||
elif self.root.left == None and self.root.right: #if the other one is nono | ||
self.root = self.root.right | ||
elif self.root.left and self.root.right : | ||
bst.if_left_and_right(self.root) #intialting recursive loop | ||
parent = None #setting parent none | ||
node= self.root | ||
while node and node.data != target: | ||
parent=node | ||
if target < node.data: #if target less than target | ||
node = node. left | ||
elif target > node.data: #if target greater than target | ||
node = node.right | ||
if node == None or node.data != target: #if one is nono other is not target | ||
return False | ||
elif node.left== None and node.right==None: #bith of them are None | ||
if target < parent.data: #if target less than parent | ||
parent.left =None | ||
else: | ||
parent.right=None | ||
return True | ||
elif node.left and node.right==None: # | ||
if target< parent.data: | ||
parent.left =node.right | ||
else: | ||
parent.right = node.right | ||
return True | ||
else: | ||
bst.if_left_and_right(node) #funtion for deleting the code | ||
|
||
#example calls, which construct and display the tree | ||
bst = BinaryTree() | ||
bst.insert(4) | ||
bst.insert(2) | ||
bst.insert(6) | ||
bst.insert(1) | ||
bst.insert(3) | ||
bst.insert(5) | ||
bst.insert(7) | ||
bst.insert(8) | ||
bst.insert(9) | ||
bst.insert(10) | ||
bst.insert(11) | ||
bst.insert(12) | ||
bst.insert(13) | ||
bst.insert(14) | ||
bst.insert(15) | ||
bst.insert(100) | ||
bst.insert(200) | ||
|
||
bst.display(bst.root) | ||
|
||
print(bst.find_i(8)) | ||
print(bst.find_r(17)) | ||
print (bst.remove(12)) | ||
bst.display(bst.root) | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.