Java理解實現二叉排序樹
阿新 • • 發佈:2018-12-12
Java實現二叉排序樹
要求:
- 實現二叉排序樹,包括生成、插入,刪除,深度
- 對二叉排序樹進行先根、中根、和後根非遞迴遍歷
- 每次對樹的修改操作和遍歷操作的顯示結果都需要在螢幕上用樹的形狀表示出來
結點類:
public class Node {
public String data = "";//結點資料
public Node lChild = null;
public Node rChild = null;
Node(){}
Node(String data){
this.data = data;
}
}
二叉排序樹及功能類:
import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;
public class BinarySortTree {
private static LinkedList<String> charList= new LinkedList<String>();;// 儲存字串資料
private LinkedList<Node> NodeList=new LinkedList<Node>();;// 儲存節點的佇列
private static Node root;
private Node rnode;
private int length = 0;//儲存二叉排序樹的深度
/*
*建立二叉排序樹
*/
public void creatBSTress() throws IOException {
//將元素存進List中
getString();
// 2.根據第一步的結構,建立節點
creatNodes();
root = creatTree();
//System.out.println(root.data);
//50System.out.println(root.lChild.rChild.data);
//System.out.println(root.lChild.rChild.data);
System.out.print("前序遍歷:");
preordertraversal(root);
System.out.println();
System.out.print("中序遍歷:");
inordertraversal(root);
System.out.println();
System.out.print("後序遍歷:");
postordertraversal(root);
System.out.println();
//求二叉排序樹的深度
length = depthBST(root);
System.out.println("該樹的深度為"+length);
//列印二叉排序樹
printBST(root,length);
System.out.print("請輸入要查詢的字元:");
Scanner sr = new Scanner(System.in);
String key = sr.next();
searchBST(root,key);
rnode = null;
System.out.print("請輸入要插入的字元:");
Scanner sr2 = new Scanner(System.in);
String key2 = sr2.next();
Node newnode = new Node(key2);
InsertBST(root,newnode);
printBST(root,length);
System.out.print("請輸入要刪除的字元:");
Scanner sr3 = new Scanner(System.in);
String key3 = sr2.next();
delete(root,key3);
printBST(root,length);
}
//橫向列印二叉樹
private void printBST(Node tree,int len) {
if(tree.rChild!=null)
printBST(tree.rChild,len+1);
for(int i=0;i<len;i++)
System.out.print(" ");
System.out.println(tree.data);
if(tree.lChild!=null)
printBST(tree.lChild,len+1);
}
private int depthBST(Node tree) {
// TODO Auto-generated method stub
if(tree==null) {
return 0;
}else {
int lef=depthBST(tree.lChild);
int rig=depthBST(tree.rChild);
return Math.max(lef, rig)+1;
}
}
//前序遍歷
public void preordertraversal(Node tree) {
System.out.print(tree.data+" ");
if(tree.lChild!=null)
preordertraversal(tree.lChild);
if(tree.rChild!=null)
preordertraversal(tree.rChild);
}
//中序遍歷
public void inordertraversal(Node tree) {
if(tree.lChild!=null)
inordertraversal(tree.lChild);
System.out.print(tree.data+" ");
if(tree.rChild!=null)
inordertraversal(tree.rChild);
}
//後序遍歷
public void postordertraversal(Node tree) {
if(tree.lChild!=null)
postordertraversal(tree.lChild);
if(tree.rChild!=null)
postordertraversal(tree.rChild);
System.out.print(tree.data+" ");
}
//連線結點建立二叉樹
private Node creatTree() {
// TODO Auto-generated method stub
root = NodeList.poll();
Node temp = root;
while(!NodeList.isEmpty()) {
Node node =NodeList.poll();
while(true) {
if(node.data.compareTo(temp.data)<0) {
if(temp.lChild==null) {
temp.lChild = node;
break;
}else {
temp = temp.lChild;
continue;
}
}else {
if(temp.rChild==null) {
temp.rChild = node;
break;
}else {
temp = temp.rChild;
continue;
}
}
}
temp = root;
}
return root;
}
//建立結點佇列
private void creatNodes() {
//Collections.sort(charList);
for(int i=0;i<charList.size();i++) {
String data = charList.get(i);
Node node = new Node(data);
NodeList.add(node);
}
//System.out.println(NodeList.toString());
}
//儲存獲取的元素
private void getString() throws IOException {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line=bufr.readLine()) != null) {
if("over".equals(line))//區別於==
break;
charList.add(line);
}
//System.out.print(charList);
//bufr.close();
}
public static void main(String[] args) throws IOException {
BinarySortTree test = new BinarySortTree();
test.creatBSTress();
//System.out.println(charList);
}
//查詢
private Node searchBST(Node tree,String key) {
if(tree.data==null) {
System.out.println("不存在");
return null;
}else {
if(key.equals(tree.data)) {
System.out.println("存在");
return tree;
}else
if(key.compareTo(tree.data)>0)
if(tree.rChild==null) {
System.out.println("不存在");
return null;
}else {
return searchBST(tree.rChild,key);
}else
if(tree.lChild==null) {
System.out.println("不存在");
return null;
}else
return searchBST(tree.lChild,key);
}
}
//插入
private void InsertBST(Node tree,Node key) {
if(tree.data.equals(key.data))
System.out.println("樹中已存在該字元");
if(key.data.compareTo(tree.data)>0)
if(tree.rChild==null)
tree.rChild=key;
else
InsertBST(tree.rChild,key);
else
if(tree.lChild==null)
tree.lChild = key;
else
InsertBST(tree.lChild,key);
}
//刪除
private void delete(Node tree,String key) {
rnode = searchBST(tree,key);
Node pnode = parenter(tree,rnode);
if(rnode.lChild==null&&rnode.rChild==null)
{
if(pnode.lChild==rnode)
pnode.lChild=null;
if(pnode.rChild==rnode)
pnode.rChild=null;
}
else if(rnode.lChild==null)
{
if(pnode.lChild==rnode)
pnode.lChild=rnode.rChild;
if(pnode.rChild==rnode)
pnode.rChild=rnode.rChild;
}
else if(rnode.rChild==null){
if(pnode.lChild==rnode)
pnode.lChild=rnode.lChild;
if(pnode.rChild==rnode)
pnode.rChild=rnode.lChild;
}
else
{
if(pnode.lChild==rnode) {
pnode.lChild=rnode.lChild;
while(rnode.lChild.rChild!=null) {
rnode.lChild= rnode.lChild.rChild;
}
rnode.lChild.rChild=rnode.rChild;
}
if(pnode.rChild==rnode)
pnode.rChild=rnode.rChild;
while(rnode.rChild.lChild!=null) {
rnode.rChild=rnode.rChild.lChild;
}
rnode.rChild.lChild=rnode.lChild; }
}
//獲取父節點
private Node parenter(Node tree,Node rnode) {
Node pnode = tree;
while(!pnode.lChild.data.equals(rnode.data)&&!pnode.rChild.data.equals(rnode.data)) {
if(pnode.data.compareTo(rnode.data)<0)
pnode = pnode.lChild;
else
pnode = pnode.lChild;
}
System.out.println("父節點為:"+pnode.data);
return pnode;
}
}