java二叉树算法_JAVA 二叉树算法 (遍历、深度、汇总求和)
二叉樹構(gòu)造類:
public class BinaryTree {
int data; // 根節(jié)點數(shù)據(jù)
BinaryTree left; // 左子樹
BinaryTree right; // 右子樹
public BinaryTree(int data) // 實例化二叉樹類
{
this.data = data;
left = null;
right = null;
}
public void insert(BinaryTree root, int data) { // 向二叉樹中插入子節(jié)點
if (data > root.data) // 二叉樹的左節(jié)點都比根節(jié)點小
{
if (root.right == null) {
root.right = new BinaryTree(data);
} else {
this.insert(root.right, data);
}
} else { // 二叉樹的右節(jié)點都比根節(jié)點大
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){ //計算二叉樹節(jié)點總和數(shù)為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]); // 創(chuàng)建二叉樹
for (int i = 1; i < array.length; i++) {
root.insert(root, array[i]); // 向二叉樹中插入數(shù)據(jù)
}
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);
}
}
二叉樹圖形:
總結(jié)
以上是生活随笔為你收集整理的java二叉树算法_JAVA 二叉树算法 (遍历、深度、汇总求和)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于深度学习的一点理解
- 下一篇: ubuntu16安装anaconda显示