typedef struct TreeNode{
string val;
TreeNode* left;
TreeNode* right;
TreeNode() { val = "0", left = nullptr, right = nullptr; }
TreeNode(string x) { val = x, left = nullptr, right = nullptr; }
}TreeNodeTemp, *BiTree;
static int index = 0;
void CreateTree(BiTree &root, vector<string>& temp) {
index++;
if (temp[index] == "#" && index < temp.size())
root = nullptr;//这个地方出去递归
else {
root = new TreeNode(temp[index]);
CreateTree(root->left, temp);
CreateTree(root->right, temp);
}
}
//先序遍历 栈
vector<string> PreTraverse(BiTree root) {
vector<string> strRet;
stack<BiTree> preStack;
BiTree cur = root;
//preStack.push(root);
//我的思路是到节点先压自身的值 然后压入右节点 去往左节点 压值 如果左节点为空 则退栈 此时退出的栈都是右节点的 然后重复
while (!preStack.empty() || cur) {
if (cur) {
strRet.push_back(cur->val);
if (cur->right)
preStack.push(cur->right);//并没有压入过左节点 都是访问的
cur = cur->left;//这个地方会压入一个空指针 然后去到else 出栈
}
else {
cur = preStack.top();
preStack.pop();
}
//cur
}
return strRet;
}
//中序遍历 栈
vector<string> MidTraverse(BiTree root) {
vector<string> strRet;
stack<BiTree> midStack;
BiTree cur = root;
//preStack.push(root);
//我的思路是一直压左节点 空值时 退栈 去往右节点 重复
while (!midStack.empty() || cur) {
if (cur) {
midStack.push(cur);
cur = cur->left;//空值去往else
}
else {
BiTree temp = midStack.top();//不能使用cur 因为cur的左节点为null
if(temp)//前面循环并不会压入空值 因此不要也行
strRet.push_back(temp->val);
midStack.pop();
if (temp->right)
cur = temp->right;
}
}
return strRet;
}
//后序遍历 栈
vector<string> AfterTraverse(BiTree root) {
vector<string> strRet;
stack<BiTree> afterStack;
BiTree cur = root;
BiTree lastVisit = cur;
//preStack.push(root);
//分析 每个节点都是第三次访问时才输出
while (!afterStack.empty() || cur) {
if (cur) {
afterStack.push(cur);
//if (cur->right)
// afterStack.push(cur->right);
/*if (cur->left)
afterStack.push(cur->left);*/
cur = cur->left;
//lastVisit = cur;
}
else {
BiTree temp = afterStack.top();//不能出栈 出栈会丢失
if (temp->left != lastVisit && temp->right != lastVisit) {
strRet.push_back(temp->val);
lastVisit = temp;
afterStack.pop();
}
else if (temp->left == lastVisit) {
if (temp->right) {
cur = temp->right;
}
else {
strRet.push_back(temp->val);
lastVisit = temp;
afterStack.pop();
}
}
else if (temp->right == lastVisit)
{
strRet.push_back(temp->val);
lastVisit = temp;
afterStack.pop();
}
}
}
return strRet;
}
//层序遍历
vector<vector<string>> LayerTraverse(BiTree root) {
vector<vector<string>> strRet;
queue<BiTree> layerQueue;
BiTree cur = root;
//BiTree lastVisit = cur;
layerQueue.push(root);
while (!layerQueue.empty()) {
int size = layerQueue.size();
vector<string> temp;
for (; size--; ) {
cur = layerQueue.front();
layerQueue.pop();
temp.push_back(cur->val);
if (cur->left)
layerQueue.push(cur->left);
if (cur->right)
layerQueue.push(cur->right);
}
strRet.push_back(temp);
}
return strRet;
}
int main() {
//TreeNode* temp = new TreeNode();
BiTree temp;
//vector<int> test{ 1, 2, 3, 4, 5, 6 };
vector<string> test{ "5", "a", "2", "13", "#", "#", "#", "4" , "6", "#", "#", "8", "#", "#"};
int size = test.size() - 1;
CreateTree(temp, test);
vector<string> myPree = AfterTraverse(temp);
for (auto mypre : myPree) {
cout << mypre << endl;
}
vector<vector<string>> myLayer = LayerTraverse(temp);
for (auto mlayer : myLayer) {
for (auto la : mlayer) {
cout << la << " ";
}
cout << endl;
}
system("pause");
return 0;
}
数据结构树遍历前序 中序 和后序以及层序非递归实现
最新推荐文章于 2022-03-15 21:20:53 发布