当前位置:网站首页>实现二叉树的基本操作
实现二叉树的基本操作
2022-07-31 05:19:00 【欧粒粒】
先序遍历,中序遍历,后序遍历,求节点个数,求叶子节点的个数,求第k层节点的个数,查找节点。
import java.util.ArrayList;
public class TestBinaryTree {
static class TreeNode {
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
public TreeNode creteTree() {
TreeNode A = new TreeNode('A');
TreeNode B = new TreeNode('B');
TreeNode C = new TreeNode('C');
TreeNode D = new TreeNode('D');
TreeNode E = new TreeNode('E');
TreeNode F = new TreeNode('F');
TreeNode G = new TreeNode('G');
TreeNode H = new TreeNode('H');
A.left = B;
A.right = C;
B.left = D;
B.right = E;
E.right = H;
C.left = F;
C.right = G;
return A;//根节点
}
// 前序遍历
public void preOrder(TreeNode root) {
if(root == null) {
return;
}
System.out.print(root.val+" ");
preOrder(root.left);
preOrder(root.right);
}
// 中序遍历
void inOrder(TreeNode root) {
if(root == null) {
return;
}
inOrder(root.left);
System.out.print(root.val+" ");
inOrder(root.right);
}
// 后序遍历
void postOrder(TreeNode root) {
if(root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val+" ");
}
public static int count = 0;
// 获取树中节点的个数 遍历思路:
public void size1(TreeNode root){
if(root == null) {
return ;
}
count++;
size1(root.right);
size1(root.left);
}
public int size2(TreeNode root){
if(root == null) {
return 0;
}
//root不为空
int tmp = size2(root.left) + size2(root.right)+1;
return tmp;
}
public static int leafCount = 0;
// 获取叶子节点的个数 遍历思路
void getLeafNodeCount1(TreeNode root){
if(root == null) {
return;
}
if(root.left == null && root.right == null) {
leafCount++;
}
getLeafNodeCount1(root.left);
getLeafNodeCount1(root.right);
}
// 获取叶子节点的个数 子问题
int getLeafNodeCount2(TreeNode root){
if(root == null) {
return 0;
}
if(root.left == null && root.right == null) {
return 1;
}
int tmp = getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
return tmp;
}
// 获取第K层节点的个数
int getKLevelNodeCount(TreeNode root,int k){
if(root == null) {
return 0;
}
if(k == 1) {
return 1;
}
return getKLevelNodeCount(root.left,k-1)
+ getKLevelNodeCount(root.right,k-1);
}
// 获取二叉树的高度
int getHeight(TreeNode root){
if(root == null) {
return 0;
}
int leftH = getHeight(root.left);
int leftR = getHeight(root.right);
return leftH > leftR ? leftH+1 : leftR+1;
}
// 检测值为value的元素是否存在
TreeNode find(TreeNode root, int val){
if(root == null) {
return null;
}
if(root.val == val) {
return root;
}
TreeNode ret = find(root.left,val);
if(ret != null) {
return ret;
}
ret = find(root.right,val);
if(ret != null) {
return ret;
}
return null;
}
// 判断一棵树是不是完全二叉树
boolean isCompleteTree(TreeNode root) {
return true;
}
}
public class TestDemo {
public static void main(String[] args) {
TestBinaryTree testBinaryTree = new TestBinaryTree();
TestBinaryTree.TreeNode root = testBinaryTree.creteTree();
testBinaryTree.preOrder(root);
System.out.println();
testBinaryTree.inOrder(root);
System.out.println();
testBinaryTree.postOrder(root);
System.out.println();
System.out.println("=========节点的个数======");
testBinaryTree.size1(root);
System.out.println(TestBinaryTree.count);
System.out.println("=========节点的个数======");
System.out.println(testBinaryTree.size2(root));
System.out.println("叶子节点的个数:");
testBinaryTree.getLeafNodeCount1(root);
System.out.println(TestBinaryTree.leafCount);
System.out.println(testBinaryTree.getLeafNodeCount2(root));
System.out.println("=============第K层==========");
System.out.println(testBinaryTree.getKLevelNodeCount(root, 4));
System.out.println("============查找某个数据========");
TestBinaryTree.TreeNode ret = testBinaryTree.find(root,'p');
try {
System.out.println(ret.val);
}catch (NullPointerException e) {
System.out.println("没有你要找的数据!");
e.printStackTrace();
}
}
}
边栏推荐
猜你喜欢
【内网开发日记】用websocket手搓一个聊天软件
Qt TreeView 问题记录
哈希表基础
链表理论基础
C语言对文件的操作(完整版)
[Solved] ssh connection report: Bad owner or permissions on C:\\Users/XXX/.ssh/config
Getting Started with MySQL: The Case Statement Works Well
vs2022 xlua 集成第三方库编译报错Generator Visual Studio 15 2017 could not find any instance of Visual Studio.
超详细!!!让你通透数组!!!初学复习不迷路!!
The content of the wangeditor editor is transferred to the background server for storage
随机推荐
滴滴被罚超80亿!收集并泄露1.07亿条乘客人脸识别信息
定义一个类,super的使用,私有属性
DOM操作-案例:切换背景图片
十分钟教你玩转分支语句!!!!!小白速进,新手福利!!
Unity加载GIf动画
Wlan实验(ENSP)
ES6-箭头函数
Virtual machine view port number process
ES6-新增的基本数据:Symbol
Incredibuild 宣布支持 Yocto
Webrtc从理论到实践一:初识
DSPE-PEG-Thiol DSPE-PEG-SH phospholipid-polyethylene glycol-thiol liposome for later use
ES6-数组
WIN10,配置adb环境
The solution to the IDEA console not being able to enter information
C语言静态变量static
ES6-02-let和const关键字
The array technique, my love
简单计算器,单层循环输出乘法表,字符串方法的使用,格式化输出
文件内容浏览cut、uniq、sort、tr命令的使用,