#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;
class TreeNode
{
public:
TreeNode(char ch)
{
data = ch;
parent = lchild = rchild = nullptr;
}
TreeNode* addLeftChild(char ch)
{
TreeNode* node = new TreeNode(ch);
lchild = node;
node->parent = this;
return node;
}
TreeNode* addRightChild(char ch)
{
TreeNode* node = new TreeNode(ch);
rchild = node;
node->parent = this;
return node;
}
public:
char data;
TreeNode* parent;
TreeNode* lchild;
TreeNode* rchild;
};
//二叉树(二叉链表)前序遍历的非递归实现
void PreOrder(TreeNode* root)
{
stack<TreeNode*> s;
while (root != nullptr || !s.empty())
{
while (root != nullptr)
{
s.push(root);
root = s.top()->lchild;
printf("%c ", s.top()->data);//访问结点数据
}
if (!s.empty())
{
root = s.top()->rchild;
s.pop();
}
}
}
//二叉树(二叉链表)中序遍历的非递归实现
void InOrder(TreeNode* root)
{
stack<TreeNode*> s;
while (root != nullptr || !s.empty())
{
while (root != nullptr)
{
s.push(root);
root = root->lchild;
}
if (!s.empty())
{
printf("%c ", s.top()->data);//访问结点数据
root = s.top()->rchild;
s.pop();
}
}
}
//二叉树(二叉链表)后序遍历的非递归实现
class StackElementForPostOrder
{
public:
StackElementForPostOrder(TreeNode* ptr,int flag)
{
this->ptr = ptr;
this->flag = flag;
}
public:
TreeNode* ptr;
int flag;
};
void PostOrder(TreeNode* root)
{
stack<StackElementForPostOrder> s;
while (root != nullptr || !s.empty())
{
while (root != nullptr)
{
s.push(StackElementForPostOrder(root, 1));
root = root->lchild;
}
while (!s.empty() && s.top().flag == 2)
{
printf("%c ", s.top().ptr->data);//访问结点数据
s.pop();
}
if (!s.empty())
{
s.top().flag = 2;
root = s.top().ptr->rchild;
}
}
}
//二叉树(二叉链表)层序遍历
void LeverOrder(TreeNode* root)
{
if (root == nullptr)
return;
queue<TreeNode*> q;
q.push(root);
while (!q.empty())
{
TreeNode* node = q.front();
printf("%c ", node->data);//访问结点数据
q.pop();
if (node->lchild != nullptr)q.push(node->lchild);
if (node->rchild != nullptr)q.push(node->rchild);
}
}
//二叉树(二叉链表)获取树的最大深度
int BTreeDepth(TreeNode* root)
{
if (root == nullptr)
return 0;
int res = 0;
queue<TreeNode*> q;
q.push(root);
while (!q.empty())
{
int size = q.size();
while (size--)
{
TreeNode* cur = q.front();
q.pop();
if (cur->lchild) q.push(cur->lchild);
if (cur->rchild) q.push(cur->rchild);
}
res++;
}
return res;
}
//二叉树(二叉链表)获取指定结点所在的层号
int GetTreeNodeLayer(TreeNode* node)
{
int lay = 0;
TreeNode* p;
for (p=node;p!=nullptr;p=p->parent)
lay++;
return lay;
}
//二叉树(二叉链表)获取两个结点的最近公共祖先
TreeNode* LowestCommonAncestor(TreeNode* node1, TreeNode* node2)
{
for (TreeNode* p = node1; p != nullptr; p = p->parent)
{
for (TreeNode* q = node2; q != nullptr; q = q->parent)
{
if (p == q)
return p;
}
}
return nullptr;
}
void main()
{
TreeNode root('A');
TreeNode* A = &root;
TreeNode* B = A->addLeftChild('B');
TreeNode* D = B->addLeftChild('D');
TreeNode* E = B->addRightChild('E');
TreeNode* C = A->addRightChild('C');
TreeNode* F = C->addLeftChild('F');
TreeNode* G = C->addRightChild('G');
TreeNode* H = F->addLeftChild('H');
TreeNode* I = F->addRightChild('I');
LeverOrder(A);
printf("\n完成\n");
TreeNode* lca = LowestCommonAncestor(A, D);
printf("最近公共祖先:%c\n", lca->data);
getchar();
}
二叉树(二叉链表)的非递归遍历
于 2023-12-15 16:21:02 首次发布