摘要:
#includeusing namespace std;const int Max=20;struct Node{ Node *pLeft; Node *pRight; char chValue;};template class Stack{public: Stack(int s=Max):size(s),top(-1){a=new T[size];} ~Stack(){delete[] a;} void push(T x) { if(top-1) return a[top--]; } T getT() const { if(top!=-1) return a[top]; } boo... 阅读全文
摘要:
#include<iostream>using namespace std;const int Max=20;struct Node{ Node *pLeft; Node *pRight; char chValue;};template <class T>class Stack{public: Stack(int s=Max):size(s),top(-1){a=new T[size];} ~Stack(){delete[] a;} void push(T x) { if(top<size-1) a[++top]=x; } T pop() { if(top> 阅读全文
摘要:
1.用栈记忆:在访问途中将依次遇到的节点保存下来。版本一是模拟递归实现的结果;版本二是直接模拟递归。2.节点增加指向父节点的指针:通过指向父节点的指针来回溯,增加一个访问标志位,指示节点是否已经被访问。一、先序遍历// 先序遍历伪代码:非递归版本,用栈实现,版本1 void preOrder1(TNode* root) { Stack S; while ((root != NULL) || !S.empty()) { if (root != NULL) { Visit(root); S.push(root); // 先序就体现在这里了,先访问,再入栈 root = root->left; 阅读全文