首页 > 代码库 > JAVA向,二叉查找树
JAVA向,二叉查找树
满足二叉查找树的存储规则。
类似折半查找:
import javax.management.remote.rmi._RMIConnectionImpl_Tie; /** * Created by John on 14-5-22. */ public class IntTree { private static class IntTreeNode { private int data; private IntTreeNode leftLink; private IntTreeNode rightLink; public IntTreeNode(int newData,IntTreeNode newLeftLink,IntTreeNode newRightLink) { data =newData; leftLink=newLeftLink; rightLink=newRightLink; } } private IntTreeNode root; public IntTree() { root=null; } public void add(int item) { root=insertInSubtree(item,root); } public boolean contains(int item) { return isInSubtree(item,root); } public void showElements() { showElementsInSubtree(root); } private static boolean isInSubtree(int item,IntTreeNode subTreeRoot) { if(subTreeRoot==null) return new IntTreeNode(item,null,null); else if(item <subTreeRoot.data) { subTreeRoot.leftLink=insertInSubtree(item, subTreeRoot.leftLink); return subTreeRoot; } else { subTreeRoot.rightLink=insertInSubtree(item,subTreeRoot.rightLink); return subTreeRoot; } } private static boolean isInSubtree(int item,IntTreeNode subTreeRoot) { if(subTreeRoot==null) return false; else if(subTreeRoot.data=http://www.mamicode.com/=item) return true; else if(item<subTreeRoot.data) return isInSubtree(item,subTreeRoot.leftLink); else return isInSubtree(item,subTreeRoot.rightLink); } private static void showElementsInSubtree(IntTreeNode subTreeRoot) { if(subTreeRoot!=null) { showElementsInSubtree(subTreeRoot.leftLink); System.out.print(subTreeRoot.data+" "); showElementsInSubtree(subTreeRoot.rightLink); } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。