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*); //尋找節(jié)點(diǎn)(遞歸)
static void rprint(BiNode*, int); //簡(jiǎn)易樹形打?。ㄟf歸)
public:
BiTree(); //無(wú)參構(gòu)造
BiTree(int); //有參構(gòu)造
void preprint(); //前序遍歷
void inprint(); //中序遍歷
void postprint(); //后序遍歷
void print(); //簡(jiǎn)易樹形
BiNode* find(int); //尋找節(jié)點(diǎn)
bool insert(int, int, int); //插入節(jié)點(diǎn)
};
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遞歸)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 同時(shí)發(fā)布:https://notes.daiyuhe.com/traversal-of-binary-tree/ ...
- 二叉樹的遍歷也是面試中的經(jīng)典算法題,是算法相關(guān)崗位必須掌握的內(nèi)容。 但是網(wǎng)上能找到的資料大部分是C/C++的,本文...
- 此處以一個(gè)用數(shù)組表示的完全二叉樹為例,根節(jié)點(diǎn)為1 流程: 1、從根節(jié)點(diǎn)出發(fā),一路向左,把每一個(gè)左孩子(如果有)推入...
- Morris Traversal 非遞歸,不用棧,空間O(1)時(shí)間O(n) 二叉樹的形狀不能被破壞(中間過(guò)程允許改...
- 但凡是做Android開發(fā)的相信都對(duì)webview不會(huì)陌生,而且也對(duì)系統(tǒng)自帶的webview本身存在的問(wèn)題也是怨念...