部门1下有部门2 
部门2下有部门3
部门3下有部门1
这样明显是不合理的
怎么在插入的时候控制
如果上级 和上上级部门是当前部门
就不让当前部门插入
1-2-3-4-5-1
like this

解决方案 »

  1.   

    如果这题是个sql题,还是挺不错的一个题的。
      

  2.   

    just for fun~public class TreeTest {
    public static void main(String[] args) {
    Set<TreeVo>  tree = new TreeSet<TreeVo>();
    TreeVo vo1 = new TreeVo(1,2);
    TreeVo vo2 = new TreeVo(2,3);
    TreeVo vo3 = new TreeVo(3,1);
    if(addTree(tree, vo1)){
    tree.add(vo1);
    }
    if(addTree(tree, vo2)){
    tree.add(vo2);
    }
    if(addTree(tree, vo3)){
    tree.add(vo3);
    }

    }

    public static boolean addTree(Set tree,TreeVo  vo){
    if(tree.contains(vo)){
    System.out.println("its fail because exit:"+ vo.getNext()); 
    return false;
    }else{
    System.out.println("its ok:" + vo.getNext());
    return true;
    }
    }}
    /**   
    * 文件名:TreeVo.java   
    * @auter chenjw
    * 版本信息:   
    * 日期:2012-7-17   
    * Copyright 足下 Corporation 2012    
    * 版权所有      
    */   
        
    package com.csdn.tree;/**
     * @ClassName: TreeVo
     * @Description: TODO(这里用一句话描述这个类的作用)
     * @author chenjw
     * @date 2012-7-17 下午5:58:14
     */public class TreeVo implements Comparable<TreeVo>{
    /* (non-Javadoc)   
    * @see java.lang.Comparable#compareTo(java.lang.Object)   
    */   
        
    public int compareTo(TreeVo o) {
    // TODO Auto-generated method stub
    return o.value - this.next;
    } private int value;
    private int next;

    public TreeVo(){

    }
    public TreeVo(int value,int next){
    this.value = value;
    this.next = next;

    }
    /**   
     * value   
     *   
     * @return the value   
     * @since   CodingExample Ver(编码范例查看) 1.0   
     */

    public int getValue() {
    return value;
    }
    /**   
     * @param value the value to set   
     */

    public void setValue(int value) {
    this.value = value;
    }
    /**   
     * next   
     *   
     * @return the next   
     * @since   CodingExample Ver(编码范例查看) 1.0   
     */

    public int getNext() {
    return next;
    }
    /**   
     * @param next the next to set   
     */

    public void setNext(int next) {
    this.next = next;
    }
    }