1

I created a tree structure using python. Assume the tree has root node, left and right child. I need to acceess the tree whenever required. But on opening the file and running makes recreation of tree. As i have huge number of nodes in tree, so the method of recreation is time consuming. Please provide me a solution such that i can store the tree, fetch and update(insert or delete a node) whenever required.

Rohit
  • 11
  • 1
  • Welcome to StackOverflow. Your question is to broad and lacks of informations and a minimal working example. Please visit https://stackoverflow.com/help and read there to find out how to ask questions. Beside this maybe the `json` module could be your friend. I solve similiar task with it. – buhtz Jun 19 '20 at 09:15

1 Answers1

1

you could use the pickle module that can be used to serialize and deserialize python objects quite easily.

import pickle

tree = {'a': 'b'}
# serializing the data
with open('/path/to/some/file', 'wb') as f:
    pickle.dump(tree, f, pickle.HIGHEST_PROTOCOL)

# and deserializing it again
with open('/path/to/same/file', 'rb') as f:
    tree = pickle.load(f)