1、单向链表
实现思路:创建Node类,包括自己的数据和指向下一个;创建Node类,包括头尾节点,实现添加、删除、输出等功能。
tips:n = n.next不破坏链表结果,而n.next = n.next.next就等于是n节点的next属性变成了再下一个,即指向n+1个节点的指针丢失,但实际上n+1节点仍在,只不过从链表中去除。
具体代码:
public class NodeList<Integer> {
class Node<Integer> {
Node<Integer> next = null; //指向下一节点
Integer data; //节点所存数据 //Node类构造方法
public Node(Integer d) {
this.data = d;
}
}
Node<Integer> head; //链表的头节点
Node<Integer> last; //链表的尾节点
int length; //链表长度 //链表的无参构造方法
public NodeList() {
this.head = new Node<Integer>(null); //头节点为空
} //创建链表的同时添加第一个数据
public NodeList(Integer d) {
this.head = new Node<Integer>(d);
this.last = head; //此时头尾节点一样
length++;
} //尾部添加节点
public void add(Integer d) {
if (head == null) {
head = new Node<Integer>(d);
last = head;
length++;
} else {
Node<Integer> newNode = new Node<Integer>(d);
last.next = newNode; //令之前的尾节点指向新节点
last = newNode; //新节点成为尾节点
length++;
}
} //删除指定数据
public boolean del(Integer d) {
if (head == null) {
return false;
}
Node<Integer> n = head; //从头开始判断
if (n.data == d) {
head = head.next;
length--;
return true;
} while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next; //n节点指向了n+2节点
length--;
return true;
}
n = n.next; //正常移动不破坏链表
}
return false;
}
public void print() {
if (head == null) {
System.out.println("此链表为空!");
}
Node<Integer> n = head;
while (n != null) {
System.out.println(n.data+",");
n = n.next;
}
}
}
2016-12-13 16:22:13
2、栈(链式结构)
实现思路:仍然使用节点,最重要的是栈顶节点top,利用其完成压、弹栈操作。
tips:出栈就是改栈顶为下一个,压栈就是新栈顶与原栈顶建立链接。
具体代码:
public class Stack {
class Node {
Object data;
Node next = null; public Node(Object d) {
this.data = d;
}
} Node top; //创建栈顶节点 //出栈
public Object pop() {
if (top != null) {
Object d = top.data;
top = top.next; //栈顶改为下一个
return d;
}
return null;
} //压栈
public void push(Object d) {
Node n = new Node(d);
n.next = top; //与原栈进行连接
top = n;
} //输出栈顶的元素
public Object peek() {
return top.data;
}
}
2016-12-14 14:14:12
3、队列(链式结构)
实现思路:创建首尾节点first、last,实现入队出队操作。
tips:记得判断是否为空。
具体代码:
public class Queue {
class Node {
Object data;
Node next; public Node(Object d) {
this.data = d;
}
} //创建队首队尾指针
Node first;
Node last; //入队
public void enQueue(Object d) {
if (first == null) {
first = new Node(d);
last = first;
} else {
last.next = new Node(d); //原队尾指向新节点
last = last.next; //更改队尾
}
} //出队
public Object deQueue() {
if (first != null) {
Object item = first.data; //保存原队首数据
first = first.next; //更改队首
return item;
}
return null;
}
}
2016-12-14 14:27:16
4、树的遍历
4.1、前序preorder
顺序:根左右
非递归:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public List<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> preorder = new ArrayList<Integer>();
if (root == null) {
return preorder;
}
stack.push(root);
while (!stack.empty()) {
TreeNode node = stack.pop();
preorder.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return preorder;
}
}
Traverse:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> preorder = new ArrayList<Integer>();
helper(root, preorder);
return preorder;
}
private void helper(TreeNode root, ArrayList<Integer> preorder) {
if (root == null) {
return;
}
preorder.add(root.val);
helper(root.left, preorder);
helper(root.right, preorder);
}
Divide & Conquer:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> preorder = new ArrayList<Integer>();
if (root == null) {
return preorder;
}
//Divide
ArrayList<Integer> left = preorderTraversal(root.left);
ArrayList<Integer> right = preorderTraversal(root.right);
//Conquer
preorder.add(root.val);
preorder.addAll(left);
preorder.addAll(right);
return preorder;
}
}
4.2、中序inorder
顺序:左根右
非递归:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
ArrayList<Integer> inorder = new ArrayList<Integer>();
TreeNode curt = root;
while (curt != null || !stack.empty()) {
while (curt != null) {
stack.add(curt);
curt = curt.left;
}
curt = stack.peek();
stack.pop();
inorder.add(curt.val);
curt = curt.right;
}
return inorder;
}
}
Traverse:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> inorder = new ArrayList<Integer>();
helper(root, inorder);
return inorder;
}
private void helper(TreeNode root, ArrayList<Integer> inorder) {
if (root == null) {
return;
}
helper(root.left, inorder);
inorder.add(root.val);
helper(root.right, inorder);
}
}
Divide & Conquer:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> inorder = new ArrayList<Integer>();
if (root == null) {
return inorder;
}
ArrayList<Integer> left = inorderTraversal(root.left);
ArrayList<Integer> right = inorderTraversal(root.right);
inorder.addAll(left);
inorder.add(root.val);
inorder.addAll(right);
return inorder;
}
}
4.3、后序postorder
顺序:左右根
非递归:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in ArrayList which contains node values.
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> postorder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode prev = null; //前一个遍历节点
TreeNode curr = root;
if (root == null) {
return postorder;
}
stack.push(root);
while (!stack.empty()) {
curr = stack.peek();
if (prev == null || prev.left == curr || prev.right == curr) {
//往下遍历
if (curr.left != null) {
stack.push(curr.left);
} else if (curr.right != null) {
stack.push(curr.right);
}
} else if (curr.left == prev) {
//从左往上遍历
if (curr.right != null) {
stack.push(curr.right);
}
} else {
//从右往上遍历
postorder.add(curr.val);
stack.pop();
}
prev = curr;
}
return postorder;
}
}
递归与上面一样
2017-01-27