美文网首页
java二叉树查找、遍历、添加与删除的代码

java二叉树查找、遍历、添加与删除的代码

作者: laohuli | 来源:发表于2018-12-04 15:37 被阅读0次

把写代码过程中经常用的内容记录起来,下边代码是关于java二叉树查找、遍历、添加与删除的代码。

package com.structures.tree;

import java.util.ArrayList;

import java.util.NoSuchElementException;

import java.util.Stack;

public class Searchtree {

public Integer data;

public Searchtree left;

public Searchtree right;

public void setData(Integer data) {

this.data = data;

}

public Searchtree(Integer number) {

this.data = number;

}

public void setLeft(Searchtree left) {

this.left = left;

}

public void setRight(Searchtree right) {

this.right = right;

}

public void add(int number) {

if (number <= this.data)

add(number, this, this.left, 0);

else

add(number, this, this.right, 1);

}

private void add(int number, Searchtree father, Searchtree child, int tag) {

if (child == null) {

child = new Searchtree(number);

if (tag == 0)

father.setLeft(child);

else

father.setRight(child);

} else {

add(number, child, child.left, 0);

else

add(number, child, child.right, 1);

}

}

public Searchtree find(int number) {

if (number < data) {

return find(number, this);

} else if (number > data) {

return find(number, this);

} else {

return find(number, this);

}

}

private Searchtree find(int number, Searchtree tree) {

if (tree == null)

throw new NoSuchElementException(

"no such element in the binary search tree");

if (number < tree.data) {

return find(number, tree.left.left);

} else if (number > tree.data) {

return find(number, tree.right);

} else

return tree;

}

private ArrayList<Searchtree> trees = new ArrayList<Searchtree>();

private Searchtree findPrevious(int number, Searchtree tree) {

if (tree == null)

throw new NoSuchElementException(

"no such element in the binary search tree");

trees.add(tree);

if (number < tree.data) {

return findPrevious(number, tree.left);

} else if (number > tree.data) {

return findPrevious(number, tree.right);

} else {

Searchtree pre = trees.get(trees.size() - 2);

trees = null;

return pre;

}

}

public Searchtree findMin(Searchtree tree) {

if (tree == null)

throw new NoSuchElementException(

"no such element in the binary search tree");

else if (tree.left == null)

return tree;

else

return findMin(tree.left);

}

public Searchtree findMax(Searchtree tree) {

if (tree == null)

throw new NoSuchElementException(

"no such element in the binary search tree");

else if (tree.right == null)

return tree;

else

return findMax(tree.right);

}

public Searchtree remove(int number) {

return remove(number, this);

}

public Searchtree remove(int number, Searchtree tree) {

Searchtree delete = null;

if (tree == null)

throw new IndexOutOfBoundsException("tree size is 0");

else if (number < tree.data) {

tree.left = remove(number, tree.left);

} else if (number > tree.data) {

tree.right = remove(number, tree.right);

} else if (tree.left != null && tree.right != null) {

delete = findMin(tree.right);

tree.setData(delete.data);

tree.setRight(remove(tree.data, tree.right));

delete = null;

} else {

delete = tree;

if (delete.left == null) {

tree = tree.right;

} else if (delete.right == null) {

tree = tree.left;

}

delete = null;

}

return tree;

}

public void preorder() {

System.out.print(data + " ");

if (left != null)

left.preorder();

if (right != null)

right.preorder();

}

public void inorder() {

if (left != null)

left.inorder();

System.out.print(data + " ");

if (right != null)

right.inorder();

}

public void postorder() {

if (left != null)

left.postorder();

if (right != null)

right.postorder();

System.out.print(data + " ");

}

public int getDepth(Searchtree root) {

int depth;

if (root == null)

return 0;

else {

depth = 1 + Math.max(getDepth(root.left), getDepth(root.right));

return depth;

}

}

public static void main(String[] args) {

Searchtree tree = new Searchtree(5);

tree.add(3);

tree.add(7);

tree.add(2);

tree.add(4);

tree.add(6);

tree.add(8);

tree.add(1);

tree.add(1);

tree.remove(1);

tree.preorder();

System.out.println();

tree.inorder();

System.out.println();

System.out.println(tree.getDepth(tree));

System.out.println(tree.find(7).data);

System.out.println(tree.findPrevious(8, tree).data);

}

}

相关文章

  • java二叉树查找、遍历、添加与删除的代码

    把写代码过程中经常用的内容记录起来,下边代码是关于java二叉树查找、遍历、添加与删除的代码。 package c...

  • 二叉树

    1.二叉树的遍历:前序、中序、后序遍历 2.二叉树查找节点和删除节点的代码

  • Set

    下面是set的添加 删除 查找 遍历 清空

  • 二叉树的递归遍历(java版)

    1. 场景需求 二叉树如图 java中利用递归实现二叉树的各种遍历 前序遍历 中序遍历 后序遍历 3.代码实现 3...

  • Javascript树(一):广度遍历和深度遍历

    文章关键点 1. 树的建立 2. 深度遍历和广度遍历 3.callback回调函数 4. 遍历方式查找添加删除等接...

  • 二叉树查找与节点删除的javascript实现

    前言 紧接前面说过的 二叉树的实现 和 二叉树的遍历,今天来说一下用javascript实现二叉树的查找和节点删除...

  • 二叉树操作

    树节点 逐行顺序解析二叉树 前序遍历二叉树 中序遍历二叉树 后序遍历二叉树 删除指定数值的节点 前序遍历顺序存储的...

  • 7天练|Day5:二叉树和堆

    关于二叉树和堆的7个必知必会的代码实现二叉树实现一个二叉查找树,并且支持插入、删除、查找操作实现查找二叉查找树中某...

  • 树结构入门教程-二叉树的顺序存储

    经过前面的学习我们已经熟悉了二叉树的基本操作,其中包括遍历(前中后)查找(前中后)删除等操作,有了这些二叉树的基础...

  • JAVA实现二叉树生成

    给定某二叉树三序遍历中的两个,我们即可以通过生成该二叉树,并遍历的方法,求出剩下的一序,具体代码如下 [java]...

网友评论

      本文标题:java二叉树查找、遍历、添加与删除的代码

      本文链接:https://www.haomeiwen.com/subject/gvzmcqtx.html