c#创建一个完全二叉树

public class Node
    {
        public int Date { get; set; }
        public Node Leftnode { get; set; }
        public Node Rightnode { get; set; }
        public Node()
        {
            Date = 0;
            Leftnode = null;
            Rightnode = null;
        }
        public Node(int num)
        {
            Date = num;
            Leftnode = null;
            Rightnode = null;
        }
        public Node(int num, Node left, Node right)
        {
            Date = num;
            Leftnode = left;
            Rightnode = right;
        }
        public void Display()
        {
            Console.WriteLine(Date);
            
        }
    }
     public class CreatTree 
    {
        //public Node i = new Node();
        public Node creattree(int x, int n)
        {
            Node i;
            if (x < n)
            {
                
                i = new Node(x);
              //  Console.WriteLine(x);
                i.Leftnode = creattree(2 * x, n);
                i.Rightnode = creattree(2 * x + 1, n);
            }
            else
            {
                i = null;
            }
            return i;
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言实现完全二叉树的代码: ```c #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *left; struct node *right; }; struct node *create_node(int data) { struct node *new_node = malloc(sizeof(struct node)); new_node->data = data; new_node->left = NULL; new_node->right = NULL; return new_node; } struct node *insert_node(struct node *root, int data, int index, int size) { if (index > size) { printf("Invalid index!\n"); return root; } if (root == NULL) { return create_node(data); } if (index <= size / 2) { root->left = insert_node(root->left, data, index, size / 2); } else { root->right = insert_node(root->right, data, index - size / 2, size / 2); } return root; } void inorder_traversal(struct node *root) { if (root == NULL) { return; } inorder_traversal(root->left); printf("%d ", root->data); inorder_traversal(root->right); } int main() { int size = 7; struct node *root = NULL; root = insert_node(root, 1, 1, size); insert_node(root, 2, 2, size); insert_node(root, 3, 3, size); insert_node(root, 4, 4, size); insert_node(root, 5, 5, size); insert_node(root, 6, 6, size); insert_node(root, 7, 7, size); printf("Inorder traversal: "); inorder_traversal(root); printf("\n"); return 0; } ``` 在这个代码中,我们首先定义了一个结构体 `node`,表示二叉树中的节点,包含一个整数数据 `data` 和两个指针 `left` 和 `right`,分别指向左子树和右子树。 接下来,我们定义了一个函数 `create_node()`,用于创建一个新的节点,并返回该节点的指针。 然后,我们定义了一个函数 `insert_node()`,用于向二叉树中插入一个新节点。该函数接受四个参数:根节点指针 `root`、要插入的数据 `data`、要插入的位置 `index` 和二叉树的总大小 `size`。如果插入位置超出了二叉树的范围,函数将会输出一个错误信息并返回根节点指针。如果根节点为空,则直接创建一个新节点并将其作为根节点返回。如果插入位置在左子树范围内,则递归调用 `insert_node()` 函数并将其返回值赋值给当前节点的左子树指针;否则,递归调用 `insert_node()` 函数并将其返回值赋值给当前节点的右子树指针。最后,函数返回根节点指针。 最后,我们定义了一个函数 `inorder_traversal()`,用于中序遍历二叉树,并输出每个节点的数据。该函数接受一个参数:根节点指针 `root`。如果根节点为空,函数将直接返回。否则,先递归遍历左子树,然后输出当前节点的数据,最后递归遍历右子树。 在 `main()` 函数中,我们定义了一个变量 `size`,表示这个完全二叉树的大小。然后,我们创建一个空的根节点指针 `root`,并依次向二叉树中插入七个节点。最后,我们调用 `inorder_traversal()` 函数对二叉树进行中序遍历,并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值