简体   繁体   English

试图从存储在二进制文件中的预序遍历中构建一棵树?

[英]Trying to build a tree out of a pre order traversal which is stored in a binary file?

Example of a binary file I have:我有一个二进制文件的例子:

0000000: 11111011 11111111 11111111 11111111 00000001 11111100  ......
0000006: 11111111 11111111 11111111 00000001 11111101 11111111  ......
000000c: 11111111 11111111 00000001 11111110 11111111 11111111  ......
0000012: 11111111 00000001 11111111 11111111 11111111 11111111  ......
0000018: 00000001 00000000 00000000 00000000 00000000 00000001  ......
000001e: 00000001 00000000 00000000 00000000 00000001 00000010  ......
0000024: 00000000 00000000 00000000 00000001 00000101 00000000  ......
000002a: 00000000 00000000 00000001 00000100 00000000 00000000  ......
0000030: 00000000 00000000                                      ..

This is txt representation of that same file where each line represents a node:这是同一文件的 txt 表示,其中每一行代表一个节点:

-5 1
-4 1
-3 1
-2 1
-1 1
 0 1
 1 1
 2 1
 5 1
 4 0

Where the first number is the value of the node.其中第一个数字是节点的值。 And the next number represents how many children the node has.下一个数字代表节点有多少个孩子。 0 means no children. 0 表示没有孩子。 1 means one right child. 1 表示一个右子。 2 means one left child. 2 表示一个左孩子。 3 means two children. 3 表示两个孩子。

This is the structure of a node:这是一个节点的结构:

struct Tnode {
int key;
char child;
struct Tnode * left;
struct Tnode * right;
} Tnode;

Currently for each node I am trying to use fread twice, one to get the int key, and then to get the char child.目前,对于每个节点,我尝试使用 fread 两次,一次获取 int 键,然后获取 char 子项。 But how can I actually build the tree given that the binary tree contains the pre-order traversal ?但是,鉴于二叉树包含先序遍历,我该如何实际构建树? The first node we encounter has to be the root node.我们遇到的第一个节点必须是根节点。 Since it has a 1 next to it, it will only have a right child (this will be stored in char child).因为它旁边有一个 1,所以它只有一个右孩子(这将存储在 char 孩子中)。 Therefore, the next node has to be the right child of that, as and so on.因此,下一个节点必须是它的右孩子,依此类推。 Basically, with this example, the right keeps on going on the right until it ends with 4. But I am having trouble building it.基本上,在这个例子中,右边一直在右边,直到它以 4 结束。但是我在构建它时遇到了麻烦。 Any tips?有小费吗?

You can solve this probem using recursion :您可以使用递归解决这个问题:

You create a function process_node which takes one parameter, which is the address of the pointer which should point to the new node.您创建了一个函数process_node ,它接受一个参数,即指向新节点的指针的地址。 When calling the function for the first time, this will be the address of the pointer to the root node.第一次调用该函数时,这将是指向根节点的指针的地址。

The function process_node does the following:函数process_node执行以下操作:

  1. It allocates memory for one struct Tnode .它为一个struct Tnode分配内存。
  2. It then reads the int key and the char child from file and writes that information to the newly allocated struct Tnode .然后它从文件中读取int keychar child ,并将该信息写入新分配的struct Tnode
  3. It then writes the address of the newly created node to the address it received as a function parameter (which on the first function call is the address of the pointer to the root node).然后它将新创建的节点的地址写入它作为函数参数接收的地址(在第一个函数调用中是指向根节点的指针的地址)。 That way, the new node is now properly linked to the tree.这样,新节点现在正确链接到树。
  4. If the new node has a right child, then the function process_node will now recursively calls itself, but the function parameter of this function call will this time be the address of its right member variable.如果新节点有一个右孩子,那么函数process_node现在将递归调用自己,但这次函数调用的函数参数将是它的right成员变量的地址。 That way, the newly called function will write to that address instead.这样,新调用的函数将改为写入该地址。 If the node does not have a right child, then the right member variable is set to NULL .如果节点没有右孩子,则right成员变量设置为NULL
  5. Step 4 is repeated for the left child.对左孩子重复步骤 4。
  6. The function returns.函数返回。

After all recursive function calls return, you should have a fully built binary tree.在所有递归函数调用返回后,您应该有一个完全构建的二叉树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM