展开全部
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
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起