日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java二叉树算法_JAVA 二叉树算法 (遍历、深度、汇总求和)

發布時間:2025/3/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java二叉树算法_JAVA 二叉树算法 (遍历、深度、汇总求和) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二叉樹構造類:

public class BinaryTree {

int data; // 根節點數據

BinaryTree left; // 左子樹

BinaryTree right; // 右子樹

public BinaryTree(int data) // 實例化二叉樹類

{

this.data = data;

left = null;

right = null;

}

public void insert(BinaryTree root, int data) { // 向二叉樹中插入子節點

if (data > root.data) // 二叉樹的左節點都比根節點小

{

if (root.right == null) {

root.right = new BinaryTree(data);

} else {

this.insert(root.right, data);

}

} else { // 二叉樹的右節點都比根節點大

if (root.left == null) {

root.left = new BinaryTree(data);

} else {

this.insert(root.left, data);

}

}

}

}

二叉樹遍歷、深度及求和:

public class BinaryTreePreorder {

private static StringBuffer current = new StringBuffer();

private static int sum = 0;

private static int needSum = 178;

public static void preOrder(BinaryTree root) { //前序遍歷(遞歸)

if (root != null) {

System.out.print(root.data + "-");

preOrder(root.left);

preOrder(root.right);

}

}

public static void inOrder(BinaryTree root) { // 中序遍歷

if (root != null) {

inOrder(root.left);

System.out.print(root.data + "--");

inOrder(root.right);

}

}

public static void postOrder(BinaryTree root) { // 后序遍歷

if (root != null) {

postOrder(root.left);

postOrder(root.right);

System.out.print(root.data + "---");

}

}

public static int getDepth(BinaryTree node) { //深度

if (node == null) {

return 0;

}

int leftDepth = 0;

int rightDepth = 0;

leftDepth = getDepth(node.left);

rightDepth = getDepth(node.right);

return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;

}

public static void visit(BinaryTree p) {

System.out.print(p.data + " ");

}

protected static void iterativePreorder(BinaryTree p) { //前序遍歷(非遞歸)

Stack stack = new Stack();

if (p != null) {

stack.push(p);

while (!stack.empty()) {

p = stack.pop();

visit(p);

if (p.right != null)

stack.push(p.right);

if (p.left != null)

stack.push(p.left);

}

}

}

private static void sum(BinaryTree node){ //計算二叉樹節點總和數為N的集合,這里N = 178

int val = node.data;

current.append(val+",");

if(node.left==null && node.right==null){ //leaf here

sum = 0;

String[] nums = current.toString().split(",");

for (int i=0;i

sum += Integer.parseInt(nums[i]);

}

if (sum == needSum) {

for (int i=0;i

System.out.print(nums[i]+"-");

}

}

}

else{

if(node.left!=null){

sum(node.left);

current.setLength(current.length()-(node.left.data+"").length()-1);

}

if(node.right!=null){

sum(node.right);

current.setLength(current.length()-(node.right.data+"").length()-1);

}

}

}

public static void main(String[] str) {

int[] array = { 12, 76, 35, 22, 16, 48, 90, 46, 9, 40 };

BinaryTree root = new BinaryTree(array[0]); // 創建二叉樹

for (int i = 1; i < array.length; i++) {

root.insert(root, array[i]); // 向二叉樹中插入數據

}

System.out.println("(遞歸)前序遍歷:");

preOrder(root);

System.out.println();

System.out.println("(非遞歸)前序遍歷:");

iterativePreorder(root);

System.out.println();

System.out.println("中序遍歷:");

inOrder(root);

System.out.println();

System.out.println("后序遍歷:");

postOrder(root);

System.out.println();

System.out.println("深度:");

System.out.println( getDepth(root));

System.out.println("遍歷求和,取總和為178的序列:");

sum(root);

}

}

二叉樹圖形:

總結

以上是生活随笔為你收集整理的java二叉树算法_JAVA 二叉树算法 (遍历、深度、汇总求和)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。