Java中二叉樹數(shù)據(jù)結構的實現(xiàn)示例
更新時間:2015年08月06日 11:57:20 作者:zinss26914
這篇文章主要介紹了Java中二叉樹數(shù)據(jù)結構的實現(xiàn)示例,包括前中后序遍歷和求二叉樹深度的方法,需要的朋友可以參考下
來看一個具體的習題實踐:
題目
根據(jù)二叉樹前序遍歷序列例如:7,-7,8,#,#,-3,6,#,9,#,#,#,-5,#,#,構建二叉樹,并且用前序、中序、后序進行遍歷
代碼
import java.util.Scanner;
public class BinaryTree {
public static String[] str;
public static int count;
/**
* 靜態(tài)內(nèi)部類,定義二叉樹節(jié)點
*/
static class TreeNode {
public String data;
TreeNode lchild;
TreeNode rchild;
public TreeNode(String x) {
this.data = x;
}
}
/**
* 根據(jù)前序序列遞歸構建二叉樹
*
* @return
*/
public static TreeNode createBtree() {
TreeNode root = null;
if (count >= str.length || str[count++].equals("#")) {
root = null;
} else {
root = new TreeNode(str[count - 1]);
root.lchild = createBtree();
root.rchild = createBtree();
}
return root;
}
/**
* 前序遍歷
*
* @param root
*/
public static void preTraverse(TreeNode root) {
if (root != null) {
System.out.print(root.data + " ");
preTraverse(root.lchild);
preTraverse(root.rchild);
}
}
/**
* 中序遍歷
*
* @param root
*/
public static void inTraverse(TreeNode root) {
if (root != null) {
inTraverse(root.lchild);
System.out.print(root.data + " ");
inTraverse(root.rchild);
}
}
/**
* 后序遍歷
*
* @param root
*/
public static void postTraverse(TreeNode root) {
if (root != null) {
postTraverse(root.lchild);
postTraverse(root.rchild);
System.out.print(root.data + " ");
}
}
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
String s = cin.nextLine();
str = s.split(",");
count = 0;
TreeNode root = createBtree();
// 前序遍歷
preTraverse(root);
System.out.println();
// 中序遍歷
inTraverse(root);
System.out.println();
// 后序遍歷
postTraverse(root);
System.out.println();
}
}
}
二叉樹的深度
下面是是實現(xiàn)二叉樹的遞歸算法的實現(xiàn),其思想就是,若為空,則其深度為0,否則,其深度等于左子樹和右子樹的深度的最大值加1:
class Node{
String name;
Node left;
Node right;
public Node(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
//定義二叉樹
class BinaryTree{
Node root;
public BinaryTree(){
root = null;
}
//為了方便起見,我就直接寫個初始化的二叉樹,詳細的可以見以前的日志
public void initTree(){
Node node1 = new Node("a");
Node node2 = new Node("b");
Node node3 = new Node("c");
Node node4 = new Node("d");
Node node5 = new Node("e");
root = node1;
node1.left = node2;
node2.right = node3;
node1.right = node4;
node3.left = node5;
}
//求二叉樹的深度
int length(Node root){
int depth1;
int depth2;
if(root == null) return 0;
//左子樹的深度
depth1 = length(root.right);
//右子樹的深度
depth2 = length(root.left);
if(depth1>depth2)
return depth1+1;
else
return depth2+1;
}
}
public class TestMatch{
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.initTree();
System.out.println(tree.length(tree.root));
}
}
相關文章
Spring Boot Actuator未授權訪問漏洞的問題解決
Spring Boot Actuator 端點的未授權訪問漏洞是一個安全性問題,可能會導致未經(jīng)授權的用戶訪問敏感的應用程序信息,本文就來介紹一下解決方法,感興趣的可以了解一下2023-09-09
java中double強制轉(zhuǎn)換int引發(fā)的OOM問題記錄
這篇文章主要介紹了java中double強制轉(zhuǎn)換int引發(fā)的OOM問題記錄,本文給大家分享問題排查過程,感興趣的朋友跟隨小編一起看看吧2024-10-10
java substring(a)與substring(a,b)的使用說明
這篇文章主要介紹了java substring(a)與substring(a,b)的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Fluent Mybatis實現(xiàn)環(huán)境隔離和租戶隔離
我們在實際的業(yè)務開發(fā)中,經(jīng)常會碰到環(huán)境邏輯隔離和租戶數(shù)據(jù)邏輯隔離的問題。本文就詳細的來介紹一下,感興趣的小伙伴們可以參考一下2021-08-08

