C# 如何创建二叉树

C# 如何创建二叉树,c#,data-structures,binary-tree,C#,Data Structures,Binary Tree,我不是说二叉搜索树 比如说,, 如果我在二叉搜索树中插入值1,2,3,4,5,按序遍历将给出 1,2,3,4,5作为输出 但是,如果我在二叉树中插入相同的值,顺序遍历应该会给出 4,2,5,1,3作为输出 可以使用动态数组创建二叉树,其中对于索引n中的每个元素, 2n+1和2n+2分别表示其左和右子代 因此,在这里表示和级别顺序遍历非常容易 但我认为,顺序、后顺序、前顺序都很难 我的问题是如何创建一个二叉树,就像二叉搜索树一样。 即。 有一个树类,它包含数据、左指针和右指针,而不是数组。 因此,

我不是说二叉搜索树

比如说,, 如果我在二叉搜索树中插入值1,2,3,4,5,按序遍历将给出 1,2,3,4,5作为输出

但是,如果我在二叉树中插入相同的值,顺序遍历应该会给出 4,2,5,1,3作为输出

可以使用动态数组创建二叉树,其中对于索引n中的每个元素, 2n+1和2n+2分别表示其左和右子代

因此,在这里表示和级别顺序遍历非常容易

但我认为,顺序、后顺序、前顺序都很难

我的问题是如何创建一个二叉树,就像二叉搜索树一样。 即。 有一个树类,它包含数据、左指针和右指针,而不是数组。
因此,我们可以递归地进行遍历。

树类声明部分当然不是这里的难点。在问题中,您基本上明确说明了如何申报:

class BinaryTree
{
private:
    int data;
    BinaryTree *left, *right;
};
这支持各种形式的遍历,如下所示:

void Inorder(const BinaryTree *root)
{
  if(root == 0)
    return;
  Inorder(root->left);
  printf("now at %d\n", root->data);
  Inorder(root->right);
}

您应该能够从中推断出前序和后序遍历。在实际的实现中,树可能会被模板化以存储随机数据,遍历例程当然会更通用(用户数据输入,或者用户提供的每个节点回调,或者其他),因为我没有收到对我所问问题的任何答案,我将使用数组发布我自己的二叉树实现。 现在我知道数组的实现比我想象的要容易,但我仍然不知道如何使用链表实现同样的功能

代码是c语言的#

类二进制树
{
私有静态int MAX_ELEM=100;//数组的初始大小
int-lastElementIndex;
int?[]数据阵列;
公共二叉树()
{
数据数组=新整数?[MAX_ELEM];
lastElementIndex=-1;
}
//函数将数据插入到树中
//作为完整的二叉树插入
公共void insertData(int数据)
{
int?[]温度;
如果(lastElementIndex+1<最大元素)
{
dataArray[++lastElementIndex]=数据;
}
其他的
{//达到限制时将数组的大小增加一倍
温度=新整数?[MAX_ELEM*2];
对于(int i=0;i=(2*索引+1))
收益率(2*index+1);
返回-1;
}
//用于获取数组中元素的正确子级的内部函数
int getRightChild(int索引){
如果(lastElementIndex>=(2*索引+2))
收益率(2*指数+2);
返回-1;
}
//函数检查树是否为空
公共布尔值为空(){
如果(lastElementIndex==-1)
返回true;
返回false;
}
//有序遍历的递归函数
公共void遍历索引(int索引){
如果(索引==-1)
返回;
TraverseInoder(getLeftChild(索引));
Write(“{0}”,dataArray[index]);
TraverseInoder(getRightChild(索引));
}
//用于前序遍历的递归函数
公共void遍历优先级(int索引){
如果(索引==-1)
返回;
Write(“{0}”,dataArray[index]);
traversePreOrder(getLeftChild(索引));
traversePreOrder(getRightChild(索引));
}
//用于后序遍历的递归函数
公共void traversePostOrder(整数索引){
如果(索引==-1)
返回;
TraverseStorder(getLeftChild(索引));
TraverseStorder(getRightChild(索引));
Write(“{0}”,dataArray[index]);
}
//函数以按级别顺序遍历树
public void transverselevelorder()
{
Console.WriteLine(“\n按升序打印树的元素\n”);
如果(lastElementIndex==-1)
{
Console.WriteLine(“空树!…按任意键返回”);
Console.ReadKey();
返回;
}

对于(inti=0;i,如果我理解正确,您希望从数组创建二叉树

int[] values = new int[] {1, 2, 3, 4, 5};
BinaryTree tree = new BinaryTree(values);
这将使用值1-5预填充二叉树,如下所示:

    1
   / \
  2   3
 / \
4   5
这可以使用以下类完成:

class BinaryTree
{
    int value;
    BinaryTree left;
    BinaryTree right;

    public BinaryTree(int[] values) : this(values, 0) {}

    BinaryTree(int[] values, int index)
    {
        Load(this, values, index);
    }

    void Load(BinaryTree tree, int[] values, int index)
    {
        this.value = values[index];
        if (index * 2 + 1 < values.Length)
        {
            this.left = new BinaryTree(values, index * 2 + 1);
        }
        if (index * 2 + 2 < values.Length)
        {
            this.right = new BinaryTree(values, index * 2 + 2);
        }
    }
}
类二进制树
{
int值;
左二叉树;
二叉树右;
公共二进制树(int[]值):此(值,0){}
二进制树(int[]值,int索引)
{
荷载(该荷载、值、索引);
}
无效加载(二进制树,int[]值,int索引)
{
this.value=值[索引];
如果(索引*2+1
如果您正在寻找全面的二进制树实现的源代码,您可以从中学习。

类BstNode
{
公共int数据;
公共节点(int数据)
{
这个数据=数据;
}
公共节点左;
公共节点权;
}
班级计划
{
公共静态BstNode插入(BstNode根,int数据)
{
如果(root==null)root=新节点(数据);
else如果(data root.data)root.right=In
class BinaryTree
{
    int value;
    BinaryTree left;
    BinaryTree right;

    public BinaryTree(int[] values) : this(values, 0) {}

    BinaryTree(int[] values, int index)
    {
        Load(this, values, index);
    }

    void Load(BinaryTree tree, int[] values, int index)
    {
        this.value = values[index];
        if (index * 2 + 1 < values.Length)
        {
            this.left = new BinaryTree(values, index * 2 + 1);
        }
        if (index * 2 + 2 < values.Length)
        {
            this.right = new BinaryTree(values, index * 2 + 2);
        }
    }
}
  class BstNode
    {
        public int data;
        public BstNode(int data)
        {
            this.data = data;
        }
        public BstNode left;
        public BstNode right;
    }
    class Program
    {
        public static BstNode Insert(BstNode root, int data)
        {
            if (root == null) root = new BstNode(data);
            else if (data <= root.data) root.left = Insert(root.left, data);
            else if (data > root.data) root.right = Insert(root.right, data);

            return root;
        }

public static void Main(string[] args)
        {
            // create/insert into BST
            BstNode Root = null;
            Root = Insert(Root, 15);
            Root = Insert(Root, 10);
            Root = Insert(Root, 20);
            Root = Insert(Root, 8);
            Root = Insert(Root, 12);
            Root = Insert(Root, 17);
            Root = Insert(Root, 25);
         }

}