/* * @Author: Xiyuan Yang xiyuan_yang@outlook.com * @Date: 2025-03-23 11:47:09 * @LastEditors: Xiyuan Yang xiyuan_yang@outlook.com * @LastEditTime: 2025-03-23 15:04:18 * @FilePath: /20250316_stack_and_queue/template_stack.cpp * @Description: Template of increasing stack * Do you code and make progress today? * Copyright (c) 2025 by Xiyuan Yang, All Rights Reserved. */
/* template of increasing stack */
#include<iostream>
intmain(){ int size = 10; //the list that needs to be scanned int *list = newint[size];
for(int i = 0; i < size; ++i){ std::cin >> list[i]; }
//the simulation of stack using traditional array //the stack stores the index of several numbers, but not the value of them! int *st = newint[size]; int top = 0; st[top] = 0;
//模版目标:找到每一个元素后面第一个比其大的元素,如果不存在则为-1 int *ans = newint[size];
当然也可以自定义其他的比较函数,那么while循环里面的内容就需要更改 */ for(int i = 1; i < size; i++){ //the stack is not empty and maintain the increasing sequence while(top >= 0 && list[st[top]] < list[i]){ //now the top of the stack has been poped, which means the first element that is bigger than the top index has appeared! ans[st[top]] = i; top --; }
//if the stack has been cleaned, then top = -1, doens't lead to contradiction //push new element st[++ top] = i; }
//for the elemnet remaining, there is no element backwards which is bigger than it. while(top >= 0){ ans[st[top --]] = -1; }
//check the answers for(int i = 0; i < size; ++i){ std::cout << list[i] << " "; } std::cout << std::endl; for(int i = 0; i < size; ++i){ std::cout << ans[i] << " "; }
/* * @Author: Xiyuan Yang xiyuan_yang@outlook.com * @Date: 2025-03-23 11:47:09 * @LastEditors: Xiyuan Yang xiyuan_yang@outlook.com * @LastEditTime: 2025-03-23 15:39:34 * @FilePath: /20250316_stack_and_queue/template_queue.cpp * @Description: template of increasing queue * Do you code and make progress today? * Copyright (c) 2025 by Xiyuan Yang, All Rights Reserved. */
/* Template of Increasing Queue */ #include<iostream> intmain(){ int size = 10; int windows_size = 3; //the list that needs to be scanned int *list = newint[size];
for(int i = 0; i < size; ++i){ std::cin >> list[i]; }
//the simulation of queue using traditional array int *qu = newint[windows_size + 1]; int left_index = 0, right_index = 0;
//模版目标:找到每一个元素后面第一个比其大的元素,如果不存在则为-1 int *ans = newint[size - windows_size + 1];
/* 此处使用单调递减的队列,可以用来求得每一个滑动窗口中的最小值 此处left_index是出队列的地方: 1.如果新加入的元素超过了windows的限制 */ for(int i = 1; i < size; i++){ //exceed the window while(left_index <= right_index && qu[left_index] <= i - windows_size){ ++ left_index; }
// maintain the decreasing size of the queue while(left_index <= right_index && list[qu[right_index]] < list[i]){ -- right_index; } //此处和单调栈很类似,为了维护单调性,我们需要pop掉一些不满足单调性的元素(此处是为了满足单调递减的性质)
structnode { func fu; int n;//the input int pc;//the status int fr, gr;//the return value node(func f, int n, int p, int fr, int gr):fu(f), n(n), pc(p), fr(fr), gr(gr){} };
/* The recursive implementation: traverse(x.left) traverse(x.right) traverse(x)
the default function if(node.left == nullptr && node.right == nullptr){ traverse the node of x }
*/
classNode; structInfo { // Add whatever you want here int current_size; // now the traversa has gone to which section int pc; //pc = 1: The initial statement //pc = 2: Has been traversed the left node //pc = 3: has been traversed the right node const Node* root_node; int *arr; int total_treesize;
Info(int current= 0, int pc= 1, const Node* root= nullptr, int *arr =nullptr, int total_treesize = 0) :current_size(current), pc(pc),root_node(root), arr(arr),total_treesize(total_treesize) { // TODO: }
~Info() { // TODO: } };
classNode { // DO NOT edit this part private: int _data; Node *_lson, *_rson; mutable Info _info; // custom data
friend Node *genTree();
public: Node();
Node(int data);
~Node();
intdata()const; const Node *constlson()const; const Node *constrson()const; Info &info()const; };
voidtraversePostOrder(const Node *const root, int *arr, int tree_size){ // TODO int currentsize = 0; Info* st = new Info[tree_size];//the simulation of stack int top = 0; st[top] = Info(0, 1, root, arr, tree_size);