简体   繁体   English

如何创建具有两个int值的二叉树?

[英]How do I create a binary tree with two int values?

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. 我正在尝试创建包含两个int值和一个按字典顺序排序的字符串值的二叉树,但是我不确定该怎么做。 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. 任何简短的想法将不胜感激。

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). 创建一个名为BinaryTree的类(我希望您使用C ++或.NET或JAVA),该类具有对另外两个BinaryTrees的两个引用(默认为null)。 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. 我不知道您要完成什么,但是在构建二叉树时,通常找不到数组。

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). 听起来您已经有一个数据结构来存储两个int值和一个字符串(因为您已将它们排序在数组列表中)。 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. 一个节点通常具有指向父节点(除非它是根节点)和2个子节点的引用指针。

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. 指向下面的Binary Heap Wikipedia页面的链接具有一种算法,用于显示如何对二进制堆进行排序。

http://en.wikipedia.org/wiki/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/Binary_tree

http://en.wikipedia.org/wiki/Heap_(data_structure) 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. 可以使用数组中的索引来代替使用引用指针(父节点和1或2个子节点)。 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. 使用此模式,可以使用array [2 * n + 1]和array [2 * n + 2]找到任何节点的子节点,其中n是父节点的行。 The parent of any node can be found by using array[floor( (n-1)/2)]. 可以使用array [floor((n-1)/ 2)]找到任何节点的父节点。

You first should create a class to store your data and implement Comparable or use a Comparator . 首先,您应该创建一个类来存储数据并实现Comparable或使用Comparator

public class Data { // Implement Comparable...
    private String s;
    private int n1;
    private int n2;

    // Implement constructors, getters, setters based on what you need...

    // Implement compareTo (+ equals + hashCode) unless your going with Comparator
}

Then use a Collection that implements SortedSet to store your data, TreeSet is a good choice. 然后使用实现SortedSetCollection来存储您的数据,TreeSet是一个不错的选择。 The objects in the SortedSet are stored by reference so if you modify a value set in a local variable it will be modified in the collection as well. SortedSet中的对象是按引用存储的,因此,如果您修改局部变量中的值集,则该值也会在集合中被修改。

Edit : If I understood your question about reference based lists correctly the following is possible in Java. 编辑 :如果我正确理解了您有关基于引用的列表的问题,则可以在Java中进行以下操作。

List<Data> dataList = // Create list and add data into it.
Data data = dataList.get(4);
data.setS(103); // Modifies S in the local data-object and in dataList because they are reference based.

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

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