哈希表 二叉树

/**
 * 哈希表
 */

public class Haxi {

    public static void main(String[] args) {
       ListArr listArr=new ListArr(10);//0~9
        Scanner sc=new Scanner(System.in);
        String key="";//接收输入的数据
        while (true){
            System.out.println("add加入" );
            System.out.println("find查找");
            System.out.println("del删除");
            System.out.println("list 遍历");
            System.out.println("exit退出");
            key=sc.next();
            switch (key){
                case "add":
                    System.out.println("请输入员工姓名");
                    String name=sc.next();
                    System.out.println("请输入员工年龄");
                    int age=sc.nextInt();
                    System.out.println("请输入员工编号");
                    int id=sc.nextInt();
                    emp e=new emp(name,age,id);
                    listArr.add(e);
                    break;
                case "find":
                    System.out.println("请输入员工编号");
                    id=sc.nextInt();
                    listArr.find(id);
                    break;
                case "del":
                    System.out.println("请输入员工编号");
                    id=sc.nextInt();
                    listArr.delete(id);
                    break;
                case "list":
                    listArr.list();
                    break;
                case "exit":
                    sc.close();
                    System.exit(0);
                    break;
            }
        }
    }
}

//创建员工信息
class emp{
    private String name;
    private int age;
    private int id;
    public emp next;
    public emp(String name,int age,int id){
        this.age=age;
        this.id=id;
        this.name=name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "emp{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}
//创建链表
class empList {
    emp emp = new emp(null, 0, 0);
    private emp head = emp;//头指针默认为空

    //加入
    public void add(emp e) {
        if (head.next == null) {
            head.next = e;
        }
        emp temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            temp = temp.next;
        }
        temp = e;
    }

    //遍历
    public void list(int no) {
        if (head.next == null) {
            System.out.println("第" + no + "条链表--->为空");
            return;
        }
        emp temp = head.next;
        while (true) {
            System.out.println("第" + no + "条链表--->");
            System.out.println(temp.toString());
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
    }

    //查找
    public emp find(int id) {
        if (head.next == null) {
            return null;
        }
        emp temp = head.next;
        while (true) {
            if (temp.getId() == id) {
                break;
            } else {
                if (temp.next == null) {
                    return null;
                }
                temp = temp.next;
            }
        }
        return temp;
    }

    //删除
    public void delete(int id) {
        boolean f=false;
        if (head.next == null) {
            System.out.println("链表为空");
        } else {
            emp temp = head;
            while (true) {
                if (temp.next==null){
                    break;
                }
                if (temp.next.getId()==id){
                    f=true;
                    break;
                }
                temp=temp.next;
            }
           if (f==true){
               System.out.println("已删除");
               temp.next=temp.next.next;
           }else {
               System.out.println("没有找到");
           }

        }

    }
}


class ListArr {
    private empList[] empLists;
    private int size;

    public ListArr(int size) {
        this.size = size;
        empLists = new empList[size];
        //创建链表
        for (int i = 0; i < empLists.length; i++) {
            empLists[i] = new empList();
        }
    }

    //加入
    public void add(emp e) {
        //根据员工信息,判断员工应该加入到哪条链表
        int num = e.getId() % size;
        empLists[num].add(e);
    }

    //遍历所有链表
    public void list() {
        for (int i = 0; i < empLists.length; i++) {
            empLists[i].list(i);
        }
    }

    //查找
    public void find(int id) {
        //先找到在哪个链表中
        int num = id % size;
        emp e = empLists[num].find(id);
        if (e != null) {
            System.out.println("您找的员工在第" + num + "个链表中");
            System.out.println("员工信息:" + e.toString());
        } else {
            System.out.println("无法找到");
        }
    }

    //删除
    public void delete(int id) {
        int num = id % size;
        empLists[num].delete(id);
    }
}
/**
二叉树
*/

public class bianli {
    public static void main(String[] args) {
        //创建节点
        HeroNode node1=new HeroNode(1,"aa");
        HeroNode node2=new HeroNode(2,"bb");
        HeroNode node3=new HeroNode(3,"cc");
        HeroNode node4=new HeroNode(4,"dd");
        HeroNode node5=new HeroNode(5,"ee");

      //创建一个二叉树
        Tree tree=new Tree(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setLeft(node5);
        node3.setRight(node4);

        System.out.println("前序遍历");
        tree.proBL();
        System.out.println();
        System.out.println("中序遍历");
        tree.midBL();
        System.out.println();
        System.out.println("后序遍历");
        tree.aftBL();

    }
}

//二叉树
class Tree{
    private HeroNode root;
    public Tree(HeroNode root){
        this.root=root;
    }

    //前序遍历
    public void proBL(){
        if (root!=null){
            this.root.pro();
        }else {
            System.out.println("当前二叉树为空");
        }
    }
    //中序遍历
    public void midBL(){
        if (root!=null){
            this.root.mid();
        }else {
            System.out.println("当前二叉树为空");
        }
    }
    //后序遍历
    public void aftBL(){
        if (root!=null){
            this.root.aft();
        }else {
            System.out.println("当前二叉树为空");
        }
    }
}

//节点类
class HeroNode{
    private int id;
    private String name;
    private HeroNode left;
    private HeroNode right;
    public HeroNode(int id,String name){
        this.id=id;
        this.name=name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public HeroNode getLeft() {
        return left;
    }

    public HeroNode getRight() {
        return right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历(根左右)
    public void pro(){
        System.out.println(this);
        if (left!=null){
            left.pro();
        }
        if (right!=null){
            right.pro();
        }
    }
    //中序遍历(左跟右)
    public void mid(){
        if (left!=null){
            left.mid();
        }
        System.out.println(this);
        if (right!=null){
            right.mid();
        }
    }
    //后序遍历(左右根)
    public void aft(){
        if (left!=null){
            left.aft();
        }
        if (right!=null){
            right.aft();
        }
        System.out.println(this);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_61741696/article/details/126092075