Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DIVIDER = 2 * ' '
- class Node():
- def __init__(self, name, parent=None):
- self.nodes = []
- self.name = name
- self.parent = parent
- def add_node(self, node):
- self.nodes.append(node)
- node.change_parent(self)
- def get_nodes(self):
- return self.nodes
- def change_parent(self, parent):
- self.parent = parent
- def get_specfic_node(self, name):
- for node in self.nodes:
- if node.name == name:
- return node
- def print_tree(root):
- print(tree_getter(root)[:-1])
- def write_to_file(filename):
- string = tree_getter(root)
- string = string[:-1]
- print(string)
- with open(filename, 'w') as f:
- f.write(string)
- print('done')
- return 0
- def front_digit_count(string, other):
- count = 0
- string_index = 0
- other_index = 0
- while True:
- if string[string_index] != other_index:
- return count
- string_index += 1
- other_index += 1
- if string_index == len(string):
- return count
- if other_index == len(other):
- other_index = 0
- count += 1
- def read_from_file(filename):
- with open(filename, 'r') as f:
- readlist = f.read()
- readlist = readlist.split('\n')
- cur_parents = []
- current = 1
- root = Node(readlist[0])
- cur_parents.append(root)
- while current != len(readlist):
- prev = readlist[current - 1]
- cur = readlist[current]
- prev_indentation = front_digit_count(prev, DIVIDER)
- cur_indentation = front_digit_count(cur, DIVIDER)
- prev_ind = len(DIVIDER) * prev_indentation
- cur_ind = len(DIVIDER) * cur_indentation
- prev_name = prev[prev_ind:]
- cur_name = cur[cur_ind:]
- if prev_indentation + 1 == cur_indentation:
- cur_parents.append(Node(prev_name))
- elif prev_indentation > cur_indentation:
- diff = prev_indentation - cur_indentation
- cur_parents = cur_parents[:-diff]
- cur_parents[-1].add_node(Node(cur_name))
- current += 1
- return root
- def tree_getter(root, indentation=0, stringy=''):
- stringy += (indentation * DIVIDER) + root.name + '\n'
- for node in root.get_nodes():
- stringy += tree_getter(node, indentation+1)
- return stringy
- def add_nodes_to_root(root, nodes):
- for node in nodes:
- root.add_node(Node(node))
- def traverse_one_direction(root, nodes):
- current = root
- for node in nodes:
- tempo = current.get_specfic_node(node)
- if tempo == None:
- print('node not in there')
- else:
- current = tempo
- return current
- if __name__ == '__main__':
- root = Node('start')
- current_root = root
- while True:
- inp = input('\n')
- inp = inp.split(' ')
- arg, anp = inp[0], inp[1:]
- if arg == '0':
- print("current_root is " + current_root.name)
- elif arg == '1':
- print_tree(root)
- elif arg == '2':
- add_nodes_to_root(current_root, anp)
- elif arg == '3':
- current_root = traverse_one_direction(current_root, anp)
- elif arg == '4':
- if current_root == root:
- print('current_root is the main root')
- continue
- current_root = current_root.parent
- elif arg == '5':
- write_to_file(anp[0])
- elif arg == '6':
- root = read_from_file(anp[0])
- current_root = root
- elif arg == '7':
- root = Node('start')
- current_root = root
- # add ability to remove nodes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement