二叉树的简单实现--数据结构与算法纪录片第二记
Posted qugemingzihaonan13
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树的简单实现--数据结构与算法纪录片第二记相关的知识,希望对你有一定的参考价值。
这几天状态不好看不下去,所以停了一下。今天把二叉树的实现写了一下,把其中的原理也重新理解了一遍。
package tree.bstree;
/**
* @Description: TODO
* @Auther: Zhang
* @Date: 2018/11/5 16:34
*/
public class BSTNode<T extends Comparable<T>> {
private T key;
private BSTNode<T> parentNode;
private BSTNode<T> leftChildrenNode;
private BSTNode<T> rightChildrenNode;
public BSTNode(T key) {
this.key = key;
parentNode = leftChildrenNode = rightChildrenNode = null;
}
public BSTNode(T key, BSTNode<T> parentNode, BSTNode<T> leftChildrenNode, BSTNode<T> rightChildrenNode) {
this.key = key;
this.parentNode = parentNode;
this.leftChildrenNode = leftChildrenNode;
this.rightChildrenNode = rightChildrenNode;
}
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
public BSTNode<T> getParentNode() {
return parentNode;
}
public void setParentNode(BSTNode<T> parentNode) {
this.parentNode = parentNode;
}
public BSTNode<T> getLeftChildrenNode() {
return leftChildrenNode;
}
public void setLeftChildrenNode(BSTNode<T> leftChildrenNode) {
this.leftChildrenNode = leftChildrenNode;
}
public BSTNode<T> getRightChildrenNode() {
return rightChildrenNode;
}
public void setRightChildrenNode(BSTNode<T> rightChildrenNode) {
this.rightChildrenNode = rightChildrenNode;
}
}
package tree.bstree;
/**
* @Description: TODO
* @Auther: Zhang
* @Date: 2018/11/5 16:33
*/
public class BSTree<T extends Comparable<T>> {
private BSTNode<T> rootNode;
public BSTree() {
rootNode = null;
}
public BSTree(BSTNode<T> node) {
rootNode = node;
}
//查找最大节点
private BSTNode<T> maxNode(BSTNode<T> bstNode) {
if (bstNode == null) {
return bstNode;
}
while (bstNode.getRightChildrenNode() != null) {
bstNode = bstNode.getRightChildrenNode();
}
return bstNode;
}
public BSTNode<T> maxNode() {
return this.maxNode(rootNode);
}
//查找最小节点
private BSTNode<T> minNode(BSTNode bstNode) {
if (bstNode == null) {
return bstNode;
}
while (bstNode.getLeftChildrenNode() != null) {
bstNode = bstNode.getLeftChildrenNode();
}
return bstNode;
}
public BSTNode<T> minNode() {
return this.minNode(rootNode);
}
//插入节点
private void insert(BSTNode<T> bstNode) {
int compareResult;
BSTNode<T> parent = null;
BSTNode<T> current = rootNode;
//如果树为空,该节点为根节点
if (rootNode == null) {
rootNode = bstNode;
return;
}
//数不为空,判断节点的位置
while (current != null) {
parent = current;
compareResult = bstNode.getKey().compareTo(current.getKey());
if (compareResult == 0) {
System.out.println("该节点已存在树中");
return;
}
if (compareResult < 0) {
current = current.getLeftChildrenNode();
} else {
current = current.getRightChildrenNode();
}
}
//将节点插入到树中
bstNode.setParentNode(parent);
compareResult = bstNode.getKey().compareTo(parent.getKey());
if (compareResult < 0) {
parent.setLeftChildrenNode(bstNode);
} else {
parent.setRightChildrenNode(bstNode);
}
}
public void insert(T key) {
BSTNode<T> bstNode = new BSTNode<>(key);
this.insert(bstNode);
}
//删除节点
private void delete(T key, BSTree<T> bsTree) {
BSTNode<T> bstNode = this.find(key);
if (bstNode == null) {
System.out.println("没有找到该节点");
}
BSTNode<T> parent = bstNode.getParentNode();
int comp = bstNode.getKey().compareTo(parent.getKey());
//如果节点没有孩子节点
if (bstNode.getLeftChildrenNode() == null && bstNode.getRightChildrenNode() == null) {
if (comp > 0) {
parent.setRightChildrenNode(null);
} else {
parent.setLeftChildrenNode(null);
}
return;
}
//如果该节点只有左子树或者该节点只有右子树
//只有左子树
if (bstNode.getLeftChildrenNode() != null && bstNode.getRightChildrenNode() == null) {
if (comp > 0) {
parent.setRightChildrenNode(bstNode.getLeftChildrenNode());
} else {
parent.setLeftChildrenNode(bstNode.getLeftChildrenNode());
}
return;
}
//只有右子树
if (bstNode.getRightChildrenNode() != null && bstNode.getLeftChildrenNode() == null) {
if (comp > 0) {
parent.setRightChildrenNode(bstNode.getRightChildrenNode());
} else {
parent.setLeftChildrenNode(bstNode.getRightChildrenNode());
}
return;
}
//既有左子树又有右子树
if (bstNode.getLeftChildrenNode() != null && bstNode.getRightChildrenNode() != null) {
BSTNode<T> newNode = minNode(bstNode.getRightChildrenNode());
if (bstNode.getRightChildrenNode() == newNode) {
newNode.setRightChildrenNode(null);
} else {
parent.setRightChildrenNode(newNode);
newNode.setLeftChildrenNode(bstNode.getLeftChildrenNode());
newNode.setRightChildrenNode(bstNode.getRightChildrenNode());
}
if (comp > 0) {
parent.setRightChildrenNode(newNode);
} else {
parent.setLeftChildrenNode(newNode);
}
}
}
public void delete(T key) {
delete(key, this);
}
//查找节点
private BSTNode<T> find(T key, BSTree<T> bsTree) {
int comp;
BSTNode<T> current = bsTree.rootNode;
comp = key.compareTo(current.getKey());
while (comp != 0) {
if (comp > 0) {
current = current.getRightChildrenNode();
} else {
current = current.getLeftChildrenNode();
}
if (current == null) {
return current;
}
comp = key.compareTo(current.getKey());
}
return current;
}
public BSTNode<T> find(T key) {
BSTNode bstNode = this.find(key, this);
if (bstNode == null) {
System.out.println("没有找到该节点");
}
return bstNode;
}
//前序遍历
private void preOrder(BSTNode<T> bstNode) {
if (bstNode != null) {
System.out.println("key is : " + bstNode.getKey());
preOrder(bstNode.getLeftChildrenNode());
preOrder(bstNode.getRightChildrenNode());
} else {
return;
}
}
public void preOrder() {
System.out.println("前序遍历: ");
this.preOrder(rootNode);
}
//中序遍历
private void midOrder(BSTNode<T> bstNode) {
if (bstNode != null) {
midOrder(bstNode.getLeftChildrenNode());
System.out.println("key is : " + bstNode.getKey());
midOrder(bstNode.getRightChildrenNode());
} else {
return;
}
}
public void midOrder() {
System.out.println("中序遍历: ");
this.midOrder(rootNode);
}
//后序遍历
private void postOrder(BSTNode<T> bstNode) {
if (bstNode != null) {
postOrder(bstNode.getLeftChildrenNode());
postOrder(bstNode.getRightChildrenNode());
System.out.println("key is : " + bstNode.getKey());
} else {
return;
}
}
public void postOrder() {
System.out.println("后序遍历: ");
this.postOrder(rootNode);
}
//前驱节点
private BSTNode<T> predecessor(BSTNode<T> bstNode) {
//如果存在左孩子,则前驱为最大左孩子
if (bstNode.getLeftChildrenNode() != null) {
return maxNode(bstNode.getLeftChildrenNode());
}
//不存在左孩子:如果该节点是其父节点的右孩子,则前驱节点为该节点的父节点
//不存在左孩子:如果该节点是其父节点的左孩子,则往上查找最低父节点,该节点必须是其父节点的左孩子
BSTNode<T> parent = bstNode.getParentNode();
while (parent != null && parent.getLeftChildrenNode() == bstNode) {
bstNode = parent;
parent = bstNode.getParentNode();
}
return parent;
}
public BSTNode<T> predecessor(T key) {
BSTNode<T> bstNode = this.find(key);
if (bstNode == null) {
return null;
}else {
return predecessor(bstNode);
}
}
//后继节点
private BSTNode<T> successor(BSTNode<T> bstNode) {
//如果存在右孩子,则为右子树中的最小值
if (bstNode.getRightChildrenNode() != null) {
return minNode(bstNode.getRightChildrenNode());
}
//不存在右孩子:该节点为父节点的右节点,则后继节点为父节点
//不存在右孩子:该节点为父节点的左节点,则往上查找最低的父节点且该节点为父节点的左节点
BSTNode<T> parent = bstNode.getParentNode();
while (parent != null && parent.getRightChildrenNode() == bstNode) {
bstNode = parent;
parent = bstNode.getParentNode();
}
return parent;
}
public BSTNode<T> successor(T key) {
BSTNode<T> node;
BSTNode<T> bstNode = find(key);
if (bstNode == null) {
return null;
}else {
return successor(bstNode);
}
}
}
package tree.bstree;
/**
* @Description: TODO
* @Auther: Zhang
* @Date: 2018/11/5 18:15
*/
public class MainTest {
public static void main(String[] args) {
BSTNode<Integer> bstNode = new BSTNode<>(5);
BSTree<Integer> bsTree = new BSTree<>(bstNode);
bsTree.insert(4);
bsTree.insert(2);
bsTree.insert(9);
bsTree.find(222);
bsTree.insert(3);
bsTree.delete(2);
bsTree.insert(33);
bsTree.insert(2);
bsTree.preOrder();
bsTree.midOrder();
bsTree.postOrder();
if (bsTree.predecessor(32)!=null){
System.out.println(bsTree.predecessor(32).getKey());
}
System.out.println(bsTree.predecessor(33).getKey());
System.out.println(bsTree.successor(3).getKey());
}
}
以上是关于二叉树的简单实现--数据结构与算法纪录片第二记的主要内容,如果未能解决你的问题,请参考以下文章