文章目录
链表
头插法
public void addFirst(int data) {
// 1. 拿到一个实体
Node node = new Node(data);
// 2. 插入
// 如果是第一次插入,直接到头节点
if (this.head == null) {
this.head = node;
} else { //不是第一次插入
node.next = this.head; // 插入的节点指向头节点
this.head = node; // 更新头节点
}
}
尾插法
public void addLast(int data) {
// 1. 拿到一个实体
Node node = new Node(data);
Node cur = this.head;
// 2. 插入
// 如果是第一次插入,直接到头节点
if (this.head == null) {
this.head = node;
} else {
// 找尾巴
while (cur.next != null) {
cur = cur.next;
}
// 退出上面的循环,cur所执行的位置就是尾节点
cur.next = node;
}
}
任意位置插入
public boolean addIndex(int index, int data) {
Node node = new Node(data);
Node cur = searchIndex(index);
// 如果链表为空,直接插入到头节点
if (cur == null) {
node.next = this.head;
this.head = node;
} else { // 链表不为空,插入到 cur 的位置处
node.next = cur.next; // 将node链接到cur的下一个节点
cur.next = node; // 再将cur链接到node
}
return true;
}
查找是否包含
public boolean contains(int key) {
Node cur = this.head;
while (cur != null) {
if (cur.data == key) {
return true;
}
cur = cur.next;
}
return false;
}
删除元素(首次出现)
public int remove(int key) {
int oldData = 0;
Node pre = searchPre(key);
// 1. 若没有找到
if (pre == null) {
// return -1;
throw new UnsupportedOperationException("没有key的前驱");
}
// 2. 找到了,并且在第一个节点
if (pre == this.head && pre.data == key){
oldData = this.head.data;
this.head = this.head.next;
return oldData;
}
// 3. 找到了,并且不在第一个节点
Node delNode = pre.next; // 确定要删除的节点的位置
pre.next = delNode.next; // 让要删除的节点的前驱指向要删除的节点的后一个节点,进而删除该节点
return 0;
}
树
先序遍历(DLR)
public void preOrder(TreeNode treeNode) {
if(treeNode!= null) {
System.out.print(treeNode.getValue()+" ");
preOrder(treeNode.getLeftTreeNode());
preOrder(treeNode.getRightTreeNode());
}
}
中序遍历(LDR)
public void inOrder(TreeNode treeNode) {
if(treeNode!=null) {
midOrder(treeNode.getLeftTreeNode());
System.out.print(treeNode.getValue() +" ");
midOrder(treeNode.getRightTreeNode());
}
}
后序遍历(LRD)
public void afterOrder(TreeNode treeNode) {
if(treeNode!=null) {
afterOrder(treeNode.getLeftTreeNode());
afterOrder(treeNode.getRightTreeNode());
System.out.print(treeNode.getValue() + " ");
}
}
层次遍历
public void levelTraverse(TreeNode root) {
if (root == null) {
return;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
System.out.print(node.val+" ");
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
深度遍历
public void depthOrderTraverse(TreeNode root) {
if (root == null) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
System.out.print(node.val+" ");
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
}
查找
public boolean Search(Integer val) {
if(root == null) {
return false;
}
TreeNode treeNode = root;
while(treeNode!=null) {
if(treeNode.getValue()==val)
return true;
else if(treeNode.getValue()>val)
treeNode = treeNode.getLeftTreeNode();
else
treeNode = treeNode.getRightTreeNode();
}
return false;
}