Want to save binary tree to disk for “20 questions” game

后端 未结 8 2138

In short, I\'d like to learn/develop an elegant method to save a binary tree to disk (a general tree, not necessarily a BST). Here is the description of my problem:

I\'

8条回答
  •  無奈伤痛
    2021-02-06 15:41

    The most arbitrary simple way is just a basic format that can be used to represent any graph.

    ,,
    

    Ie:

    "Is it Red", "yes", "does it have wings" 
    "Is it Red", "no" , "does it swim"
    

    There isn't much redundancy here, and the formats mostly human readable, the only data duplication is that there must be a copy of a parent for every direct child it has.

    The only thing you really have to watch is that you don't accidentally generate a cycle ;)

    Unless that's what you want.

    The problem here is rebuilding the tree afterwards. If I create the "does it have wings" object upon reading the first line, I have to somehow locate it when I later encounter the line reading "does it have wings","yes","Has it got a beak?"

    This is why I traditionally just use graph structures in memory for such a thing with pointers going everywhere.

    [0x1111111 "Is It Red"           => [ 'yes' => 0xF752347 , 'no' => 0xFF6F664 ], 
     0xF752347 "does it have wings"  => [ 'yes' => 0xFFFFFFF , 'no' => 0x2222222 ], 
     0xFF6F664 "does it swim"        => [ 'yes' => "I Dont KNOW :( " , ... etc etc ]
    

    Then the "child/parent" connectivity is merely metadata.

提交回复
热议问题