怎样编写一个程序,把一个有序整数数组放到二叉树中

分析:本题考察二叉搜索树的建树方法,简单的递归结构。
关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。

而,学会把递归改称非递归也是一种必要的技术。
毕竟,递归会造成栈溢出,关于系统底层的程序中不到非不得以最好不要用。
但是对某些数学问题,就一定要学会用递归去解决。

 

#include <stdio.h>
#include <stdlib.h>

struct student {
 int value;
 struct student *lchild;
 struct student *rchild;
};

void arraytotree(int *a, int len, struct student **p) {
 if(len) {
  *p = (struct student*)malloc(sizeof(struct student));
  (*p)->value = a[len/2];
  arraytotree(a, len/2, &((*p)->lchild));
  arraytotree(a+len/2+1, len-len/2-1, &((*p)->rchild));
 } else {
  *p = NULL;
 }
}

void display_tree(struct student *head) {
 if(head->lchild)display_tree(head->lchild);
 printf("%d\t", head->value);
 if(head->rchild)display_tree(head->rchild);
}

int main() {
 
 int a[] = {1,2,3,4,9,10,33,56,78,90};
 struct student *tree;
 arraytotree(a, sizeof(a)/sizeof(a[0]), &tree);
 printf("After convert:\n");
 display_tree(tree);
 printf("\n");
 return 0;
 
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很高兴为您提供帮助!请参考以下代码: public class BinaryTreeSort { // 根结点 private Node root; // 构建二叉树的结构 public class Node { int value; Node left; Node right; public Node(int value) { this.value = value; left = null; right = null; } } // 插入新结点 public void insert(int value) { root = insertRec(root, value); } // 插入新结点,递归实现 private Node insertRec(Node root, int value) { if (root == null) { root = new Node(value); return root; } if (value < root.value) root.left = insertRec(root.left, value); else if (value > root.value) root.right = insertRec(root.right, value); return root; } // 序遍历,实现排序 public void inOrder() { inOrderRec(root); } // 序遍历,递归实现 private void inOrderRec(Node root) { if (root != null) { inOrderRec(root.left); System.out.println(root.value); inOrderRec(root.right); } } } ### 回答2: 二叉树排序是一种常见的排序算法,可以通过构建一颗二叉搜索树来实现。以下是一个使用Java编写二叉树排序程序的示例: ```java // 定义二叉树节点类 class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; left = null; right = null; } } // 定义二叉搜索树类 class BinarySearchTree { private TreeNode root; BinarySearchTree() { root = null; } // 插入一个节点 private TreeNode insert(TreeNode root, int val) { if (root == null) { root = new TreeNode(val); return root; } else if (val < root.val) { root.left = insert(root.left, val); } else if (val > root.val) { root.right = insert(root.right, val); } return root; } // 序遍历二叉树,输出排序结果 private void inOrderTraversal(TreeNode root) { if (root != null) { inOrderTraversal(root.left); System.out.print(root.val + " "); inOrderTraversal(root.right); } } // 对外提供排序方法 void sort(int[] arr) { for (int val : arr) { root = insert(root, val); } inOrderTraversal(root); } } // 测试程序 public class BinaryTreeSort { public static void main(String[] args) { BinarySearchTree bst = new BinarySearchTree(); int[] arr = {7, 3, 9, 2, 4, 8, 10}; bst.sort(arr); } } ``` 以上程序,我们定义了一个`BinarySearchTree`类来表示二叉搜索树,通过调用`sort`方法可以对传入的整数数组进行二叉树排序,并通过序遍历算法输出排序结果。在`BinarySearchTree`类,我们使用`insert`方法来实现节点的插入操作,使用`inOrderTraversal`方法来进行序遍历。最后,我们在`main`方法创建了一个`BinarySearchTree`对象,并通过`sort`方法对整数数组进行排序并输出。 ### 回答3: 以下是一个用Java编写的实现二叉树排序的程序: ```java // 定义二叉树节点 class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; } } // 实现二叉树排序 class BinaryTreeSort { // 向二叉树插入一个节点 public TreeNode insert(TreeNode root, int val) { if (root == null) { return new TreeNode(val); } if (val < root.val) { root.left = insert(root.left, val); } else if (val > root.val) { root.right = insert(root.right, val); } return root; } // 序遍历二叉树,得到升序排序结果 public void inorderTraversal(TreeNode root) { if (root == null) { return; } inorderTraversal(root.left); System.out.print(root.val + " "); inorderTraversal(root.right); } } // 主程序 class Main { public static void main(String[] args) { BinaryTreeSort treeSort = new BinaryTreeSort(); int[] arr = {5, 2, 8, 1, 3}; TreeNode root = null; for (int num : arr) { root = treeSort.insert(root, num); } System.out.println("排序结果:"); treeSort.inorderTraversal(root); } } ``` 这个程序通过定义一个`TreeNode`类来表示二叉树的节点,然后使用`BinaryTreeSort`类实现二叉树的插入和序遍历,最后在`Main`类创建一个二叉树并进行排序。运行程序后,将输出升序排序的结果: ``` 排序结果: 1 2 3 5 8 ``` 此程序利用二叉树的性质,每次插入新的节点时,比较节点值与当前节点的大小关系,然后递归地插入到左子树或右子树。通过序遍历可以得到升序排序的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值