java二叉树的增删改查原理_二叉树的增删改查

package erTree;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Stack;

public class erTree {

public static void main(String[] args) {

BiTree bt = new BiTree();

bt.insert(50, 1.5);

bt.insert(25, 1.7);

bt.insert(75, 1.9);

bt.insert(55, 1.0);

bt.insert(20, 1.1);

bt.insert(30, 2.0);

bt.insert(5, 0.2);

bt.insert(27, 0.1);

bt.insert(40, 0.3);

bt.insert(28, 0.7);

HuffmanTreeBuilder hb =new HuffmanTreeBuilder();

hb.ss();

}

}

class Node {

Person p1 = new Person();

public Node left;

public Node right;

public int value;

}

class Person {

int iDate;

double fDate;

}

class BiTree {

Node root;

public BiTree() {

this.root = root;

}

public Node find(int key) {

Node currect = root;

while (currect.p1.iDate != key) {

if (key < currect.p1.iDate) {

currect = currect.left;

} else {

currect = currect.right;

}

if (currect == null) {

return null;

}

}

return currect;

}

public void insert(int id, double dd) {

Node newNode = new Node();

newNode.p1.iDate = id;

newNode.p1.fDate = dd;

if (root == null) {

root = newNode;

} else {

Node currect = root;

Node parent;

while (true) {

parent = currect;

if (id < currect.p1.iDate) {

currect = currect.left;

if (currect == null) {

parent.left = newNode;

return;

}

} else {

currect = currect.right;

if (currect == null) {

parent.right = newNode;

return;

}

}

}

}

}

public void delete(int key) {

Node currect = root;

Node parent = root;

boolean isLeft = true;

while (currect.p1.iDate != key && currect != null) {

parent = currect;

if (key > currect.p1.iDate) {

isLeft = true;

currect = currect.right;

} else {

isLeft = false;

currect = currect.left;

}

}

if (currect.left == null && currect.right == null) {

if (currect == root) {

root = null;

} else if (isLeft) {

parent.right = null;

} else {

parent.left = null;

}

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

if (currect == root) {

root = root.left;

} else if (isLeft) {

parent.right = currect.left;

} else {

parent.left = currect.left;

}

} else if (currect.left == null) {

if (currect == root) {

root = root.right;

} else if (isLeft) {

parent.left = currect.right;

} else {

parent.right = currect.right;

}

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

Node houji = currect.right;

Node houjiparent = currect.right;

while (houji.left != null) {

houjiparent = houji;

houji = houji.left;

}

if (currect == root) {

root = houji;

} else {

currect.p1.iDate = houji.p1.iDate;

if (houji.right == null) {

houjiparent.left = null;

} else {

houjiparent.left = houji.right;

}

}

}

}

// 先序遍历非递归

public void inOrder(Node localRoot) {

Stack stack = new Stack();

if (localRoot != null) {

stack.push(localRoot);

while (!stack.empty()) {

localRoot = stack.pop();

System.out.print(localRoot.p1.iDate + ",");

if (localRoot.right != null) {

stack.push(localRoot.right);

}

if (localRoot.left != null) {

stack.push(localRoot.left);

}

}

}

}

// 先序遍历递归

public void inOrder1(Node localRoot) {

if (localRoot != null) {

System.out.print(localRoot.p1.iDate + ",");

inOrder(localRoot.left);

inOrder(localRoot.right);

}

}

// 中序遍历非递归

public void iterativeInorder(Node localRoot) {

Stack stack = new Stack();

while (localRoot != null || !stack.empty()) {

while (localRoot != null) {

stack.push(localRoot);

localRoot = localRoot.left;

}

if (!stack.empty()) {

localRoot = stack.pop();

System.out.print(localRoot.p1.iDate + ",");

localRoot = localRoot.right;

}

}

}

// 中序遍历递归

public void iterativeInorder1(Node localRoot) {

if (localRoot != null) {

iterativeInorder1(localRoot.left);

System.out.print(localRoot.p1.iDate + ",");

iterativeInorder1(localRoot.right);

}

}

// 后序遍历

public void iterativePostorder(Node node) {

if (node == null)

return;

Stack s = new Stack();

Node curNode; // 当前访问的结点

Node lastVisitNode; // 上次访问的结点

curNode = node;

lastVisitNode = null;

// 把currentNode移到左子树的最下边

while (curNode != null) {

s.push(curNode);

curNode = curNode.left;

}

while (!s.empty()) {

curNode = s.pop(); // 弹出栈顶元素

// 一个根节点被访问的前提是:无右子树或右子树已被访问过

if (curNode.right != null && curNode.right != lastVisitNode) {

// 根节点再次入栈

s.push(curNode);

// 进入右子树,且可肯定右子树一定不为空

curNode = curNode.right;

while (curNode != null) {

// 再走到右子树的最左边

s.push(curNode);

curNode = curNode.left;

}

} else {

// 访问

System.out.print(curNode.p1.iDate + ",");

// 修改最近被访问的节点

lastVisitNode = curNode;

}

} // while

}

// 求最小值

public Node min() {

Node current, last = null;

current = root;

while (current != null) {

last = current;

current = current.left;

}

return last;

}

// 求最大值

public Node max() {

Node current, last = null;

current = root;

while (current != null) {

last = current;

current = current.right;

}

return last;

}

// 求最大深度

public int maxDepth(Node node) {

if (node == null) {

return 0;

}

int left = maxDepth(node.left);

int right = maxDepth(node.right);

return Math.max(left, right) + 1;

}

// 求最小深度

public int minDepth(Node node) {

if (node == null) {

return 0;

}

int left = maxDepth(node.left);

int right = maxDepth(node.right);

return Math.min(left, right) + 1;

}

// 求二叉树中节点个数

public int number(Node node) {

if (node == null) {

return 0;

}

int left = number(root.left);

int right = number(root.right);

return left + right + 1;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值