首页 > 编程知识 正文

二叉树的三种遍历例题,完全二叉树和二叉树

时间:2023-05-06 12:32:39 阅读:156093 作者:1062

定义节点

# include iostream # includestackusingnamespacestd; typedef char ElemType; typedefstructbtnode//binary treenode { tsdmtleftchild; tsdmt rightchild; elemtype数据; }BtNode,* BinaryTree;新建节点

tsdmt Buynode () TSD MTS=(ts DMT ) malloc ) sizeof ) Btnode ); if(nullptr==s ) exit(1) 1; memset(s,0,sizeof ) Btnode ); 返回s; } 创建二叉树超级之夜(const char* is,int n,ElemType val ) {int pos=-1; for(intI=0; i n; I () if ) is[I]==val ) {pos=i; 黑; }}return pos; }tsDMTcreatepi(constchar*pr,const char* is,int n ) ) {tsdmt s=nullptr; if(n0 ) {s=Buynode ); s-data=pr[0]; //intpos=findis(is,n,pr[0]; if(pos==-1 ) exit(1) 1; s-leftchild=createpi(pr1,is,pos ); s-rightchild=createpi(prpos1,is pos 1,n-pos-1 ); }return s; (/)基于给定的前扫描和中扫描结果创建二叉树tsDMTcreatetreepi(constchar*pr,const char* is ) if (pr==nullptr|| is==nullp ) intin=Strlen(is; if(pn!=in ) return nullptr; returncreatepi(pr,is,pn ); }tsDMTcreateIP(constchar*is,const char* ps,int n ) {tsdmt s=nullptr; if(n0 ) {s=Buynode ); s-data=ps[n - 1]; intpos=findis(is,n,ps[n - 1] ); if(pos==-1 ) exit(1) 1; s-leftchild=createIP(is,ps,pos ); s-rightchild=createIP(ispos1,ps pos,n - pos - 1 ); }return s; //根据给定的中顺序扫描和后续扫描的结果创建二叉树tsDMTcreatetreeip(constchar*is,const char* ps ) (if ) is==nullptr|||PS==nullptr intpn=Strlen(PS; if (英!=pn ) return nullptr; returncreateIP(is,ps,in ); }int main () {BinaryTree root=nullptr; char pr[]={ 'ABCDEFGH' }; char is[]={ 'CBEDFAGH' }; char ps[]={ 'CEFDBHGA' }; //root=createtreepi(pr,is ); root=createtreeip(is,ps ); nicepreorder(root; niceinorder(root; nicepastorder(root; 返回0; } 递归遍历二叉树

1 .超前横移

语音播放器(tsdmtptr ) if ) ptr!=nullptr(coutptr-data ' ); preorder(ptr-leftchild; preorder(ptr-rightchild ); )2.中序扫描

voidinorder(tsDMTptr ) if ) ptr!=nullptr(inorder(ptr-leftchild ) ); cout ptr-data '; inorder(ptr-rightchild ); }3.后续横移

voidpastorder(tsDMTptr ) if ) ptr!=nullptr (past order (ptr-leftchild ) ); pastorder(ptr-rightchild ); cout ptr-g

t;data << " ";}}

非递归遍历二叉树

1.前序遍历

void NicePreOrder(tsdmt ptr){if (ptr == nullptr) return;std::stack<tsdmt> st;st.push(ptr);while (!st.empty()){ptr = st.top(); st.pop();cout << ptr->data << " ";if (ptr->rightchild != nullptr){st.push(ptr->rightchild);}if (ptr->leftchild != nullptr){st.push(ptr->leftchild);}}cout << endl;}

2.中序遍历

void NiceInOrder(tsdmt ptr){if (ptr == nullptr) return;std::stack<tsdmt> st;while(ptr != nullptr || !st.empty()){ while (ptr != nullptr){st.push(ptr);ptr = ptr->leftchild;}ptr = st.top(); st.pop();cout << ptr->data << " ";ptr = ptr->rightchild; }cout << endl;}

3.后序遍历

void NicePastOrder(tsdmt ptr){if (ptr == nullptr) return;tsdmt tag = nullptr;std::stack<tsdmt> st;while (ptr != nullptr || !st.empty()){while (ptr != nullptr){st.push(ptr);ptr = ptr->leftchild;}ptr = st.top(); st.pop();if (ptr->rightchild == nullptr || ptr->rightchild == tag){ cout << ptr->data << " ";tag = ptr;ptr = nullptr;}else{st.push(ptr);ptr = ptr->rightchild;}}cout << endl;}

4.层次遍历

void NiceLevelOrder(tsdmt ptr){if (ptr == nullptr) return;std::queue<tsdmt> qu;qu.push(ptr);while (!qu.empty()){tsdmt p = qu.front(); qu.pop();cout << p->data << " ";if (p->leftchild != nullptr){qu.push(p->leftchild);}if (p->rightchild != nullptr){qu.push(p->rightchild);}}cout << endl;}

5.Z字型遍历

void NiceZLOrder(tsdmt ptr){if (ptr == nullptr) return;std::stack<tsdmt> sta,stb;sta.push(ptr);while (!sta.empty() || !stb.empty()){while (!sta.empty()){ptr = sta.top(); sta.pop();cout << ptr->data << " ";if (ptr->leftchild != nullptr) stb.push(ptr->leftchild);if (ptr->rightchild != nullptr) stb.push(ptr->rightchild);}while (!stb.empty()){ptr = stb.top(); stb.pop();cout << ptr->data << " ";if (ptr->rightchild != nullptr) sta.push(ptr->rightchild);if (ptr->leftchild != nullptr) sta.push(ptr->leftchild);}}cout << endl;}

获取二叉树结点个数

int GetSize(tsdmt ptr){if (ptr == nullptr) return 0;else return GetSize(ptr->leftchild) + GetSize(ptr->rightchild) + 1;}

获取二叉树深度

int GetDepth(tsdmt ptr){if (ptr == nullptr)return 0;else return std::max(GetDepth(ptr->leftchild), GetDepth(ptr->rightchild)) + 1;} int GetNiceDepth(tsdmt ptr){int sum = 0;if (ptr == nullptr) return sum;std::queue<tsdmt> qu;qu.push(ptr);while (!qu.empty()){int n = qu.size();while (n--){ptr = qu.front(); qu.pop();if (ptr->leftchild != nullptr) qu.push(ptr->leftchild);if (ptr->rightchild != nullptr) qu.push(ptr->rightchild);}sum += 1;}return sum;}

判断是否是满二叉树

bool Is_Full(tsdmt ptr){int he = 1;bool res = true;if (ptr == nullptr) return res;std::queue<tsdmt> qu;qu.push(ptr);while (!qu.empty()){int n = qu.size();if (n != he){res = false;break;}while (n--){ptr = qu.front(); qu.pop();if (ptr->leftchild != nullptr) qu.push(ptr->leftchild);if (ptr->rightchild != nullptr) qu.push(ptr->rightchild);}he += he;}return res;}

判断是否是完全二叉树

bool Is_Comp(tsdmt ptr){bool res = true;if (ptr == nullptr) return res;std::queue<tsdmt> qu;qu.push(ptr);while (!qu.empty()){ptr = qu.front(); qu.pop();if (ptr == nullptr) break;qu.push(ptr->leftchild);qu.push(ptr->rightchild);}while (!qu.empty()){ptr = qu.front(); qu.pop();if (ptr != nullptr){res = false;break;}}return res;}

查找值

tsdmt FindValue(tsdmt ptr, ElemType val){if (ptr == nullptr || ptr->data == val){return ptr;}else{tsdmt p = FindValue(ptr->leftchild, val);if (p == nullptr){p = FindValue(ptr->rightchild, val);}return p;}}

查找直接双亲结点

tsdmt Parent(tsdmt ptr, tsdmt child){if (ptr == nullptr || ptr->leftchild == child || ptr->rightchild == child){return ptr;}else{tsdmt p = Parent(ptr->leftchild, child);if (p == nullptr){p = Parent(ptr->rightchild, child);}return p;}}tsdmt FindParent(tsdmt ptr, tsdmt child){if (ptr == nullptr || child == nullptr || ptr == child){return nullptr;}else{return Parent(ptr, child);}}

光亮的花卷

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。