def sort(self): nums = self.getTextList() self.root = Node(0) for num in nums: self.root.insert(int(num)) self.circles_point = [] self.font_point = [] self.line_point = [] self.setPoint(self.root, rootx, rooty, Rx, Ry) self.setFont(self.root, rootx, rooty, Rx, Ry) self.setLine(self.root, rootx, rooty, Rx, Ry) self.draw()
class Win(QMainWindow, Ui_Frame): def __init__(self): super().__init__() self.setupUi(self) self.pushButton.clicked.connect(self.sort) def sort(self): nums = self.getTextList() self.root = Node(0) for num in nums: self.root.insert(int(num)) self.circles_point = [] self.font_point = [] self.line_point = [] self.setPoint(self.root, rootx, rooty, Rx, Ry) self.setFont(self.root, rootx, rooty, Rx, Ry) self.setLine(self.root, rootx, rooty, Rx, Ry) self.draw() def getTextList(self): nums = self.lineEdit.text().split(',') return nums def setPoint(self, root, x, y, rx, ry): if root: self.setPoint(root.left, x - rx, y + ry, rx / 1.5, ry / 1.5) self.circles_point.append([QPoint(x, y), Radix, Radix]) self.setPoint(root.right, x + rx, y + ry, rx / 1.5, ry / 1.5) def setFont(self, root, x, y, rx, ry): if root: self.setFont(root.left, x - rx, y + ry, rx / 1.5, ry / 1.5) self.font_point.append([ QRectF(x - Radix, y - Radix, 2 * Radix, 2 * Radix), str(root.data) ]) self.setFont(root.right, x + rx, y + ry, rx / 1.5, ry / 1.5) def setLine(self, root, x, y, rx, ry): if root: if root.left: self.line_point.append([x, y, x - rx, y + ry]) if root.right: self.line_point.append([x, y, x + rx, y + ry]) self.setLine(root.left, x - rx, y + ry, rx / 1.5, ry / 1.5) self.setLine(root.right, x + rx, y + ry, rx / 1.5, ry / 1.5) def draw(self): cir = draw_circle(self.circles_point, self.font_point, self.line_point) self.scrollArea.setWidget(cir)
def advance(self): page_num = self.page_num page = self.table.pager.get_page(page_num) node = Node.deserialize_from(page) self.cell_num += 1 if self.cell_num >= node.num_cells: self.is_at_end_of_table = True
def insert_leaf_node(self, key, row): current_page = self.table.pager.get_page(self.page_num) current_node = Node.deserialize_from(current_page) num_cells = current_node.num_cells if num_cells >= LEAF_NODE_MAX_CELLS: # node is full raise NotImplementedError("need to implement splitting leaf nodes") exit(0)
def insert(self,data): ''' Inserts a new node. ''' if not self.getRoot(): #if tree is empty, create root first self.setRoot(data) else: parent,match_node = self.find(data,return_parent=True) node = Node(data) if match_node: #if duplicate key is present node.right = match_node.right #shift existing twin's node right subtree as new node's right subtree match_node.right = node #Insert twin node as existing twin node's right child elif data > parent.data: #node to be parent's right child assert(not parent.right) parent.right = node elif data < parent.data: #node to be parent's right child assert(not parent.left) parent.left = node
def __init__(self, table, page_num, is_at_end_of_table): # type: (Table, int, bool) -> None self.table = table self.page_num = table.root_page_num self.cell_num = 0 root_node_page = table.pager.get_page(table.root_page_num) root_node = Node.deserialize_from(root_node_page) num_cells = root_node.num_cells self.is_at_end_of_table = num_cells == 0
def table_end(cls, table): # type: (Table) -> Cursor page_num = table.root_page_num root_node_page = table.pager.get_page(table.root_page_num) root_node = Node.deserialize_from(root_node_page) num_cells = root_node.num_cells is_at_end_of_table = True return cls(table, num_cells, is_at_end_of_table)
def table_start(cls, table): # type: (Table) -> Cursor page_num = table.root_page_num cell_num = 0 root_node_page = table.pager.get_page(table.root_page_num) root_node = Node.deserialize_from(root_node_page) num_cells = root_node.num_cells is_at_end_of_table = num_cells == 0 return cls(table, cell_num, is_at_end_of_table)
def dfs(node: Node) -> None: if not node: return dfs(node.left) if self.largest: #Indicates that the minimum node has been reached self.largest.right = node node.left = self.largest else: self.smallest = node self.largest = node dfs(node.right)
def __init__(self, filename): # type: (Text) -> None self.filename = filename self.pager = Pager.open(filename, max_pages=Table.MAX_PAGES) self.root_page_num = 0 if self.pager.num_pages == 0: # new db file root_node_page = self.pager.get_page(0) root_node = Node.initialize_root_node() nodes[id(root_node)] = root_node
def decode_btree(obj): if hasattr(obj, '__iter__') and '__class__' in obj: cls_name = obj['__class__'] data = obj['data'] if cls_name == 'Leaf': obj = Leaf(tree, bucket=bucket_to_lazynodes(data, tree)) elif cls_name == 'Node': bucket = bucket_to_lazynodes(data['bucket'], tree) obj = Node(tree, bucket=bucket, rest=LazyNode(offset=data['rest'], tree=tree)) else: tree.max_size = data['max_size'] tree.root = LazyNode(offset=data['root'], tree=tree) obj = tree return obj
def add_node(self, data): node = Node(data) if self.root is None: self.root = node return current = self.root while True: if current.data < node.data: if current.right is None: current.right = node return current = current.right else: if current.left is None: current.left = node return current = current.left
def __init__(self, data, deep=0): Node.__init__(self, data, deep) self.color = 'R' # ONLY in big 'R' and 'B'
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from btree import BTree, Node, flip_tree tree = BTree(4, Node(2, Node(1), Node(3)), Node(7, Node(6), Node(9))) tree.tree_print() flip_tree(tree) print("\nFlipping tree...") tree.tree_print()
def location(self): # type: () -> Tuple[Page, int] page_num = self.page_num page = self.table.pager.get_page(page_num) return Node.deserialize_from(page).value(self.cell_num)
from btree import Node root = Node(0) for i in range(7): root.random_insert(root, Node(i + 1)) print("=========================================================") print("Printing tree") print("=========================================================") root.btree_print(root) print("=========================================================") print("Printing all visited nodes with BFS") print(root.traverse_queue(root)) print("=========================================================") print("Printing all visited nodes with DFS") print(root.traverse_stack(root)) print("=========================================================")