好像一个多星期没写代码了,感觉手生很,脑袋不转啊。还是复习一下算法,今天早上把二叉查找树重写了一遍,而且还把节点的删除完全看明白了, package com.kinglong.binarytree; /** * @author kinglong * @version 2011-6-26 上午09:29:40 */ public class SearchTree { private Node root; public SearchTree() { root = null; } public SearchTree(Node root) { this.root = root; } // 插入节点 public void insert(Node node) { if (root == null) { root = node; } else { Node temp = root; Node privous = temp; while (temp != null) { privous = temp; if (temp.getData() < node.getData()) { temp = temp.getRight(); } else { temp = temp.getLeft(); } } if (privous.getData() < node.getData()) { privous.setRight(node); } else { privous.setLeft(node); } node.setParent(privous); } } // 返回最小值的节点(默认值为) public Node Minimum() { return Minimum(root); } public Node Minimum(Node node) { Node temp = node; while (temp.getLeft() != null) { temp = temp.getLeft(); } return temp; } // 返回最大值的节点 public Node Maximum() { Node temp = root; while (temp.getRight() != null) { temp = temp.getRight(); } return temp; } // 查询包含关键字K的节点(非递归) public Node search(int k) { Node x = root; while (x != null) { if (x == null || k == x.getData()) { return x; } else if (k < x.getData()) { x = x.getLeft(); } else { x = x.getRight(); } } return x; } //查找包含关键字k的节点(递归) public Node searchRecursive(Node node, int k) { if (node == null || k == node.getData()) { return node; } if (k < node.getData()) { return searchRecursive(node.getLeft(), k); } else { return searchRecursive(node.getRight(), k); } } //返回后继节点 public Node successor(int k) { Node node = search(k); return successor(node); } public Node successor(Node node) { if (node.getRight() != null) { return Minimum(node.getRight()); } Node temp = node; Node parent = node.getParent(); while (parent != null && parent.getRight() == temp) { temp = parent; parent = parent.getParent(); } return parent; } //返回前驱节点 public Node predecessor(int k) { Node node = search(k); return predecessor(node); } public Node predecessor(Node node) { if (node.getLeft() != null) { return node.getLeft(); } Node temp = node; Node parent = node.getParent(); while (parent != null && parent.getLeft() == temp) { temp = parent; parent = parent.getParent(); } return parent; } //删除关键字为k的节点 public Node delete(int k) { Node node = search(k); Node temp = null; Node tempChild = null; if (node.getLeft() == null || node.getRight() == null) { temp = node; } else { temp = successor(node); } if (temp.getLeft() != null) { tempChild = temp.getLeft(); } else { tempChild = temp.getRight(); } if (tempChild != null) { tempChild.setParent(temp.getParent()); } if (temp.getParent() == null) { root = tempChild; } else if (temp.getParent().getRight() == temp) { temp.getParent().setRight(tempChild); } else if (temp.getParent().getLeft() == temp) { temp.getParent().setLeft(tempChild); } if (temp != node) { node.setData(temp.getData()); } return temp; } //中序遍历 public void printTree() { printTree(root); System.out.println(" "); } public void printTree(Node node) { if (node != null) { printTree(node.getLeft()); System.out.print(node.getData() + " "); printTree(node.getRight()); } } } class Node { private Node left; private Node right; private Node parent; private int data; public Node() { } public Node(int data) { this(data, null, null, null); } private Node(int data, Node parent, Node left, Node right) { this.data = data; this.left = left; this.parent = parent; this.right = right; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public int getData() { return data; } public void setData(int data) { this.data = data; } } 测试代码: package com.kinglong.binarytree; /** *@author kinglong *@version 2011-6-26 上午10:41:38 */ public class SearchTest { public static void main(String[] args) { SearchTree tree = new SearchTree(); tree.insert(new Node(5)); tree.insert(new Node(2)); tree.insert(new Node(10)); tree.insert(new Node(17)); tree.insert(new Node(3)); tree.printTree(); System.out.println("最大值:"+tree.Maximum().getData()); System.out.println("最小值:"+tree.Minimum().getData()); tree.delete(5);//删除5 tree.printTree(); System.out.println("节点3 的前驱为:" + tree.predecessor(3).getData()); tree.insert(new Node(12)); System.out.println("节点12的后继为:" + tree.successor(12).getData()); } } 算法的重点在于找节点的后继,节点的前驱,和删除。