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

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

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. echarts实现仪表盘(自己动起来,没有后端,顺便重温math.random

    let a = parseInt(Math.random() * (2 + 1), 10); let arr = []; arr.push(res[a]); let option = { toolti ...

  2. C#传递数组参数

    在C#中,可以将数组作为参数传递给方法,同时方法可以更改数组元素的值. 一.将一维数组作为参数传递给方法 using System;using System.Collections.Generic;u ...

  3. 01_2_模拟spring装载bean

    01_2_模拟spring装载bean 1. xml配置文件内容 beans.xml <beans> <bean id="u" class="com.w ...

  4. 关于cocos2dx for lua资源加载优化方案

    之前我写游戏加载都是从一个json文件写入要加载的文件名来实现加载,但是如果资源 比较多的情况下,会导致非常难管理,需要逐个写入.所以换了另外一种方式来加载文件. 首先,我是通过场景之前的切换时候,加 ...

  5. dSYM文件

    来到新公司后,前段时间就一直在忙,前不久 项目 终于成功发布上线了,最近就在给项目做优化,并排除一些线上软件的 bug,因为项目中使用了友盟统计,所以在友盟给出的错误信息统计中能比较方便的找出客户端异 ...

  6. Mysql数据库插入中文出现乱码相关

    查看数据库编码的命令:show variables like "character%"; mysql> show variables like "character ...

  7. 流程控制之while循环for循环

    流程控制之while循环1.什么是循环 循环就是重复做某件事2.为什么要有循环 为了让计算机能够具备人重复做某件事的能力3.如何用循环 while语法: while 条件: code1 code2 c ...

  8. 模拟ajax请求爬取微博

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/26 10:26 # @Author : Sa.Song # @Desc ...

  9. bootmem_init_node

    static unsigned long __init bootmem_init_node(int node, struct meminfo *mi) in arch/arm/mm/init.c 1. ...

  10. 牛客网暑期ACM多校训练营(第七场)A Minimum Cost Perfect Matching(找规律)

    题意: 给定n, 求一个0~n-1的全排列p, 使得的和最小 分析: 打表发现最优解肯定是和为0的, 然后如果为2的幂就是直接反转即可, 不然的话就要分开从前面到后面逐步拆分, 具体思想模拟一下n = ...