import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
class Node{
public int iData;
public double dData;
public Node leftChild;
public Node rightChild;
@Override
public String toString() {
return "{"+iData+","+dData+"}";
}
public void display(){
System.out.print("{"+iData+","+dData+"}");
}
}
class Tree{
private Node root;
public Tree(){
root=null;
}
public Node find(int key){
Node current=root;
while(current.iData!=key){
if(key<current.iData)current=current.leftChild;
else current=current.rightChild;
if(current==null)return null;
}
return current;
}
public void insert(int id,double dd){
Node newNode=new Node();
newNode.iData=id;
newNode.dData=dd;
if(root==null){
root=newNode;
}
else{
Node current=root;
Node parent;
while (true) {
parent=current;
if (id < current.iData) {
current = current.leftChild;
if (current == null) {
parent.leftChild=newNode;
return;
}
}else{
current=current.rightChild;
if(current==null){
parent.rightChild=newNode;
return;
}
}
}
}
}
public boolean delete(int key){
Node current=root;
Node parent=root;
boolean ifLeftChild=true;
while(current.iData!=key){
parent=current;
if(key<current.iData){
ifLeftChild=true;
current=current.leftChild;
}else{
ifLeftChild=false;
current=current.rightChild;
}
if(current==null)return false;
}
if(current.leftChild==null&¤t.rightChild==null){
if(current==root){
root=null;
}else if(ifLeftChild){
parent.leftChild=null;
}else{
parent.rightChild=null;
}
}else if(current.rightChild==null){
if(current==root)root=current.leftChild;
else if(ifLeftChild){
parent.leftChild=current.leftChild;
}else{
parent.rightChild=current.leftChild;
}
}else if(current.leftChild==null){
if(current==root)root=current.rightChild;
else if(ifLeftChild){
parent.leftChild=current.rightChild;
}else{
parent.rightChild=current.rightChild;
}
}else{
Node successor=getSuccessor(current);
if(current==root)root=successor;
else if(ifLeftChild){
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 traverse(int traverseType){
switch(traverseType){
case 1:System.out.print("\nPerorder traversal:");
preOrder(root);
break;
case 2:System.out.print("\nInorder traversal:");
inOrder(root);
break;
case 3:System.out.print("\nPostorder traversal:");
PostOrder(root);
break;
}
System.out.println();
}
private void preOrder(Node localRoot) {
if(localRoot!=null){
localRoot.display();
System.out.println();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
private void inOrder(Node localRoot) {
if(localRoot!=null){
preOrder(localRoot.leftChild);
localRoot.display();
System.out.println();
preOrder(localRoot.rightChild);
}
}
private void PostOrder(Node localRoot) {
if(localRoot!=null){
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
localRoot.display();
System.out.println();
}
}
public void displayTree(){
Stack<Node> globalStack=new Stack<Node>();
globalStack.push(root);
int nBlanks=42;
boolean ifRowEmpty=false;
System.out.println("------------------------------------------------------------------------");
while(ifRowEmpty==false){
Stack<Node> localStack=new Stack<Node>();
ifRowEmpty=true;
for(int i=0;i<nBlanks;i++)System.out.print(' ');
while(globalStack.isEmpty()==false){
Node temp=globalStack.pop();
if(temp!=null){
System.out.print(temp.iData+":"+temp.dData);
localStack.push(temp.leftChild);
localStack.push(temp.rightChild);
if(temp.leftChild!=null||temp.rightChild!=null)
ifRowEmpty=false;
}else{
System.out.print("-----");
localStack.push(null);
localStack.push(null);
}
for(int j=0;j<nBlanks*2-2;j++)System.out.print(' ');
}
System.out.println();
nBlanks/=2;
while(localStack.isEmpty()==false)
globalStack.push(localStack.pop());
}
System.out.println("------------------------------------------------------------------------");
}
}
public class TreeApp {
public static void main(String[] args) throws IOException {
int value;
Tree theTree=new Tree();
theTree.insert(50, 1.5);
theTree.insert(25, 1.2);
theTree.insert(75, 1.7);
theTree.insert(37, 1.2);
theTree.insert(43, 1.7);
theTree.insert(30, 1.5);
theTree.insert(33, 1.2);
theTree.insert(87, 1.7);
theTree.insert(93, 1.5);
theTree.insert(97, 1.5);
while(true){
System.out.print("Enter first letter of show,");
System.out.print("insert,find,delete or traverse");
int choice=getChar();
switch(choice){
case 's':
theTree.displayTree();
break;
case 'i':
System.out.print("Enter value to insert:");
value=getInt();
theTree.insert(value, value+0.9);
break;
case 'f':
System.out.print("Enter value to find:");
value=getInt();
Node found=theTree.find(value);
if(found!=null){
System.out.print("Find:");
found.display();
System.out.println();
}else{
System.out.println("could not find "+value);
}
break;
case 'd':
System.out.print("Enter value to delete:");
value=getInt();
boolean didDelete=theTree.delete(value);
if(didDelete){
System.out.println("delete vaule:"+value);
}else{
System.out.println("could not delete value:"+value);
}
break;
case 't':
System.out.print("Enter type 1,2or3");
value=getInt();
theTree.traverse(value);
break;
default:
System.out.println("Invalid Entry!");
}
}
}
private static int getChar() throws IOException {
String s=getString();
return s.charAt(0);
}
private static int getInt() throws IOException {
String s=getString();
return Integer.parseInt(s);
}
private static String getString() throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
return s;
}
}
普通二叉树(java实现)
最新推荐文章于 2022-03-19 22:21:17 发布