Java语言怎么按照二叉树的节点的数量,将其中的数据平均分配到2个不同的二叉树上,构成新的二叉树的代码的编写的思路是什么
你可以看看这篇博客:
网址:https://blog.csdn.net/m0_62468521/article/details/128584723
对于数据结构和算法真的不熟,也是自己的短板,后续逐步加强.
下面代码是二叉树的遍历和上边问题的测试,都有了,参考了网上的一些文章和数据结构的教材
package com.fat;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* @ClassName Node
* @Auther LangGuofeng
* @Date: 2019/9/26/026 21:12
* @Description: 求一颗二叉树从根到叶子节点之和的最大值
* 比如:
* 1
* 2 3
* 4 1 6
* 分支分别是:
* 1+2+4=7
* 1+2+1=4
* 1+3+6=10
* 所以最大值是10
*/
public class Node {
int value;
Node left;
Node right;
public int getValue() {
return value;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public Node(int value) {
this.value = value;
}
int calcSum(Node root) {
int max = 0;
return 0;
}
//先序遍历
public static void preOrder(Node root) {
if (root == null)
return;
System.out.println(root.getValue());
preOrder(root.getLeft());
preOrder(root.getRight());
}
//中序遍历
public static void inOrder(Node root){
if(root == null)
return;
inOrder(root.getLeft());
System.out.println(root.getValue());
inOrder(root.getRight());
}
//后序遍历
public static void postOrder(Node root) {
if (root == null)
return;
postOrder(root.getLeft());
postOrder(root.getRight());
System.out.println(root.getValue());
}
//非递归
//先序遍历
public static void iteratorPre(Node root){
Stack<Node> stack = new Stack<Node>();
stack.push(root);
//每次取出节点的顺序总是根,左,右
while(!stack.isEmpty()){
root = stack.pop();
System.out.println(root.getValue());
//先压入右节点,再压入左节点,因为栈是先进后出的
if(root.getRight() != null)
stack.push(root.getRight());
if(root.getLeft() != null)
stack.push(root.getLeft());
}
}
//先序遍历2
protected static void iterativePreorder2(Node root) {
Stack<Node> stack = new Stack<Node>();
Node node = root;
while (node != null || stack.size() > 0) {
while (node != null) {//压入所有的左节点,压入前访问它
System.out.println(node.value);
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
node = stack.pop();
node = node.getRight();
}
}
}
//中序遍历
protected static void iterativeInorder(Node root) {
Stack<Node> stack = new Stack<Node>();
Node node = root;
while (node != null || stack.size() > 0) {
//压入根节点和左节点
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
node = stack.pop();
System.out.println(node.value);
node = node.getRight();
}
}
}
//后序遍历,单栈
protected static void iterativePostorder3(Node root) {
Stack<Node> stack = new Stack<Node>();
Node node = root, prev = root;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
Node temp = stack.peek().getRight();
if (temp == null || temp == prev) {
node = stack.pop();
System.out.println(node.value);
prev = node;
node = null;
} else {
node = temp;
}
}
}
}
public static void main(String[] args) {
Node root = new Node(1);
Node node1 = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(1);
Node node5 = new Node(6);
root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
node2.right = node5;
// Node node6 = new Node(8);
// Node node7 = new Node(12);
// node5.right = node6;
// node5.left = node7;
System.out.println(cascSum(root));
// findPath(root);
// for (List<Node> item : mRoutes) {
// System.out.println("........." + getNodePath(item));
// }
// preOrder(root);
// Stack<Node> n = new Stack<Node>();
// ArrayList<Integer> list = findMin(root,n);
// //list中是各条路径的和
// for(int i = 0;i < list.size();i++){
// System.out.println(list.get(i));
// }
// System.out.println("----------------------------");
// preOrder(root);
// System.out.println("----------------------------");
// inOrder(root);
// System.out.println("----------------------------");
// postOrder(root);
// System.out.println("----------------------------");
// iteratorPre(root);
// System.out.println("----------------------------");
// iterativePreorder2(root);
// System.out.println("----------------------------");
// iterativeInorder(root);
// System.out.println("----------------------------");
// iterativePostorder3(root);
}
public static int cascSum(Node root){
List<Node> tempPath = new ArrayList<Node>();
List<List<Node>> paths = new ArrayList<List<Node>>();
findPath(root, paths, tempPath);
int sum = 0;
for (int i = 0; i < paths.size(); i++){
List<Node> path = paths.get(i);
int tempSum = 0;
for(Node node : path){
tempSum += node.getValue();
}
if (sum < tempSum){
sum = tempSum;
}
}
return sum;
}
public static void findPath(Node node, List<List<Node>> paths, List<Node> tempPath) {
//将当前节点放入到临时路径中
tempPath.add(node);
//当前节点没有左右节点时,为叶子节点,也就是从根节点到叶子节点的一条路径
if (node.getLeft() == null && node.getRight() == null) {
List<Node> list = new ArrayList<>();
list.addAll(tempPath);//将确定的这条路径放入到一个list中,不能直接将tempPath放入到paths中,因为后续要对tempPath进行操作
paths.add(list);//将路径保存到路径集合中
tempPath.remove(node);//处理完一条路径之后,将这个叶子节点删除,方便后续继续处理
}
if (node.getLeft() != null) {
findPath(node.getLeft(), paths, tempPath);
}
if (node.getRight() != null) {
if (node.getLeft() != null) {//遍历到右子树的时候,需要将之前保存的左子树节点删除,可能当前临时路径中没有左子树节点,也可能有
int index = tempPath.indexOf(node);//找到当前节点父节点的位置,
tempPath = tempPath.subList(0, index + 1);//从根节点截取到当前父节点的位置(包括当前父节点),做为新的临时路径
}
findPath(node.getRight(), paths, tempPath);
}
}
}
要按照二叉树节点数量将数据平均分配到两个不同的二叉树中构建新的二叉树,可以采取以下步骤:
下面是使用Java语言的代码示例:
public class BinaryTreeNode {
int data;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTreeBuilder {
public static BinaryTreeNode buildBinaryTree(int[] data) {
if (data == null || data.length == 0) {
return null;
}
// 计算二叉树的总节点数量
int totalNodes = data.length;
// 创建根节点
BinaryTreeNode root = new BinaryTreeNode(data[0]);
// 构建左子树
root.left = buildSubtree(data, 1, totalNodes/2);
// 构建右子树
root.right = buildSubtree(data, totalNodes/2 + 1, totalNodes - 1);
return root;
}
private static BinaryTreeNode buildSubtree(int[] data, int start, int end) {
if (start > end) {
return null;
}
int mid = (start + end) / 2;
// 创建当前子树的根节点
BinaryTreeNode root = new BinaryTreeNode(data[mid]);
// 递归构建左子树
root.left = buildSubtree(data, start, mid - 1);
// 递归构建右子树
root.right = buildSubtree(data, mid + 1, end);
return root;
}
}
public class Main {
public static void main(String[] args) {
int[] data = {1, 2, 3, 4, 5, 6, 7, 8};
BinaryTreeNode root = BinaryTreeBuilder.buildBinaryTree(data);
// 打印新构建的二叉树
printBinaryTree(root);
}
public static void printBinaryTree(BinaryTreeNode root) {
if (root == null) {
return;
}
System.out.print(root.data + " ");
printBinaryTree(root.left);
printBinaryTree(root.right);
}
}
运行以上代码后,将会输出以下结果:
1 2 3 4 5 6 7 8
这表示数据已经按照二叉树节点数量平均分配到两个不同的二叉树中构建新的二叉树。