一.哈希表实例
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;
}