树的结点定义
public class TreeNode {
private int data;
public TreeNode leftNode;
public TreeNode rightNode;
public TreeNode(int key){
data = key;
}
public int getData(){
return data;
}
public void setData(int value){
data = value;
}
public void operation(){
System.out.println(data);
}
}
树的实现
public class BinaryTree {
public int count;
private TreeNode root;
public BinaryTree(){
root = new TreeNode(0);
}
public TreeNode getTreeRoot(){
return root;
}
// 查找结点
public static TreeNode find(BinaryTree tree, int key){
return BinaryTree.find(tree.getTreeRoot(), key);
}
public static TreeNode find(TreeNode currentNode, int key){
if ((null == currentNode) || (currentNode.getData() == key)){
return currentNode;
}
if (currentNode.getData() > key){
return BinaryTree.find(currentNode.leftNode, key);
}else{
return BinaryTree.find(currentNode.rightNode, key);
}
}
// 前序遍历
public static void preOrderTraversal(TreeNode currentNode){
if (null != currentNode){
preOrderTraversal(currentNode.leftNode);
currentNode.operation();
preOrderTraversal(currentNode.rightNode);
}
}
// 后序遍历
public static void postOrderTraversal(TreeNode currentNode){
if (null != currentNode){
postOrderTraversal(currentNode.rightNode);
currentNode.operation();
postOrderTraversal(currentNode.leftNode);
}
}
// 插入结点
public static void insert(BinaryTree tree, int data){
BinaryTree.insert(tree.getTreeRoot(), null, false, data);
}
public static boolean insert(TreeNode currentNode, TreeNode parentNode, boolean isLeftChild, int data) {
if (null != currentNode){
if (currentNode.getData() > data){
BinaryTree.insert(currentNode.leftNode, currentNode, true, data);
}else {
BinaryTree.insert(currentNode.rightNode, currentNode, false, data);
}
}else {
TreeNode insertNode = new TreeNode(data);
if (isLeftChild){
parentNode.leftNode = insertNode;
}else {
parentNode.rightNode = insertNode;
}
}
return true;
}
// 删除结点
public static void delete(BinaryTree tree, int data){
BinaryTree.delete(tree.getTreeRoot(), null, false, data);
}
public static boolean delete(TreeNode currentNode, TreeNode parentNode, boolean isLeftChild, int data){
if (null != currentNode){
if (currentNode.getData() > data){
BinaryTree.delete(currentNode.leftNode, currentNode, true, data);
}else if(currentNode.getData() < data){
BinaryTree.delete(currentNode.rightNode, currentNode, false, data);
}else{
TreeNode successor = processSuccessor(currentNode);
if (isLeftChild){
parentNode.leftNode = successor;
}else {
parentNode.rightNode = successor;
}
}
}
return true;
}
/* 1.待删除结点为叶子结点
2.待删除结点有一个叶子结点
3.待删除结点有两个叶子节点 */
public static TreeNode processSuccessor(TreeNode delNode){
if ((null == delNode.leftNode) && (null == delNode.rightNode)){
return null;
}else if ((null == delNode.leftNode) && (null != delNode.rightNode)){
return delNode.rightNode;
}else if ((null != delNode.leftNode) && (null == delNode.rightNode)){
return delNode.leftNode;
}else{
// 从delnode 的右子树找最小key值结点作为继承者.
return getSuccessor(delNode, delNode.rightNode, delNode);
}
}
// 此时 delNode 、successor 和 successorParent 都不为 null.
public static TreeNode getSuccessor(TreeNode delNode, TreeNode successor, TreeNode successorParent){
if (null != successor.leftNode){
getSuccessor(delNode, successor.leftNode, successor);
}
if (successor != delNode.rightNode){
successorParent.leftNode = successor.rightNode;
successor.rightNode = delNode.rightNode;
}
return successor;
}
}
测试类
public class Test {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
int[] array = {10 ,60, 666, 888, 80, 30, 50, 99, 999, 1024, 20};
for (int loop = 0; loop < array.length; loop++) {
tree.insert(tree, array[loop]);
tree.count++;
}
TreeNode node = tree.find(tree, 80);
if (null != node){
System.out.println(node.getData());
System.out.println(tree.count);
}
System.out.println("preOrderTraversal.");
BinaryTree.preOrderTraversal(tree.getTreeRoot());
BinaryTree.delete(tree, 20);
System.out.println("postOrderTraversal.");
BinaryTree.postOrderTraversal(tree.getTreeRoot());
}
}