搜索二叉树(Binary Tree)java实现

 

package source.datastruct_algorithm;

import java.util.Stack;

class BaseObj{
public BaseObj(String key) {
this.key = key;
}
String key;
}
//节点
class Node<T extends BaseObj>{
 T obj;
Node<T> leftNode;
Node<T> rightNode;
public Node(T obj) {
this.obj = obj;
}
}
//二叉树类
public class SearchTree<T extends BaseObj> {
private Node<T> root;
//根据执行的关键字查找节点
public Node<T> find(String key){
Node<T> cur_node = root;
while(!key.equals(cur_node.obj.key)){
if(key.compareTo(cur_node.obj.key)<0){
cur_node = cur_node.leftNode;
}
else{
cur_node = cur_node.rightNode;
}
if(cur_node == null){
return null;
}
}
return cur_node;
}
//插入节点
public void insert(Node<T> newnode){
if(root == null)root = newnode;
else{
Node<T> cur_node = root;
Node<T> cur_parent;
while(true){
cur_parent = cur_node;

if(newnode.obj.key.compareTo(cur_node.obj.key)<0){
cur_node = cur_node.leftNode;
if(cur_node == null){
cur_parent.leftNode = newnode;
return;
}
}else{
cur_node = cur_node.rightNode;
if(cur_node == null){
cur_parent.rightNode = newnode;
return;
}
}
}
}
}
//中序遍历
public void midOrder(Node<T> node){
if(node !=null){
midOrder(node.leftNode);
System.out.print(node.obj.key +" ");
midOrder(node.rightNode);
}
}
//前序遍历
public void preOrder(Node<T> node){
if(node !=null){
System.out.print(node.obj.key +" ");
preOrder(node.leftNode);
preOrder(node.rightNode);
}
}
//后续遍历
public void nextOrder(Node<T> node){
if(node !=null){
nextOrder(node.leftNode);
nextOrder(node.rightNode);
System.out.print(node.obj.key +" ");
}
}
//取得关键字最小的节点
public Node<T> getMin(){
Node<T> cur_node,last_node = null;
cur_node = root;
while(cur_node !=null){
last_node = cur_node;
cur_node = cur_node.leftNode;
}
return last_node;
}
//取得关键字最大的节点
public Node<T> getMax(){
Node<T> cur_node,last_node = null;
cur_node = root;
while(cur_node !=null){
last_node = cur_node;
cur_node = cur_node.rightNode;
}
return last_node;
}
//根据关键字删除节点
public boolean delede(String key){
Node<T> cur_node,parent_node = null;
boolean isleft = true;
cur_node = root;
//find the node need to del
while(!key.equals(cur_node.obj.key)){
parent_node = cur_node;
if(key.compareTo(cur_node.obj.key) >0){
isleft = true;
cur_node = cur_node.leftNode;
}
else{
isleft = false;
cur_node = cur_node.rightNode;
}
if(cur_node == null){
return false;
}
}
//execute del if don't have childnode
if(cur_node.leftNode == null && cur_node.rightNode == null){
if(cur_node == root){
root = null;
}
else if(isleft){
parent_node.leftNode = null;
}else{
parent_node.rightNode = null;
}
}else if(cur_node.leftNode == null){
if(cur_node == root){
root = cur_node.rightNode;
}else if(isleft){
parent_node.leftNode = cur_node.rightNode;
}else{
parent_node.rightNode = cur_node.rightNode;
}
}else if(cur_node.rightNode == null){
if(cur_node == root){
root = cur_node.leftNode;
}else if(isleft){
parent_node.leftNode = cur_node.leftNode;
}else{
parent_node.rightNode = cur_node.leftNode;
}
}else{
Node<T> insteadNode = getInsteadNode(cur_node);
if(cur_node.obj.key.equals(root.obj.key)){
root = insteadNode;
}
else if(isleft){
parent_node.leftNode = insteadNode;
}else{
parent_node.rightNode = insteadNode;
}
insteadNode.leftNode = cur_node.leftNode;
}
return true;


}

//查找科技补充的后继节点
private Node<T> getInsteadNode(Node<T> delNode){
Node<T> instead_parent = null,instead = null,cur_node = delNode.rightNode;
while(cur_node!=null){
instead_parent = instead;
instead = cur_node;
cur_node = cur_node.leftNode;
}

if(instead!=delNode.rightNode){
instead_parent.leftNode = instead.rightNode;
instead.rightNode = delNode.rightNode;
}
return instead;
}
//以树形显示整个树
public void display(){
Stack<Node<T>> globalstack = new Stack<Node<T>>();
globalstack.push(root);
int nblanks =32;
boolean isEmpty = false;
while(isEmpty == false){
Stack<Node<T>> localStack = new Stack<Node<T>>();
isEmpty = true;
for(int j=0;j<nblanks;j++){
System.out.println(" ");
}
while(globalstack.isEmpty() == false){
Node<T> node = globalstack.pop();
if(node !=null){
System.out.print(node.obj.key);
localStack.push(node.leftNode);
localStack.push(node.rightNode);
if(node.leftNode != null || node.rightNode != null){
isEmpty = false;
}
}else{
System.out.print("-");
localStack.push(null);
localStack.push(null);
}
for(int i=0;i<nblanks*2+2;i++){
System.out.print(" ");
}
}
System.out.println();
nblanks/=2;
while(localStack.isEmpty()==false){
globalstack.push(localStack.pop());
}

}

}
public static void main(String[] args){
SearchTree<BaseObj> tree = new SearchTree<BaseObj>();
Node<BaseObj> nodeone = new Node<BaseObj>(new BaseObj("a"));
Node<BaseObj> nodeone1 = new Node<BaseObj>(new BaseObj("z"));
Node<BaseObj> nodeone2 = new Node<BaseObj>(new BaseObj("g"));
Node<BaseObj> nodeone3 = new Node<BaseObj>(new BaseObj("f"));
Node<BaseObj> nodeone4 = new Node<BaseObj>(new BaseObj("q"));
Node<BaseObj> nodeone5 = new Node<BaseObj>(new BaseObj("h"));
Node<BaseObj> nodeone6 = new Node<BaseObj>(new BaseObj("m"));
Node<BaseObj> nodeone7 = new Node<BaseObj>(new BaseObj("b"));
Node<BaseObj> nodeone8 = new Node<BaseObj>(new BaseObj("j"));
Node<BaseObj> nodeone9 = new Node<BaseObj>(new BaseObj("l"));
Node<BaseObj> nodeone10 = new Node<BaseObj>(new BaseObj("c"));

Node<BaseObj> nodeone11 = new Node<BaseObj>(new BaseObj("A"));
Node<BaseObj> nodeone12 = new Node<BaseObj>(new BaseObj("H"));
Node<BaseObj> nodeone13 = new Node<BaseObj>(new BaseObj("f"));
Node<BaseObj> nodeone14 = new Node<BaseObj>(new BaseObj("u"));
Node<BaseObj> nodeone15 = new Node<BaseObj>(new BaseObj("x"));
Node<BaseObj> nodeone16 = new Node<BaseObj>(new BaseObj("w"));
Node<BaseObj> nodeone17 = new Node<BaseObj>(new BaseObj("s"));
Node<BaseObj> nodeone18 = new Node<BaseObj>(new BaseObj("i"));
Node<BaseObj> nodeone19 = new Node<BaseObj>(new BaseObj("k"));
tree.insert(nodeone);
tree.insert(nodeone2);

tree.insert(nodeone14);
tree.insert(nodeone15);
tree.insert(nodeone16);
tree.insert(nodeone17);

tree.insert(nodeone3);
tree.insert(nodeone4);
tree.insert(nodeone11);
tree.insert(nodeone12);
tree.insert(nodeone13);
tree.insert(nodeone5);
tree.insert(nodeone6);
tree.insert(nodeone7);
tree.insert(nodeone8);
tree.insert(nodeone9);
tree.insert(nodeone10);
tree.insert(nodeone19);
tree.insert(nodeone18);
tree.insert(nodeone1);
tree.display();

tree.delede("a");
tree.midOrder(tree.root);

}
}

二叉树在数据结构中兼顾了有序数组的查找效率和列表中插入删除效率。是一个不错的选择.该二叉树为非平衡二叉树实现。下次继续平衡二叉树实现。

转载:http://blog.csdn.net/maomaolingyu/archive/2010/11/18/6018710.aspx

你可能感兴趣的:(java,数据结构,String,tree,null,Class)