精华内容
下载资源
问答
  • java Tree

    2011-08-29 23:09:48
    java Tree Demo
    java


    Tree


    Demo
    展开全文
  • Java Tree

    2015-07-29 13:09:07
    Tree 的一些遍历操作

    节点的结构

    public class TreeNode {
    
    	int val;
    	TreeNode left;
    	TreeNode right;
    	
    	public TreeNode(int x){
    		val = x;
    	}
    	public TreeNode(){
    		
    	}
    }

    给定数据集,构造BST

    public TreeNode BSTinsert(TreeNode root, int element){
    		
    		if(null == root){
    			root = new TreeNode(element);
    			root.left = null;
    			root.right = null;
    			count++;
    			return root;
    		}
    		if(element == root.val)
    			return null;
    		if(element < root.val)
    			root.left = BSTinsert(root.left, element);
    		else
    			root.right = BSTinsert(root.right, element);
    		return root;
    				
    	}
    	
    	public boolean createBST(int[] data){
    		
    		for(int element: data){
    			if((root=BSTinsert(root,element)) ==null)
    				return false;
    		//	System.out.println("insert: "+ element);
    		}
    		return true;
    	}

    中间结构

    class MyQueue<T>{
    	private LinkedList<T> queue; 
    	public  MyQueue(){
    		queue = new LinkedList<T>();
    	}
    	public void add(T t){
    		queue.addFirst(t);
    	}
    	public T poll(){
    		return queue.removeLast();
    	}
    	public boolean isEmpty(){
    		return queue.isEmpty();
    	}
    }
    
    class MyStack<T>{
    	private LinkedList<T> data;
    	public MyStack(){
    		data = new LinkedList<T>();
    	}
    	public T pop(){
    		return data.removeFirst();
    	}
    	public void push(T t){
    		 data.addFirst(t);
    	}
    	public T getTop(){
    		return data.getFirst();
    	}
    	public boolean isEmpty(){
    		return data.isEmpty();
    	}
    }

    递归遍历BST

    <pre name="code" class="java">	public void inOrderTraverse(TreeNode root){
    		if(null !=root){
    			inOrderTraverse(root.left);
    			System.out.println(root.val);
    			inOrderTraverse(root.right);
    		}
    	}


    
    

    非递归遍历BST

    	public void inOrderTraverseWithoutRec(BST bst){
    		
    		MyStack<TreeNode> TreeNodes =  new MyStack<TreeNode>();
    		TreeNode root = bst.root;
    		TreeNode currantNode = root;
    		while(currantNode!=null || !TreeNodes.isEmpty()){
    			while(currantNode!=null){
    				TreeNodes.push(currantNode);
    				currantNode = currantNode.left;
    			}
    			
    			if(TreeNodes.isEmpty())
    				return;
    			else{
    				currantNode = TreeNodes.pop();
    				System.out.println(currantNode.val);
    				currantNode = currantNode.right;
    			}
    		}
    	}
    
    	public void PreOrderTraverseWithoutRec(BST bst){
    			
    		MyStack<TreeNode> TreeNodes =  new MyStack<TreeNode>();
    		TreeNode root = bst.root;
    		TreeNode currantNode = root;
    		while(currantNode!=null || !TreeNodes.isEmpty()){
    			while(currantNode!=null){
    				System.out.println(currantNode.val);
    				TreeNodes.push(currantNode);
    				currantNode = currantNode.left;
    			}
    			
    			if(!TreeNodes.isEmpty()){
    				currantNode = TreeNodes.pop();
    				currantNode = currantNode.right;
    			}
    		}
    	}
    	
    	public void postOrderTraverWithoutRec(BST bst){
    		
    		MyStack<TreeNode> TreeNodes =  new MyStack<TreeNode>();
    		TreeNode root = bst.root;
    		TreeNode currantNode = root;
    		while(currantNode!=null){
    			
    			for(;currantNode.left!=null;currantNode = currantNode.left)
    				TreeNodes.push(currantNode);
    			
    			while(currantNode!=null &&(currantNode.right==null || currantNode.right==root)){
    				System.out.println(currantNode.val);
    				root = currantNode;
    				if(TreeNodes.isEmpty())
    					return;
    				currantNode = TreeNodes.pop();
    			}
    			TreeNodes.push(currantNode);
    			currantNode = currantNode.right;
    		}
    	}
    	
    	public void LevelOrder(BST bst){
    		
    		MyQueue<TreeNode> BSTreeNode = new MyQueue<TreeNode>();
    		if(bst.root ==null)
    			return;
    		BSTreeNode.add(bst.root);
    		while(!BSTreeNode.isEmpty()){
    			TreeNode currantNode = BSTreeNode.poll();
    			System.out.println(currantNode.val);
    			if(currantNode.left!=null)
    				BSTreeNode.add(currantNode.left);
    			if(currantNode.right!=null)
    				BSTreeNode.add(currantNode.right);
    		}
    	}
    	


    BST中第K个最小的数

    	
    	public int kthSmallest(TreeNode root, int k) {
    		if(size(root.left) == k-1)
    			return root.val;
    		
    		else if(size(root.left) < k-1)
    			return kthSmallest(root.right, k-(size(root.left)+1));
    		
    		else
    			return kthSmallest(root.left, k);	
        }
    	
    	public int size(TreeNode root){
    		if(root == null)
    			return 0;
    		if(null == root.left && null == root.right)
    			return 1;
    		else
    			return size(root.left)+size(root.right)+1;
    	}
    	


     

    展开全文
  • 简单java tree

    2016-01-12 09:17:29
    简单java tree 比较简单的demo,无限极菜单
  • Java Tree 导航栏

    2012-12-13 13:20:41
    Java Tree 导航栏 java类源码
  • import java.awt.Dimension;import java.awt.Rectangle;import java.util.Enumeration;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTree;import javax.sw...

    package com.EditJTree;

    import java.awt.Dimension;

    import java.awt.Rectangle;

    import java.util.Enumeration;

    import javax.swing.JFrame;

    import javax.swing.JScrollPane;

    import javax.swing.JTree;

    import javax.swing.tree.TreeNode;

    import javax.swing.tree.TreePath;

    //java tree自动展开树

    public class JTreeTest extends JFrame {

    //增加带滚动条容器

    JScrollPane jScrollPane1 = new JScrollPane();

    //创建树对象

    JTree treeObj = new JTree();

    public JTreeTest () {

    try {

    jbInit();

    } catch (Exception exception) {

    exception.printStackTrace();

    }

    }

    private void jbInit() throws Exception {

    getContentPane().setLayout(null);

    setSize(new Dimension(434, 347));

    setTitle("JTree Test");

    jScrollPane1.setBounds(new Rectangle(9, 9, 429, 317));

    this.getContentPane().add(jScrollPane1);

    expandTree(treeObj);

    jScrollPane1.getViewport().add(treeObj);

    setVisible(true);

    }

    /**完全展开一个JTree

    * @param tree JTree

    */

    public static void expandTree(JTree tree) {

    TreeNode root = (TreeNode) tree.getModel().getRoot();

    expandAll(tree, new TreePath(root), true);

    }

    /**

    * 完全展开或关闭一个树,用于递规执行

    * @param tree JTree

    * @param parent 父节点

    * @param expand 为true则表示展开树,否则为关闭整棵树

    */

    private static void expandAll(JTree tree, TreePath parent, boolean expand) {

    //Traverse children

    TreeNode node = (TreeNode) parent.

    getLastPathComponent();

    if (node.getChildCount() >= 0) {

    for (Enumeration e = node.children(); e.hasMoreElements(); ) {

    TreeNode n = (TreeNode) e.nextElement();

    TreePath path = parent.pathByAddingChild(n);

    expandAll(tree, path, expand);

    }

    }

    if (expand) {

    tree.expandPath(parent);

    } else {

    tree.collapsePath(parent);

    }

    }

    public static void main(String[] args) {

    JTreeTest dJTreeTest = new JTreeTest();

    }

    }

    展开全文
  • public List getMenu(long id){List treeNodes = new ArrayList();List list = sysMenuService.queryByParentId(id);Iterator iterator = list.iterator();while(iterator.hasNext()){SysMenu sysMenu = iterator.ne...

    public List getMenu(long id){

    List treeNodes = new ArrayList();

    List list = sysMenuService.queryByParentId(id);

    Iterator iterator = list.iterator();

    while(iterator.hasNext()){

    SysMenu sysMenu = iterator.next();

    TreeNode tn1 = new TreeNode();

    tn1.setId(sysMenu.getId());

    tn1.setText(sysMenu.getText());

    tn1.setLeaf(tn1.getLeaf());

    tn1.setUrl(sysMenu.getUrl());

    TreeNode tn2 = getChildren(tn1);

    treeNodes.add(tn2);

    }

    return treeNodes;

    }

    public TreeNode getChildren(TreeNode node){

    List treeNodes = new ArrayList();

    TreeNode treeNode = new TreeNode();

    treeNode.setId(node.getId());

    treeNode.setIcon(node.getIcon());

    treeNode.setUrl(node.getUrl());

    treeNode.setText(node.getText());

    List childList = sysMenuService.queryByParentId(node.getId());

    Iterator iterator = childList.iterator();

    while (iterator.hasNext()) {

    SysMenu sysMenu = iterator.next();

    TreeNode tn1 = new TreeNode();

    tn1.setId(sysMenu.getId());

    tn1.setText(sysMenu.getText());

    tn1.setUrl(sysMenu.getUrl());

    tn1.setLeaf(tn1.getLeaf());

    TreeNode tn2 = getChildren(tn1);

    treeNodes.add(tn2);

    }

    treeNode.setChildren(treeNodes);

    return treeNode;

    }

    展开全文
  • javaTree的一个例子

    2012-12-13 13:22:16
    javaTree的一个例子 很实用 需要的可以下载看看
  • import com.sun.tools.javac.tree.TreeMaker; //導入方法依賴的package包/類private JCMethodDecl createToString(JavacNode typeNode, List fields, boolean includeFieldNames, boolean callSuper, FieldAccess ...
  • java tree 树的代码实现

    万次阅读 2018-03-17 20:40:44
    java tree 树的代码实现 菜单功能等 在开发后台管理系统过程中,菜单功能是必定实现的一个功能,因此针对此功能做个笔记: 树统一接口定义 默认树类的创建 实现树状列表工具类 统一接口定义 public ...
  • java tree的实现

    2012-09-25 15:19:35
    基于java实现tree的数据结构和算法
  • Javatree java树结构

    2009-11-28 18:59:54
    关于Java的一个树结构,类似于本论坛左侧的样式,对初学者有一定的帮助
  • Java tree递归

    2016-04-15 19:24:57
    public List getMenu(long id){ List treeNodes = new ArrayList(); List list = sysMenuService.queryByParentId(id); Iterator iterator = list.iterator(); while(iterator.hasNext()){ SysMen
  • 很好用的java tree控件,适合JSP开发使用
  • import beast.evolution.tree.Tree; //导入方法依赖的package包/类/*** Extract coalescent times and tip information into ArrayList times from tree.* Upon return times contain the time of each node in the ...
  • java Tree 菜单

    2009-06-20 23:37:33
    一个类似win的树形菜单,很好用,运行 readme.html 网页看看效果
  • 思路一:1、准备表结构及对应的表数据a、表结构:create tableTB_TREE(CID NUMBER not null,CNAME VARCHAR2(50),PID NUMBER //父节点)b、表数据:insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0);...
  • WebDynpro Java tree example

    2008-11-10 16:22:42
    tree example WebDynpro Java
  • NULL 博文链接:https://jun1986.iteye.com/blog/1144751
  • java Tree结构数据

    千次阅读 2018-11-22 16:16:13
    @Override public List&lt;BaseZTree&gt; getTemplateData(){ List&lt;BaseZTree&gt; queryList = getDirectory("-1",false); List&lt;BaseZTree&gt;... ...
  • Java 递归菜单Tree并整合bootstrap ace tree  1 bootstrap ace tree Demo 的 json格式为 var tree_data = {  'for-sale' : {name: 'For Sale', type: 'folder'} ,  'vehicles' : {name: 'Vehicles', type: ...
  • import ... //導入方法依賴的package包/類/*** Create a static tree with some data in it.** @return the new tree*/@ShowcaseSourceprivate Tree createStaticTree() {// Create the tree...
  • Java TreeSet tailSet()方法java.util.TreeSet.tailSet(E fromElement)方法用于返回一组大于或等于指定元素的元素。1 语法public SortedSet tailSet(E fromElement)2 参数fromElement:这是返回集的最小边界值(包括...
  • Java.util.TreeSet.size()方法用于获取Tree集合的大小或Tree集合中存在的元素数。用法:Tree_Set.size()参数:该方法不带任何参数。返回值:该方法返回Set中存在的元素的大小或数量。以下示例程序旨在说明Java.util....
  • java tree 结构递归查询

    千次阅读 2016-08-25 14:05:29
    tree 递归
  • JAVA TREE使用核心教程

    2019-10-31 18:12:16
    //更新GUI中的树 void refreshTree() { //这里应该列取指定路径下的文件夹 rootNode.removeAllChildren();... //tree.expandPath(new TreePath(rootNode)); tree.updateUI(); }  
  • 我就废话不多说了,大家还是直接看代码吧~ private boolean... } } 运行后的结果: 以上这篇Java Tree结构数据中查找匹配节点方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 21,658
精华内容 8,663
关键字:

javatree

java 订阅