struct BiNode{
int data;
BiNode *left;
BiNode *right;
BiNode(int x){
data = x;
left = right = NULL;
}
};
class BiTree {
private:
BiNode *root;
static void rpreprint(BiNode*); //前序遍历(递归)
static void rinprint(BiNode*); //中序遍历(递归)
static void rpostprint(BiNode*); //后序遍历(递归)
static void ipreprint(BiNode*); //前序遍历(迭代)
static void iinprint(BiNode*); //中序遍历(迭代)
static void ipostprint(BiNode*); //后序遍历(迭代)
static BiNode* rfind(int, BiNode*); //寻找节点(递归)
static void rprint(BiNode*, int); //简易树形打印(递归)
public:
BiTree(); //无参构造
BiTree(int); //有参构造
void preprint(); //前序遍历
void inprint(); //中序遍历
void postprint(); //后序遍历
void print(); //简易树形
BiNode* find(int); //寻找节点
bool insert(int, int, int); //插入节点
};
void BiTree::rpreprint(BiNode *r){
if(r == NULL)
return;
cout << r->data << " " ;
rpreprint(r->left);
rpreprint(r->right);
}
void BiTree::rinprint(BiNode *r){
if(r == NULL)
return;
rinprint(r->left);
cout << r->data << " " ;
rinprint(r->right);
}
void BiTree::rpostprint(BiNode *r){
if(r == NULL)
return;
rpostprint(r->left);
rpostprint(r->right);
cout << r->data << " ";
}
void BiTree::ipreprint(BiNode *r) {
stack<BiNode*> st;
if(r == NULL)
return;
while(r){
cout << r->data << " ";
st.push(r);
r = r->left;
while(r == NULL && !st.empty()){
r = st.top();
st.pop();
r = r->right;
}
}
}
void BiTree::iinprint(BiNode *r) {
stack<BiNode*> st;
if(r == NULL)
return;
while(r){
st.push(r);
r = r->left;
while(r == NULL && !st.empty()){
r = st.top();
st.pop();
cout << r->data << " ";
r = r->right;
}
}
}
BiNode* BiTree::rfind(int x, BiNode *r){
if(r == NULL)
return NULL;
if(r->data == x)
return r;
BiNode* found = rfind(x, r->left);
return found ? found : rfind(x, r->right);
}
void BiTree::rprint(BiNode *r, int deep) {
for(int i = 0; i < deep; i++)
cout << " ";
if(r == NULL)
cout << "[/]" << endl;
else{
cout << r->data << endl;
rprint(r->left, deep+1);
rprint(r->right, deep+1);
}
}
BiTree::BiTree(){
root = NULL;
}
BiTree::BiTree(int r){
root = new BiNode(r);
}
void BiTree::preprint(){
// rpreprint(root);
ipreprint(root);
}
void BiTree::inprint() {
// rinprint(root);
iinprint(root);
}
void BiTree::postprint() {
rpostprint(root);
}
void BiTree::print() {
rprint(root, 0);
}
BiNode* BiTree::find(int x){
return rfind(x, root);
}
bool BiTree::insert(int r, int LR, int x){
BiNode* found;
found = find(r);
if(found == NULL)
return false;
if(LR == 0){
if(found->left)
return false;
found->left = new BiNode(x);
}
else{
if(found->right)
return false;
found->right = new BiNode(x);
}
return true;
}
二叉树遍历(迭代or递归)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 同时发布:https://notes.daiyuhe.com/traversal-of-binary-tree/ ...
- 此处以一个用数组表示的完全二叉树为例,根节点为1 流程: 1、从根节点出发,一路向左,把每一个左孩子(如果有)推入...
- Morris Traversal 非递归,不用栈,空间O(1)时间O(n) 二叉树的形状不能被破坏(中间过程允许改...
- 但凡是做Android开发的相信都对webview不会陌生,而且也对系统自带的webview本身存在的问题也是怨念...