二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法

来源:互联网 发布:数字图像处理算法应用 编辑:程序博客网 时间:2024/05/16 07:36

二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法 

 

源代码:

 

view plain
  1. /** 
  2.  * 
  3.  * @author sunnyykn 
  4.  */  
  5. import java.io.*;  
  6. import java.util.*;  
  7. class Node  
  8. {  
  9.     public int iData;       //data item (key)  
  10.     public double dData;    //data item  
  11.     public Node leftChild;  //this node's left child  
  12.     public Node rightChild; //this node's right child  
  13.     public void displayNode()   //display ourself  
  14.     {  
  15.         System.out.print("{");  
  16.         System.out.print(iData);  
  17.         System.out.print(",");  
  18.         System.out.print(dData);  
  19.         System.out.print("}");  
  20.     }  
  21. }//end class Node  
  22. class Tree  
  23. {  
  24.     private Node root;      //first node of tree  
  25.     public Tree()  
  26.     {  
  27.         root = null;        //no nodes in tree yet  
  28.     }  
  29.     public Node find(int key)   //find node with given key  
  30.     {  
  31.         Node current = root;        //start at root  
  32.         while(current.iData != key) //while no match  
  33.         {  
  34.             if(key < current.iData) //go left?  
  35.                 current = current.leftChild;  
  36.             else                    //go right?  
  37.                 current = current.rightChild;  
  38.             if (current == null)    //didn't find it  
  39.                 return null;  
  40.         }  
  41.         return current;  
  42.     }//end find()  
  43.     public void insert(int id ,double dd)  
  44.     {  
  45.         Node newNode = new Node();      //make new node  
  46.         newNode.iData = id;             //insert data  
  47.         newNode.dData = dd;  
  48.         if( root == null )              //no node in root  
  49.             root = newNode;  
  50.         else  
  51.         {  
  52.             Node current = root;        //start at root  
  53.             Node parent;  
  54.             whiletrue )  
  55.             {  
  56.                 parent = current;  
  57.                 if(id < current.iData)  //go left?  
  58.                 {  
  59.                     current = current.leftChild;  
  60.                     if(current == null)  //if end of the line  
  61.                     {  
  62.                         parent.leftChild = newNode;  
  63.                         return ;  
  64.                     }  
  65.                 }//end if go left  
  66.                 else                    //or go right  
  67.                 {  
  68.                     current = current.rightChild;  
  69.                     if(current == null//if end of the line  
  70.                     {  
  71.                         parent.rightChild = newNode;  
  72.                         return ;  
  73.                     }  
  74.                 }//end else go right  
  75.             }//end while  
  76.         }//end else not root  
  77.     }//end insert()  
  78.     public boolean delete(int key)      //delete node with given key  
  79.     {  
  80.         Node current = root;  
  81.         Node parent = root;  
  82.         boolean isLeftChild = true;  
  83.         while(current.iData != key)  
  84.         {  
  85.             parent = current;  
  86.             if(key < current.iData)  
  87.             {  
  88.                 isLeftChild = true;  
  89.                 current = current.leftChild;  
  90.             }  
  91.             else  
  92.             {  
  93.                 isLeftChild = false;  
  94.                 current = current.rightChild;  
  95.             }  
  96.             if(current == null)  
  97.                 return false;  
  98.         }//end while  
  99.         //found node to delete  
  100.         //if no children , simply delete it  
  101.         if(current.leftChild == null && current.rightChild == null)  
  102.         {  
  103.             if (current == root)  
  104.                 root = null;  
  105.             else if(isLeftChild)  
  106.                 parent.leftChild = null;  
  107.             else  
  108.                 parent.rightChild = null;  
  109.         }  
  110.         //if no right child , replace with left subtree  
  111.         else if(current.rightChild == null)  
  112.         {  
  113.             if (current == root)  
  114.                 root = current.leftChild;  
  115.             else if (isLeftChild)  
  116.                 parent.leftChild = current.leftChild;  
  117.             else  
  118.                 parent.rightChild = current.leftChild;  
  119.         }  
  120.         //if no left child , replace with right subtree  
  121.         else if (current.leftChild == null)  
  122.         {  
  123.             if (current == root)  
  124.                 root = current.rightChild;  
  125.             else if(isLeftChild)  
  126.                 parent.leftChild = current.rightChild;  
  127.             else  
  128.                 parent.rightChild = current.rightChild;  
  129.         }  
  130.         //two children,so replace with inorder successor  
  131.         else  
  132.         {  
  133.             //get successor of node to delete(current)  
  134.             Node successor = getSuccessor(current);  
  135.             //connect parent of current to successor insteed  
  136.             if(current == root)  
  137.                 root = successor;  
  138.             else if (isLeftChild)  
  139.                 parent.leftChild = successor;  
  140.             else   
  141.                 parent.rightChild = successor;  
  142.             //connect successor to current's left child  
  143.             successor.leftChild = current.leftChild;  
  144.         }//end else two children  
  145.         //(successor cannot have a left child)  
  146.         return true;  
  147.     }//end delete()  
  148.     //return node with next-highest value after delNode  
  149.     //goes to right child , then right child's left descendents  
  150.     private Node getSuccessor(Node delNode)  
  151.     {  
  152.         Node successorParent = delNode;  
  153.         Node successor = delNode;  
  154.         Node current = delNode.rightChild;      //go to right child  
  155.         while(current != null)                  //until no more left children  
  156.         {  
  157.             successorParent = successor;  
  158.             successor = current;  
  159.             current = current.leftChild;        //go to left child  
  160.         }  
  161.         if (successor != delNode.rightChild)    //if successor not right child , make connections  
  162.         {  
  163.             successorParent.leftChild = successor.rightChild;  
  164.             successor.rightChild = delNode.rightChild;  
  165.         }  
  166.         return successor;  
  167.     }  
  168.     public void traverse(int traverseType)  
  169.     {  
  170.         switch(traverseType)  
  171.         {  
  172.             case 1:  
  173.                 System.out.print("/nPreorder traversal:");  
  174.                 preOrder(root);  
  175.                 break;  
  176.             case 2:  
  177.                 System.out.print("/nInorder traversal: ");  
  178.                 inOrder(root);  
  179.                 break;  
  180.             case 3:  
  181.                 System.out.print("/nPostorder traversal:");  
  182.                 postOrder(root);  
  183.                 break;  
  184.         }  
  185.         System.out.println("");  
  186.     }  
  187.     private void preOrder(Node localRoot)  
  188.     {  
  189.         if (localRoot != null)  
  190.         {  
  191.             System.out.print(localRoot.iData + " ");  
  192.             preOrder(localRoot.leftChild);  
  193.             preOrder(localRoot.rightChild);  
  194.         }  
  195.     }  
  196.     private void inOrder(Node localRoot)  
  197.     {  
  198.         if(localRoot != null)  
  199.         {  
  200.             inOrder(localRoot.leftChild);  
  201.             System.out.print(localRoot.iData + " ");  
  202.             inOrder(localRoot.rightChild);  
  203.         }  
  204.     }  
  205.     private void postOrder(Node localRoot)  
  206.     {  
  207.         if (localRoot != null)  
  208.         {  
  209.             postOrder(localRoot.leftChild);  
  210.             postOrder(localRoot.rightChild);  
  211.             System.out.print(localRoot.iData + " ");  
  212.         }  
  213.     }  
  214.     public void displayTree()  
  215.     {  
  216.         Stack globalStack = new Stack();  
  217.         globalStack.push(root);  
  218.         int nBlanks = 32;  
  219.         boolean isRowEmpty = false ;  
  220.         System.out.println("....................................................");  
  221.         while(isRowEmpty == false)  
  222.         {  
  223.             Stack localStack = new Stack();  
  224.             isRowEmpty = true;  
  225.             for(int j = 0;j < nBlanks; j ++)  
  226.                 System.out.print(" ");  
  227.             while(globalStack.isEmpty() == false)  
  228.             {  
  229.                 Node temp = (Node)globalStack.pop();  
  230.                 if(temp != null)  
  231.                 {  
  232.                     System.out.print(temp.iData);  
  233.                     localStack.push(temp.leftChild);  
  234.                     localStack.push(temp.rightChild);  
  235.                     if(temp.leftChild != null || temp.rightChild != null)  
  236.                         isRowEmpty = false;  
  237.                 }  
  238.                 else  
  239.                 {  
  240.                     System.out.print("--");  
  241.                     localStack.push(null);  
  242.                     localStack.push(null);  
  243.                 }  
  244.                 for(int j = 0; j < nBlanks*2 - 2;j ++)  
  245.                     System.out.print(" ");  
  246.             }//end while globalStack not empty  
  247.             System.out.println("");  
  248.             nBlanks /= 2;  
  249.             while(localStack.isEmpty() == false)  
  250.                 globalStack.push( localStack.pop() );  
  251.         }//end while isRowEmpty is false  
  252.         System.out.println("....................................................");  
  253.     }//end displayTree()  
  254. }//end class Tree  
  255. class TreeApp  
  256. {  
  257.     public static void main(String[] args) throws IOException  
  258.     {  
  259.         int value ;  
  260.         Tree theTree = new Tree();  
  261.           
  262.         theTree.insert(501.5);  
  263.         theTree.insert(251.2);  
  264.         theTree.insert(751.7);  
  265.         theTree.insert(121.5);  
  266.         theTree.insert(371.2);  
  267.         theTree.insert(431.7);  
  268.         theTree.insert(301.5);  
  269.         theTree.insert(331.2);  
  270.         theTree.insert(871.7);  
  271.         theTree.insert(931.5);  
  272.         theTree.insert(971.5);  
  273.         whiletrue )  
  274.         {  
  275.             System.out.print("Enter first letter of show,insert,find,delete,or traverse:");  
  276.             int choice = getChar();  
  277.             switch( choice )  
  278.             {  
  279.                 case 's':  
  280.                     theTree.displayTree();  
  281.                     break;  
  282.                 case 'i':  
  283.                     System.out.print("Enter value to insert:");  
  284.                     value = getInt();  
  285.                     theTree.insert(value, value + 0.9);  
  286.                     break;  
  287.                 case 'f':  
  288.                     System.out.print("Enter value to find:");  
  289.                     value = getInt();  
  290.                     Node found = theTree.find(value);  
  291.                     if(found != null)  
  292.                     {  
  293.                         System.out.print("Found:");  
  294.                         found.displayNode();  
  295.                         System.out.print("/n");  
  296.                     }  
  297.                     else  
  298.                     {  
  299.                         System.out.print("Could not find ");  
  300.                         System.out.print(value + '/n');  
  301.                     }  
  302.                     break;  
  303.                 case 'd':  
  304.                     System.out.print("Enter value to delete:");  
  305.                     value = getInt();  
  306.                     boolean didDelete = theTree.delete(value);  
  307.                     if(didDelete)  
  308.                         System.out.print("Deleted " + value + '/n');  
  309.                     else{  
  310.                         System.out.print("Could not delete ");  
  311.                         System.out.print(value + '/n');  
  312.                     }  
  313.                     break;  
  314.                 case 't':  
  315.                     System.out.print("Enter type 1,2 or 3:");  
  316.                     value = getInt();  
  317.                     theTree.traverse(value);  
  318.                     break;  
  319.                 default:  
  320.                     System.out.print("Invalid entry/n");  
  321.             }//end switch  
  322.         }//end while  
  323.     }//end main()  
  324.     public static String getString() throws IOException  
  325.     {  
  326.         InputStreamReader isr = new InputStreamReader(System.in);  
  327.         BufferedReader br = new BufferedReader(isr);  
  328.         String s = br.readLine();  
  329.         return s;  
  330.     }  
  331.     public static char getChar() throws IOException  
  332.     {  
  333.         String s = getString();  
  334.         return s.charAt(0);  
  335.     }  
  336.     public static int getInt() throws IOException  
  337.     {  
  338.         String s = getString();  
  339.         return Integer.parseInt(s);  
  340.     }  
  341. }//end class TreeApp  

原创粉丝点击