树的基本操作
来源:互联网 发布:stata mac 编辑:程序博客网 时间:2024/06/05 21:17
public class Node { //node class, the base of treeint data;int index;Node leftChild;Node rightChild;}import java.util.Scanner;/** * @author NEU 小宇 * @version 1.1 */public class TreeDemo {public Node root; //root node/**********************************************************/public TreeDemo() //initialize root node{root = null; }/**********************************************************/public void deleteTree() // delete{root = root.leftChild = root.rightChild = null;}/**********************************************************/public Node find(Node localNode, int n) //find the node{Node p;if(localNode == null) //if root node is nullreturn null;else if (localNode.data == n) return localNode;else{p = find(localNode.leftChild,n);if(p!=null)return p;elsereturn find(localNode.rightChild,n);}}/***********************************************************/public void insert() // insert a new node{System.out.println("new node's data:");Scanner scanner = new Scanner(System.in);Node newNode = new Node(); //initialize new nodenewNode.data = scanner.nextInt();if(root == null) //if the tree is nullroot = newNode;else //if the tree is not true{System.out.println("input father node's num");int fNum = scanner.nextInt(); Node fatherNode;fatherNode = find(root,fNum);if(fatherNode == null){System.out.println("there isn't the node");return;}System.out.println("left or right? 1/2");int n = scanner.nextInt();if(n == 1) //if insert to left child node{fatherNode.leftChild = newNode;}else //if insert to right child node{fatherNode.rightChild = newNode;}}}/***********************************************************/public void inOrder(Node localNode) //inorder traverse{if(localNode != null){inOrder(localNode.leftChild);System.out.print(localNode.data + " ");inOrder(localNode.rightChild);}}/************************************************************/public void preOrder(Node localNode) //preorder traverse{if(localNode != null){System.out.print(localNode.data + " ");preOrder(localNode.leftChild);preOrder(localNode.rightChild);}}/**************************************************************/public void postOrder(Node localNode) //postorder traverse{if(localNode != null){postOrder(localNode.leftChild);postOrder(localNode.rightChild);System.out.print(localNode.data + " ");}}/***************************************************************/public void levelOrder(Node localNode) //level order traverse{Node[] array = new Node[100];int front,rear; //the queue's head and tailfront = rear = 0;array[0] = localNode;rear ++;while(rear != front){System.out.print(array[front].data + " ");int i = front;front++;if(array[i].leftChild != null){array[rear] = array[i].leftChild;rear++;}if(array[i].rightChild != null){array[rear] = array[i].rightChild;rear++;}}}/***********************************************************/public int getHeight(Node localNode) //Get the height of the tree{if(localNode == null)return 0;else{int leftHeight = getHeight(localNode.leftChild);int rightHeight = getHeight(localNode.rightChild);return (leftHeight > rightHeight)?(leftHeight + 1):(rightHeight + 1);}}/*************************************************************/}import java.util.Scanner;/**This praogran is to enforce a * Tree's basical operation * @author xiaoYu * @version 1.1 */public class TreeApp {/** * @param args */public static boolean flag; //exist the operation by this public static void main(String[] args) {// TODO Auto-generated method stubTreeDemo tree = new TreeDemo();flag = true;while(flag){treeOperation(tree);}System.out.print("bye bye,my friend");}/**********************************************************/public static void treeOperation(TreeDemo localTree) //use this to operate tree{Scanner scanner = new Scanner(System.in);System.out.println("what funtion do you need now? 1.insert node 2.preorder traverse 3.postorder traverse 4.inorder traverse");System.out.println(" 5.level order 6.get height 7 delete the tree 10.exist");int select = scanner.nextInt();switch(select){case 1:localTree.insert();break;case 2:localTree.preOrder(localTree.root);break;case 3:localTree.postOrder(localTree.root);break;case 4:localTree.inOrder(localTree.root);break;case 5:localTree.levelOrder(localTree.root);break;case 6:int h = localTree.getHeight(localTree.root);System.out.println("The height is" + h);break;case 7:localTree.deleteTree();break;case 10:flag = false;break;default:break;}}}
另有C语言代码
0 0
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 平衡树的基本操作
- Sleep和Wait的区别
- 初创公司的创始人该拿多少薪水?
- Animations(通过XML 文件控制)-- MarsChen Android 开发教程笔记
- Express框架中如何引用ejs模板引擎
- LeetCode 51 Symmetric Tree
- 树的基本操作
- C++临时对象销毁时间
- hadoop学习【12】——hadoop杂谈之我的理解
- linux 设置网络API --- 开/关网卡、设置/获取Mac地址
- 公顷、平方米、亩
- session API
- String类的compareTo()和compareToIgnoreCase()
- 自省
- 乘电梯(DP)