如何合并二叉树以共享相同的根节点? [英] How Can I Merge Binary Trees To Share The Same Root Node?

查看:206
本文介绍了如何合并二叉树以共享相同的根节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我正在尝试使用java构建一个树,我有大约100个IP地址字符串,并创建了为每个IP构建树的树地址字符串。你能告诉我如何将所有树木合并成一棵树吗?

Hi!

I am trying to build a tree using java, i have about 100 strings of IP Addresses and have created trees that build a tree for each IP address string. can you please tell me how i can merge all trees into one tree?

推荐答案

请看我对这个问题的评论。你没有定义你可能需要的操作的结果。



一个可能的定义可能是这个;它还包含一个解决方案。如果您为某种类型(表示此类地址的IP地址或字符串;但最好是IP地址)创建了二叉树结构,那么您肯定有一个方法添加具有节点类型参数的树;它应该为您的树添加任意IP地址。对于合并,您可以从其中一个树开始,并遍历另一个树的所有节点,将每个节点添加到第一个树,对除了用作目标的第一个树之外的所有树重复它。当然,合并的结果可能取决于操作的顺序。



-SA
Please see my comment to the question. You did not define the result of the operation you may need.

One possible definition could be this; and it would also contain a solution in it. If you made a binary tree structure for some type (IP address or string representing such address; but it should better be an IP address), you certainly have a method Add of a tree which has a parameter of the node type; it should add any arbitrary IP address to your tree. For merge, you can start with one of the trees, and traverse all nodes of another tree adding each node to the first tree, repeat it for all the trees except the first you use as a target. Naturally, the result of the merge can depend on the order of operation.

—SA


这篇关于如何合并二叉树以共享相同的根节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆