1. 快速排序
分治思想
quicksort(A []int, _start, _end int)
双向指针分别开工,右边(从_end开始)找到比(base指针)baseValue大的值,覆盖左边,然后左边(从_start开始)找到比baseValue大的值,覆盖右边,左右指针重合时,本轮结束,basevalue放在左右指针重合处
分治用递归实现
base指针左边都比baseValue小,因此左边再次排序 quicksort(A,_start,start-1)
base指针左边都比baseValue大,因此右边再次排序 quicksort(A,end+1,_end)
(start = end)
package main
import (
"fmt"
)
func main() {
fmt.Println("testing...")
var array = []int{9, 5, 6, 8, 4, 8, 6, 1, 10}
quicksort(array, 0, len(array)-1)
fmt.Println(array)
}
func quicksort(A []int, _start, _end int) {
if _start >= _end {
return
}
start, end := _start, _end
baseValue := A[start]
for start < end {
for A[end] >= baseValue && end > start {
end--
}
A[start] = A[end]
for A[start] <= baseValue && end > start {
start++
}
A[end] = A[start]
}
A[start] = baseValue
quicksort(A, _start, start-1)
quicksort(A, end+1, _end)
}
2. 二叉树
//定义节点(go)
type ListNode struct {
Val int
Left *ListNode
Right * ListNode
}
//定义节点(cpp)
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
main函数
func main() {
BT := new(ListNode)
CreatBinTree_PreOrder(BT)
fmt.Println("Create finished...")
preOrder(BT)
fmt.Println()
inOrder(BT)
fmt.Println()
postOrder(BT)
fmt.Println()
}
递归先序创建二叉树
golang new(LinkNode)时,Left和Right是nil,必须给其赋地址
*BT = ListNode{val, new(ListNode), new(ListNode)}
指针操作:传递地址path参数,就不能让形参再次被赋予一个地址,只能利用传递的地址 *path 来赋值,否则传递不了
递归先序创建二叉树
func CreatBinTree_PreOrder(BT *ListNode) { //先序创建二叉树
var val int
fmt.Scanf("%d", &val)
if val == 0 { //遇到-1停止
BT = nil
return
} else {
*BT = ListNode{val, new(ListNode), new(ListNode)} // !!!
CreatBinTree_PreOrder(BT.Left) //左
CreatBinTree_PreOrder(BT.Right) //右
}
}
visit输出
func visit(node *ListNode) {
if node.Val != 0 {
fmt.Print(node.Val, " ")
}
}
- 先序递归遍历
func preOrder(BT *ListNode) {
if BT == nil {
return
}
visit(BT)
preOrder(BT.Left)
preOrder(BT.Right)
}
迭代先序遍历二叉树(cpp)
golang中没有现成的栈,迭代遍历用C++实现
先序:中左右
中入栈->中出栈->右入栈(放栈底)->左入栈(成为中)
上述操作迭代遍历,直至遍历完成
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode *> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while(!st.empty()) {
TreeNode * node = st.top();
result.push_back(node->val);
st.pop();
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
return result;
}
};
- 中序递归遍历
func inOrder(BT *ListNode) {
if BT == nil {
return
}
inOrder(BT.Left)
visit(BT)
inOrder(BT.Right)
}
迭代中序遍历二叉树(cpp)
看作遵守中序遍历准则,一直左,存入栈,遇到空时,则弹出栈顶元素,将val存入结果数组,然后往右走一格,重复上述步骤
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode *> st;
vector<int> ans;
TreeNode * cur = root;
while(cur != NULL || !st.empty()){
if (cur != NULL){
st.push(cur);
cur = cur->left;
} else {
TreeNode * node = st.top();
ans.push_back(node->val);
st.pop();
cur = node->right;
}
}
return ans;
}
};
- 后序递归遍历
func postOrder(BT *ListNode) {
if BT == nil {
return
}
postOrder(BT.Left)
postOrder(BT.Right)
visit(BT)
}
迭代后序遍历二叉树(cpp)
后序:左右中 = (逆序)中左右
仿照先序改入栈顺序就行
class Solution {
public:
//左右中 = (逆序)中右左
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode *> st;
vector<int> ans;
if (root == NULL) return ans;
st.push(root);
while(!st.empty()){
TreeNode* node = st.top();
ans.push_back(node->val);
st.pop();
if (node->left) st.push(node->left);
if (node->right) st.push(node->right);
}
reverse(ans.begin(),ans.end());
return ans;
}
};
迭代层次遍历二叉树(cpp)
层次遍历需要使用队列数据结构,golang中没有现成的队列
3 9 20 15 7
按顺序进队列,出一个,就将其孩子节点放在队列末尾
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode *> que;
vector<vector<int>> ans;
if (root == NULL) return ans;
que.push(root);
while(!que.empty()) {
vector<int> temp;
int size = que.size();
for (int i=0;i<size;i++) {
TreeNode * node = que.front();
temp.push_back(node->val);
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
ans.push_back(temp);
}
return ans;
}