如何创建具有两个 int 值的二叉树?
我正在尝试创建包含两个 int 值和一个按字典顺序排序的字符串值的二叉树,但我不知道该怎么做。我创建了一个数组列表,它已经排序,但是二叉树必须是基于引用的,它没有排序,我正在考虑在创建列表时对列表进行排序。任何人都可以帮忙解决这个问题吗?任何简短的想法将不胜感激。
I'm trying to create binary tree that contains two int values and one string value sorted in the lexicographic, but I'm not sure what to do. I've created an array list, which has been already sorted, but the binary tree has to be a reference-based which is not sorted and I'm thinking about sorting the list while creating it. Can any one help with this? Any brief idea would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
二叉树是一个递归的东西。创建一个名为 BinaryTree 的类(我希望您使用的是 C++、.NET 或 JAVA),该类具有对另外两个 BinaryTree 的两个引用(默认为 null)。然后创建一个递归的插入函数。
我不知道你想要完成什么,但是在构建二叉树时,通常找不到数组。
Binary tree is a recursive thing. Make a class called BinaryTree (i hope you are in C++, or .NET or JAVA) that has two references to two other BinaryTrees (null by default). Then make an insert function that is recursive.
I don't know what you are trying to accomplish, but when building a binary tree, arrays are usually nowhere to be found.
您首先应该创建一个类来存储数据并实现
Comparable
或使用Comparator
。然后使用实现
SortedSet
的Collection
来存储数据,TreeSet 是一个不错的选择。SortedSet
中的对象通过引用存储,因此如果您修改局部变量中的值集,该集合中的值也会被修改。编辑:如果我正确理解了您关于基于引用的列表的问题,那么在 Java 中以下是可能的。
You first should create a class to store your data and implement
Comparable
or use aComparator
.Then use a
Collection
that implementsSortedSet
to store your data, TreeSet is a good choice. The objects in theSortedSet
are stored by reference so if you modify a value set in a local variable it will be modified in the collection as well.Edit: If I understood your question about reference based lists correctly the following is possible in Java.
听起来您已经有一个数据结构来存储两个 int 值和一个字符串(因为您已将它们排序在数组列表中)。您可以将此数据结构包含在“树节点”中。一个节点通常有一个指向父节点(除非它是根节点)的引用指针和 2 个子节点。
由于您希望对树进行排序,因此您真正需要的是一种特殊形式的二叉树,称为堆。下面的二叉堆维基百科页面的链接有一个算法来展示如何对二叉堆进行排序。
http://en.wikipedia.org/wiki/Binary_heap
以下是有关堆的一些更一般的信息和树木。
http://en.wikipedia.org/wiki/Binary_tree
http://en.wikipedia.org/wiki/Heap_(data_struct)
编辑:你不必使用字面树结构以树形式存储数据。使用数组构建树是完全可以接受的。您可以计算数组的索引,而不是使用引用指针(父节点和 1 或 2 个子节点)。每组子项都被视为树中的一个“行”。根元素位于零行。第一排有两个孩子。根的子级的子级位于第二行,依此类推。
使用此模式,可以使用 array[2*n+1] 和 array[2*n+2] 找到任何节点的子节点,其中 n 是父节点的行。可以使用 array[floor( (n-1)/2)] 找到任何节点的父节点。
It sounds like you already have a data structure to store your two int values and a string (since you have them sorted in an array list). You can include this data structure in a "tree node". A node typically has a reference pointer to a parent node (unless it is the root node) and 2 child nodes.
Since you want the tree to be sorted what you're really after is a special form of binary tree called a heap. The link to the Binary Heap wikipedia page below has an algorithm to show how to sort a binary heap.
http://en.wikipedia.org/wiki/Binary_heap
Here's some more general information on heaps and trees.
http://en.wikipedia.org/wiki/Binary_tree
http://en.wikipedia.org/wiki/Heap_(data_structure)
EDIT: You don't have to use a literal tree structure to store the your data in a tree form. It is perfectly acceptable to build a tree using an array. Instead of using reference pointers (parent and 1 or 2 child nodes) you can compute an index into the array. Each set of children is considered a "row" in the tree. The root element is on the zero row. It's two children are on the first row. The children of the root's children are on the second row, and so on.
Using this pattern the children of any node can be found using array[2*n+1] and array[2*n+2] where n is the row of the parent node. The parent of any node can be found by using array[floor( (n-1)/2)].