Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- text1 = open('keysInsert.txt').read()
- text2 = open('keysDelete.txt').read()
- print (text1)
- print (text2)
- posplit1 = text1.split()
- posplit2 = text2.split()
- print (posplit1)
- print (posplit2)
- h = len(posplit1)
- g = len(posplit2)
- print(h)
- for x in range(h):
- posplit1[x] = int(posplit1[x])
- for x in range(g) :
- posplit1[x] = int(posplit1[x])
- print(posplit2[1])
- class BSTreeNode:
- def __init__(self, value):
- self.value = value
- self.left = None
- self.right = None
- def insert(self,key):
- if self.value == key:
- self.right = BSTreeNode(key)
- print ("Został dodany wierzchołek:", posplit1[x])
- elif self.value > key:
- if self.left is not None:
- return self.left.insert(key)
- print("Został dodany wierzchołek:", posplit1[x])
- else:
- self.left = BSTreeNode(key)
- print("Został dodany wierzchołek:", posplit1[x])
- return True
- else:
- if self.right is not None:
- return self.right.insert(key)
- print("Został dodany wierzchołek:", posplit1[x])
- else:
- self.right = BSTreeNode(key)
- print("Został dodany wierzchołek:", posplit1[x])
- return False
- def delete(self, key) :
- """ delete the node with the given key and return the
- root node of the tree """
- if self.key == key :
- if self.right and self.left :
- [psucc, succ] = self.right._findMin(self)
- if psucc.left == succ :
- psucc.left = succ.right
- else :
- psucc.right = succ.right
- succ.left = self.left
- succ.right = self.right
- return succ
- else :
- if self.left :
- return self.left
- else :
- return self.right
- else :
- if self.key > key :
- if self.left :
- self.left = self.left.delete(key)
- else :
- if self.right :
- self.right = self.right.delete(key)
- return self
- def _findMin(self, parent) :
- """ return the minimum node in the current tree and its parent """
- if self.left :
- return self.left._findMin(self)
- else :
- return [parent, self]
- class BSTree:
- def __init__(self):
- self.root = None
- def delete(self,key):
- self.root.delete(self.root,key)
- def insert(self,data):
- if self.root:
- self.root.insert(data)
- else:
- self.root = BSTreeNode(data)
- return True
- def find_min(self,node):
- current_node = node
- while current_node.left:
- current_node = current_node.left
- return current_node
- #znalazłem nie wiem jak przerobić
- def height(bst) :
- if isempty(bst) :
- return 0
- else :
- return 1 + max(height(bst.left), height(bst.right))
- def get_min(node):
- current_node = node
- while current_node.left:
- current_node = current_node.left
- return str(current_node.value)
- def print_helper(root, indent):
- if root is not None:
- print_helper(root.right, indent + " ")
- print (indent + str(root.value))
- print_helper(root.left, indent + " ")
- def print_tree(root):
- print_helper(root, "")
- bst = BSTree()
- for x in range(h):
- bst.insert(posplit1[x])
- #nie działa
- # for x in range(g) :
- # bst.delete(posplit2[x])
- print_tree(bst.root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement