bst java_二叉排序树(BST) Java实现

该博客详细介绍了二叉搜索树(BST)的数据结构及其操作,包括节点定义、树的构造、查找、插入、删除等基本操作。此外,还提供了中序、前序和后序遍历的方法,以及找到最小和最大节点的功能。代码实现清晰易懂,适用于理解数据结构和算法。
摘要由CSDN通过智能技术生成

展开全部

public class Node {

int key; // data used as key value

E data; // other data

Node leftChild; // this node's left child

Node rightChild; // this node's right child

public Node(int key, E o) {

this.key = key;

this.data = o;

}

public void displayNode() {

System.out.printf("%d, %s\n", key, data.toString());

}

}

===============================================================

package net.acoder.adt.tree;

public class Tree {

private Node root;

public Tree(Node root) {

if (root == null) {

throw new NullPointerException("root can't be null");

}

this.root = root;

}

public Tree(int key, E o) {

this(new Node(key, o));

}

public Node getRoot() {

return root;

}

/**

* find a node by its key

*

* @param key

* @return

*/

public Node find(int key) {

Node current = root;

while (current.key !62616964757a686964616fe78988e69d8331333239313562= key) {

if (key < current.key) {

current = root.leftChild;

} else {

current = root.rightChild;

}

if (current == null) {

return null;

}

}

return current;

}

/**

* insert a node to this tree

*

* @param key

* @param o

*/

public void insert(int key, E o) {

Node aNode = new Node(key, o);

if (root == null) {

this.root = aNode;

return;

}

Node current = root;

Node parent = root;

while (true) {

parent = current;

if (key < parent.key) {

current = parent.leftChild;

if (current == null) {

parent.leftChild = aNode;

return;

}

} else {

current = parent.rightChild;

if (current == null) {

parent.rightChild = aNode;

return;

}

}

}

}

public boolean delete(int key) {

Node current = root;

Node parent = root;

boolean isLeftChild = true;

// search for node

while (current.key != key) {

parent = current;

if (key < current.key) {

isLeftChild = true;

current = current.leftChild;

} else {

isLeftChild = false;

current = current.rightChild;

}

if (current == null) {

return false;

}

}

// if no children, simply delete it

if (current.leftChild == null && current.rightChild == null) {

if (current == parent) {

root = null;

} else if (isLeftChild == true) {

parent.leftChild = null;

} else if (isLeftChild == false) {

parent.rightChild = null;

}

return true;

}

// if no left children, replace with right subtree

if (current.leftChild == null) {

if (current == root) {

root = current.rightChild;

} else if (isLeftChild) {

parent.leftChild = current.rightChild;

} else if (!isLeftChild) {

parent.leftChild = current.rightChild;

}

return true;

}

// if no right children, replace with left subtree

if (current.rightChild == null) {

if (current == root) {

root = current.leftChild;

} else if (isLeftChild) {

parent.leftChild = current.leftChild;

} else if (!isLeftChild) {

parent.leftChild = current.leftChild;

}

return true;

}

// get successor of node to delete

Node successor = getSuccessor(current);

if (current == root) {

current = successor;

} else if (isLeftChild) {

parent.leftChild = successor;

} else {

parent.rightChild = successor;

}

successor.leftChild = current.leftChild;

return true;

}

private Node getSuccessor(Node delNode) {

Node successorParent = delNode;

Node successor = delNode;

Node current = delNode.rightChild;

while (current != null) {

successorParent = successor;

successor = current;

current = current.leftChild;

}

if (successor != delNode.rightChild) {

successorParent.leftChild = successor.rightChild;

successor.rightChild = delNode.rightChild;

}

return successor;

}

public void inOrder(Node aNode) {

if (aNode != null) {

inOrder(aNode.leftChild);

aNode.displayNode();

inOrder(aNode.rightChild);

}

}

public void preOrder(Node aNode) {

if (aNode != null) {

aNode.displayNode();

inOrder(aNode.leftChild);

inOrder(aNode.rightChild);

}

}

public void backOrder(Node aNode) {

if (aNode != null) {

inOrder(aNode.leftChild);

inOrder(aNode.rightChild);

aNode.displayNode();

}

}

public Node minimum() {

Node current = this.root;

Node result = null;

while (current != null) {

result = current;

current = current.leftChild;

}

return result;

}

public Node maximum() {

Node current = this.root;

Node result = null;

while (current != null) {

result = current;

current = current.rightChild;

}

return result;

}

}

以前的代码, 记得没写完, 好像就是BST

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值