哈希表及二叉树遍历

一.哈希表实例

1.雇员信息

class Emp {
    public int id;
    public String name;
    public Emp next;
}

2.初始化哈希表上的链

class EmpLinkedList {
    // 头指针 指向第一个Emp
    private Emp head;
}

3.增删改查方法

// 添加雇员到链表
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        // 辅助雇员
        Emp curEmp = head;
        while (true) {
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;
        }
        curEmp.next = emp;
    }

    // 遍历雇员
    public void list(int no) {
        if (head == null) {
            System.out.println("当" + no + "前链表为空...");
            return;
        }
        System.out.print("第" + no + " 链表信息为:");

        Emp curEmp = head;
        while (true) {
            System.out.printf("id=> %d, name=> %s\t", curEmp.id, curEmp.name);
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();
    }

    public Emp findEmpById(int id) {
        if (head == null) {
            System.out.println("当前链表为空...");
            return null;
        }
        Emp curEmp = head;
        while (true) {
            if (curEmp.id == id) {
                break;
            }
            if (curEmp.next == null) {
                curEmp = null;
                break;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }

    public boolean deleteEmpByID(int id) {
        if (head == null) {
            return false;
        }
        if (head.id == id) {
            head = head.next;
            return true;
        }
        Emp curEmp = head;
        while (curEmp.next != null) {
            if (curEmp.next.id == id){
                curEmp.next=curEmp.next.next;
                return true;
            }
            curEmp=curEmp.next;
        }
        return false;
    }

4.初始化哈希表

class HashTable {
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTable(int size) {
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];
        //  初始化每个链表
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }
}

二.二叉树

概念: 叶子节点:没有子节点的节点

           节点的权->值

           最多有两个子节点的树叫二叉树

           满二叉树:所有叶子节点都在最后一层,节点数为2n次方-1 n为层数

           完全二叉树:所有叶子节点都在最后一层或者在倒数第二层,且最后一层左边连续,倒数第二层叶子节点在右边连续

1.先序遍历

public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

2.中序遍历

public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

3.后续遍历

public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

4.先序遍历查找

public HeroNode perOrderSearch(int no) {
        if (this.id == no) return this;

        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.perOrderSearch(no);
        }
        if (resNode != null) return resNode;

        if (this.right != null) {
            resNode = this.right.perOrderSearch(no);
        }
        return resNode;
    }

5.中序遍历查找

public HeroNode infixOrderSearch(int no) {

        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) return resNode;

        if (this.id == no) return this;

        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

6.后续遍历查找

public HeroNode postOrderSearch(int no) {

        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) return resNode;

        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        // System.out.println(1);
        if (this.id == no) return this;

        return resNode;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值