二叉树的具体特性和细节知识点,自行百度,直接上代码。

节点:节点内容、左子孩子、右子孩子、父亲

class Node {
private int data;
private Node leftChild;
private Node rightChild;
private Node parent; public Node getParent() {
return parent;
} public void setParent(Node parent) {
this.parent = parent;
} public Node(int data, Node leftChild, Node rightChild, Node parent) {
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
this.parent = parent; } public int getData() {
return data;
} public void setData(int data) {
this.data = data;
} public Node getLeftChild() {
return leftChild;
} public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
} public Node getRightChild() {
return rightChild;
} public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
} }

二叉树构造和操作:

public class BinaryTree {
private Node root;//根节点
//插入节点
public void insertNode(Node root, Node node) { Node current = root;
while (true) {
if (node.getData() < current.getData()) {
if (current.getLeftChild() == null) {
node.setParent(current);
current.setLeftChild(node);
break;
} else {
current = current.getLeftChild();
}
} else {
if (current.getRightChild() == null) {
node.setParent(current);
current.setRightChild(node);
break;
} else {
current = current.getRightChild();
} }
}
}
//删除节点
public void deleteNode(Node node) {
if (node.equals(root)) {
root = null;
} else if (node.getParent() != null) {
if (node == node.getParent().getLeftChild()) {
node.getParent().setLeftChild(null);
} else {
node.getParent().setRightChild(null); }
}
} //获取某节点的高度
public int geHeight(Node node) {
if (node == null) {
return 0;
} else {
int leftHeight = geHeight(node.getLeftChild());
int rightHeight = geHeight(node.getRightChild());
int max = Math.max(leftHeight, rightHeight);
return max + 1;
}
} //获取某节点的子节点个数
public int getChildNodes(Node node) {
if (node == null) {
return 0;
} else {
int leftNodes = getChildNodes(node.getLeftChild());
int rightNodes = getChildNodes(node.getRightChild());
return leftNodes + rightNodes + 1;
}
} //先序遍历树
public void PreOrder(Node root) {
if (root == null)
return;
System.out.print(root.getData() + " ");
PreOrder(root.getLeftChild());
PreOrder(root.getRightChild());
} //中序
public void MidOrder(Node root) {
if (root == null) return;
MidOrder(root.getLeftChild());
System.out.print(root.getData() + " ");
MidOrder(root.getRightChild());
} //后序
public void LastOrder(Node root) {
if (root == null) return;
LastOrder(root.getLeftChild());
LastOrder(root.getRightChild());
System.out.print(root.getData() + " "); } public BinaryTree() { } public BinaryTree(Node root) {
this.root = root;
} public Node getRoot() {
return root;
} public void setRoot(Node root) {
this.root = root;
} }

测试:

public class Main {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree(new Node(1, null, null, null));
int a[] = {5, 3, 2, 7, 4, 9, 8};
for (int i = 0; i < 7; i++) {
bt.insertNode(bt.getRoot(), new Node(a[i], null, null, null));
}
// System.out.println(bt.geHeight(root));//高度
// bt.PreOrder(root);
// System.out.println();
// bt.MidOrder(root);
// System.out.println();
// bt.LastOrder(root);
// System.out.println();
// bt.deleteNode(bt.getRoot());
// bt.PreOrder(bt.getRoot());
// System.out.println(bt.getChildNodes(bt.getRoot()));//子节点数 }
}

最新文章

  1. 为开发者准备的 Android 函数库(2016 年版)
  2. 【转载】最完美解决Nginx部署ThinkPHP项目的办法
  3. 解析JSON插入数据库
  4. linux下查看磁盘空间 [转]
  5. WP8 学习 ApplicationBar 的创建 XAML代码
  6. Table of Contents - Jersey
  7. str_repeat() 函数
  8. 基础数据类型的补充和深浅copy
  9. 速成KeePass全局自动填表登录QQ与迅雷(包括中文输入法状态时用中文用户名一键登录)
  10. kmp算法 模板
  11. linux 实现centos7在线升级最新版本内核
  12. mysql 查询时指定校对规则
  13. Vue项目用于Ios和Android端开发
  14. IBM X3650 M5服务器RAID阵列设置
  15. urls.py的配置[路由配置]
  16. hihocoder #1828 : Saving Tang Monk II(BFS)
  17. PLSQL入门:cursor传参,loop fetch使用,if使用,单引号字符表示
  18. rbac - 介绍
  19. webstorm累计
  20. 【vps搬家】--总结--费元星

热门文章

  1. Java 吃金币游戏设计与制作,下载版后补,代码没问题
  2. 安装mysqlclient失败
  3. 【Python学习之七】面向对象高级编程——__slots__的使用
  4. 安全和加密——openssl及自建CA
  5. windows server 服务器 环境配置
  6. JQuery 在线编辑器和手册
  7. 安装ipython的情况总结
  8. poj 2229 拆数问题 dp算法
  9. 设置eclipse中的${user}
  10. day01 项目