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

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

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()));//子节点数 }
}

Java-二叉树-插入、删除、遍历的更多相关文章

  1. java 二叉树的创建 遍历

    本来说复习一下BFS和DFS,辗转就来到了二叉树...本文包括二叉树的创建和遍历 概念 数据:1 2 3 4 5 6 7生成一颗二叉树 上面的数是数据,不是位置,要区别一下数据和位置 红色的代表位置, ...

  2. c++ 搜索二叉树 插入,删除,遍历操作

    搜索二叉树是一种具有良好排序和查找性能的二叉树数据结构,包括多种操作,本篇只介绍插入,排序(遍历),和删除操作,重点是删除操作比较复杂,用到的例子也是本人亲自画的 用到的测试图数据例子 第一.构建节点 ...

  3. 二叉树 - 建立与遍历使用Java

    二叉树的遍历(traversing binary tree)是指从根节点出发,按照某种次序依次访问二叉树中所有节点,使得每个节点仅被访问一次 前序遍历:若二叉树为空,则空操作返回null.否则先访问根 ...

  4. 毕业了-java二叉树层次遍历算法

    /*************************************** * 时间:2017年6月23日 * author:lcy * 内容:二叉树的层次遍历 * 需要借助队列这个数据结构,直 ...

  5. Java实现二叉树及相关遍历方式

    Java实现二叉树及相关遍历方式 在计算机科学中.二叉树是每一个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(r ...

  6. Java实现 LeetCode 144 二叉树的前序遍历

    144. 二叉树的前序遍历 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] /** * Definition for a ...

  7. Java实现 LeetCode 107 二叉树的层次遍历 II(二)

    107. 二叉树的层次遍历 II 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如: 给定二叉树 [3,9,20,null,null, ...

  8. Java实现 LeetCode 102 二叉树的层次遍历

    102. 二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 2 ...

  9. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  10. lintcode : 二叉树的层次遍历II

    题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...

随机推荐

  1. Bootstrap历练实例:面板的标题

    面板标题 我们可以通过以下两种方式来添加面板标题: 使用 .panel-heading class 可以很简单地向面板添加标题容器.to easily add a heading container ...

  2. cocos2x (c++/lua) spine 文件的预加载

    在之前,笔者写过一编博客,通过lua在加载场景加载spineAnimation动画精灵,保存在table中,然后在游戏中创建动画精灵时,提取加载好的spineAnimaiton中的 spSkeleto ...

  3. NSRegularExpression

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAu4AAAU2CAIAAABFtaRRAAAAAXNSR0IArs4c6QAAAe9pVFh0WE1MOm

  4. sphinx中文入门指南 (转自sphinx中文站)

    Sphinx中文入门指南 wuhuiming<blvming在gmail.com>,转载请注明来源和作者 最后修改:2010年1月23日 1.简介 1.1.Sphinx是什么 1.2.Sp ...

  5. Kafka 基础实战 :消费者和生产者实例

    学习地址: http://www.jikexueyuan.com/course/2036.html

  6. CUB reduce errorinvalid configuration argument

    解决CUB reduce errorinvalid configuration argument问题 在写TensorFlow代码时遇到报错 CUB reduce errorinvalid confi ...

  7. 监控网络流量iftop和nethogs安装

    服务器环境是centos7,centos下通常使用iftop,或者nethogs来进行网络流量监控.这2个工具都需要先安装epel,因为这个库通常操作系统是不自带的.那么就先安装epel,使用的命令是 ...

  8. 水题:HDU1716-排列2

    排列2 Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代 ...

  9. 使用sprunge粘贴文字

    在irc里面请教的时候,需要输出很多文本,irc禁止输入多行文字. 使用sprunge可以返回一个网址,省去复制粘贴的麻烦. 1> 简单使用: command | curl -F "s ...

  10. Hive安装步骤

    首先解压压缩包 然后进入bin 执行 ./hive 不过现在hive使用的是自己默认的数据库,不方便,可以通过配置使用MySQL数据库 创建hive-site.xml 粘贴一下内容 <confi ...