简体   繁体   English

如何为树节点分配唯一编号,以便可以从树叶中追溯父级?

[英]How to assign unique numbers to the tree nodes so that from the tree leaf I can trace back the parents?

I have to structure certain data and tree data structure suited the requirement. 我必须构造某些适合数据需求的数据和树数据结构。
I have to assign certain number to each node in such a manner that I can trace back the parent from its number. 我必须以某种方式为每个节点分配一定的编号,以便可以从其编号追溯到父节点。 I am planning to use hashtable to store these numbers as keys, so there cannot be any duplicate value. 我打算使用哈希表将这些数字存储为键,因此不能有任何重复的值。

eg 例如

parent - 000001 父母-000001
child1 - 000011 儿童1-000011
innerchild1 - 000111 (level 2, get 2 bits from right and we can reach parent) innerchild1-000111(级别2,从右边获得2位,我们可以到达父级)
innerchild2 - 000211 innerchild2-000211
child2 - 000021 儿童2-000021
innerchild1 - 000121 innerchild1-000121
innerchild2 - 000221 innerchild2-000221

Depending on the level, I can mask certain bits and I can uniquely identify the parent. 根据级别,我可以屏蔽某些位,并且可以唯一地标识父级。 But if my tree grows wider(ie more parents, numbers will duplicate) How to overcome this problem? 但是,如果我的树长得更大(即更多的父母,数字将重复),如何克服这个问题?

I'd advice you to use numbers with digits 1-9. 我建议您使用1到9的数字。

Why no-0? 为什么没有0? 9-digit number with leading 0 looks as 8-digit on without leading 0. It will cause errors. 带前导0的9位数字看起来像是8位,而没有前导0。这将导致错误。

Thus, till any parent has maximally 9 children, the structure will suffice. 因此,在任何父母最多有9个孩子之前,该结构就足够了。

number 0 - common root 数字0-公用根
1-9 - first children 1-9-第一个孩子
11-19 - second generation children of the first child... 11-19-第一个孩子的第二代孩子...
and so on 等等

If you'll have nodes with more than 9 digits, use 100-base arithmetic. 如果您的节点数超过9位,请使用100基算术。 (allowed digits will be 10-99). (允许的数字为10-99)。 Are 99 children enough? 99个孩子够吗? If not, use 100-999. 如果不是,请使用100-999。

Nested set tree can be used yo solve the problem. 可以使用嵌套集树解决此问题。 Please refer to the book below. 请参考下面的书。 Its an amazing book, which also contain text on Nested Set trees. 这是一本了不起的书,其中还包含有关嵌套集树的文本。

http://www.elsevier.com/wps/find/bookdescription.cws_home/702605/description http://www.elsevier.com/wps/find/bookdescription.cws_home/702605/description

Your approach of assigning "special" numbers to nodes sounds like a bad one - likely to lead to a lot of hard work, scalability limitations and most of all... bugs. 您为节点分配“特殊”数字的方法听起来很糟糕-可能会导致大量的工作,可伸缩性限制以及最主要的问题。

If you need to know what the parent node is, store a reference to the parent node in a field of the node! 如果您需要知道父节点是什么,请将对父节点的引用存储在该节点的字段中!

I don't think a "well defined" number as key is good enough. 我认为作为关键字的“定义明确”的数字不够好。

You need design a tree structure for your requirement or at least a Node class to get parentId/parent Node easier. 您需要为您的需求或至少一个Node类设计一个树形结构,以使parentId / parent节点更容易。

for example 例如

Node{
 id, #could be string, number, unique
 actualNode # you node value
 parentId (or Node parentNode )
}

if you would stick to hashtable for some reason, you can still use the node.id as key. 如果由于某种原因坚持使用哈希表,您仍然可以使用node.id作为键。 there are a number of ways to generate unique string, numbers. 有很多方法可以生成唯一的字符串,数字。 eg current nano-seconds ... 例如当前的纳秒...

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

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