递归方法实现二叉排序树的构建、打印、删除和搜索(2)

电脑杂谈  发布时间:2019-09-22 16:03:21  来源:网络整理

remove 函数代码如下:/** * 遍历打印二叉树 */ publicvoid print(SearchStrategyEnum sEnum){ if(root==null){ return; } else{ print(root,sEnum); } System.out.println(); } /** *数组枚举二叉树 * @param node */ privatevoid print(BinaryTreeNode node,SearchStrategyEnum sEnum){ if(node==null){ return; } if(sEnum==SearchStrategyEnum.Privious){ //前序遍历 System.out.print(node.getData()+" ");print(node.getLeftTreeNode(),sEnum); print(node.getRightTreeNode(),sEnum); } elseif(sEnum==SearchStrategyEnum.Medium){ //中序遍历 print(node.getLeftTreeNode(),sEnum); System.out.print(node.getData()+" "); print(node.getRightTreeNode(),sEnum); } else{ //后序遍历 print(node.getLeftTreeNode(),sEnum); print(node.getRightTreeNode(),sEnum); System.out.print(node.getData()+" "); } } package com.xxs.java; /** * 遍历二叉树的枚举类 * 包含前序遍历、中序遍历和后序遍历三种不同的方法 * Privious前序遍历 * Medium 中序遍历 * Backhanded后序遍历 * @author Administrator * */ publicenum SearchStrategyEnum { Privious, Medium, Backhanded }四检测类的实现编码实现 test 测试类,对(三)中的类进行具体测试。

package com.xxs.java; /** * @author Administrator * */ publicclass BinaryTreeTest {private BinaryTree root=null; public BinaryTreeTest(){ root=new BinaryTree(); init(); } privatevoid init(){ root.insert(8); root.insert(2); root.insert(14); root.insert(5); root.insert(11); } publicvoid print(){ System.out.println("前序遍历打印二叉树:"); root.print(SearchStrategyEnum.Privious); System.out.println("中序遍历打印二叉树:"); root.print(SearchStrategyEnum.Medium); System.out.println("后序遍历打印二叉树:"); root.print(SearchStrategyEnum.Backhanded); } publicvoid isExistTest(){ root.print(SearchStrategyEnum.Medium); System.out.println("is 8 in the tree ?"+root.isExist(8)); System.out.println("is 18 in the tree ?"+root.isExist(18)); } publicvoid removeTest(){ System.out.println("remove 8 from the tree"); root.remove(8); System.out.println("is 8 in the tree ?"+root.isExist(8)); root.print(SearchStrategyEnum.Medium); } publicvoid insertTest(){ System.out.println("insert 8 into the tree"); root.insert(8); System.out.println("is 8 in the tree ?"+root.isExist(8)); root.print(SearchStrategyEnum.Medium); } root.insert(4); root.insert(6); root.insert(1); root.insert(7); root.insert(13); root.insert(12); root.insert(10); root.insert(3); root.insert(9); root.insert(15);/** * @param args */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub BinaryTreeTest test=new BinaryTreeTest(); test.print(); test.isExistTest(); test.removeTest(); test.insertTest(); test.print(); } }五测试结果前序数组打印二叉树: 8 4 2 1 3 6 5 7 12 10 9 11 14 13 15 中序遍历打印二叉树: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 后序遍历打印二叉树: 1 3 2 5 7 6 4 9 11 10 13 15 14 12 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 is 8 in the tree ?true is 18 in the tree ?false remove 8 from the tree is 8 in the tree ?false 1 2 3 4 5 6 7 9 10 11 12 13 14 15 insert 8 into the tree is 8 in the tree ?true 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 前序遍历打印二叉树: 9 4 2 1 3 6 5 7 8 12 10 11 14 13 15 中序遍历打印二叉树: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 后序遍历打印二叉树: 1 3 2 5 8 7 6 4 11 10 13 15 14 12 9


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-123764-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...