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

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

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. sql快速删除所用表,视图,存储过程

    [http://www.th7.cn/db/mssql/2011-07-07/10127.shtml#userconsent#] 删除用户表 .select 'DROP TABLE '+name fr ...

  2. virtualvenv+django+uWSGI+nginx 部署

    原创博文 转载请注明出处! 1. virtualvenv 2. django 3. uWSGI 4. nginx 5. 踩坑记录 1. virtualvenv virtualvenv install ...

  3. 在Linux系统中重现黑客帝国经典画面

    我们需要一个叫cmatrix的小程序,下面写出步骤 1 :依赖环境  yum -y install gcc ncurses-devel 2 :下载程序  wget https://files.cnbl ...

  4. LeetCode之Weekly Contest 92

    第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...

  5. 【laravel】Laravel 5 TokenMismatchException on PHP 5.6.9

    When I realized this was only happening in IE and Chrome, but not Firefox, it led me to the fix. The ...

  6. Android后台的linux一直保持唤醒状态,不进入睡眠

    由于要做Android手机的电池续航测试,是不能插usb的,所以把case放到sh文件中,之后push到手机里,执行的. 但是出现个问题,假如case中有很长时间的sleep操作,关闭手机屏幕,这样l ...

  7. CSS预处理器(less 和 sass)

    CSS预处理器 1.基于CSS的另一种语言 2.通过工具编译成CSS 3.添加了很多CSS不具备的特性 4.能提升CSS文件的组织 提供功能:1.嵌套 反映层级和约束 2.变量和计算,减少重复戴拿 3 ...

  8. Python之log的处理方式

    配置文件: #! /usr/bin/env python # -*- coding: utf-8 -*- """ logging配置 """ ...

  9. luogu2865 [USACO06NOV]路障Roadblocks 次短路

    注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...

  10. STL学习笔记1--vector

    C++STL(Standard Template Library)标准模板库是通用类模板和算法的集合.包含一些标准的数据结构的实现如queues(队列),lists(链表),stacks(栈)等.ST ...