• 回答数

    2

  • 浏览数

    286

进击的银酱
首页 > 英语培训 > 非递归遍历英文

2个回答 默认排序
  • 默认排序
  • 按时间排序

七彩娃娃豆

已采纳

//二叉树节点定义class TreeNodeElement{ public: TreeNodeElement();TreeNodeElement(int value);TreeNodeElement(int value,TreeNodeElement* l,TreeNodeElement* r);~TreeNodeElement();private: public: int _value;TreeNodeElement* _l;TreeNodeElement* _r;};typedef TreeNodeElement* TreeNode; //递归实现(visit)void Visit(TreeNode node){ cout<_value<<" ";} //二叉树中序遍历的递归和非递归实现://递归遍历void BinRetriveATree(TreeNode root,void (* visit)(TreeNode)){ if (root) { BinRetriveATree(root->_l,visit); (*visit)(root); BinRetriveATree(root->_r,visit); }}//非递归遍历,添加#include void BinRetriveATreeWithoutRecurve(TreeNode root,void (* visit)(TreeNode)){ stack tree;while ((root != NULL) || (!tree.empty())) { while (root != NULL) { tree.push(root);root = root->_l; }if (!tree.empty()) { root = tree.top();tree.pop();visit(root);root = root->_r; //转到右子树} }}

非递归遍历英文

93 评论(8)

小红粉菲菲

全部操作都有了:#includeclass BTreeNode{int data;BTreeNode *lchild;BTreeNode *rchild;public: friend class BTree; BTreeNode(){data=0;lchild=NULL;rchild=NULL;}; BTreeNode &operator=(const BTreeNode&);};class BTree{BTreeNode *build0(int i);int*a;int n;protected:BTreeNode *root;public: BTree(){}; BTree(int a[],int i){this->a=a;this->n=i;root=build0(1);}; void inorder(BTreeNode *t); void inorder1(){cout<<"中序递归遍历:"<<'\t';inorder(root);cout<rchild ,n+1);for(int i=0;i<6*(n-1);i++)cout<<" ";cout<<"..."<data <lchild ,n+1);}};void BTree::postorder0(BTreeNode *tree)//非递归后序遍历 { BTreeNode *p; BTreeNode *s[100];//用一个指针数组来用作栈 int top=-1;//下标 int mack[100];//标志数组 p=tree; do { while(((p->lchild)!=NULL)&&(mack[top+1]!=2))//循环 直到让p向左滑到最左子树 { top=top+1; s[top]=p;//每循环一次,当前结点入栈 mack[top]=1;//结点第一次入栈时,标志为1 p=p->lchild;//找最左子树 } cout<data<<'\t'; //输出末节点 //非递归后序遍历!p=s[top];//出栈顶元素 top=top-1;//每出一个栈顶元素,下标就后退一位 while(mack[top+1]==1)//若结点是第一次出栈,则再入栈 { top=top+1; s[top]=p; mack[top]=2;//结点第二次入栈时,标志为2 p=p->rchild;//访问右子树 } }while(top!=-1);cout<data ;};void BTree::preorder0(BTreeNode *q) //前序非递归遍历函数{ BTreeNode *stack[30],*p; int top;top=0; //for(int i=0;i<30;i++) stack[i]=new BTreeNode; stack[top]=root; while(top>=0) { while(stack[top]!=NULL) { p=stack[top]; cout<data<<'\t'; p=p->lchild; stack[++top]=p; } top--; if(top>=0) { p=stack[top]; stack[top]=p->rchild; } } cout<lchild; p->lchild = p ->rchild; p->rchild = temp; changzhishu(p ->lchild); changzhishu(p ->rchild); };int BTree::leaf(BTreeNode *T){if(T!=NULL)if((T->lchild==NULL)&&(T->rchild==NULL))return 1; //求叶子总数!!!!!!else return leaf(T->lchild )+leaf(T->rchild ); else return 0;};int BTree::depth(BTreeNode *t){if (t==NULL) return NULL;else return ((depth(t->lchild)+1)>(depth(t->rchild )+1)?(depth(t->lchild)+1):(depth(t->rchild )+1));};BTreeNode &BTreeNode::operator=(const BTreeNode &obj){data=obj.data;lchild=obj.lchild;rchild=obj.rchild;return *this;};BTreeNode * BTree::build0(int i){int r,l;BTreeNode *p;if((i<=n)&&(a[i-1]!=NULL)){r=2*i+1;l=2*i; //建二叉树p=new BTreeNode;p->data=a[i-1];p->lchild=build0(l);p->rchild=build0(r);return (p);}else return (NULL);};void BTree::inorder(BTreeNode *t){if(t!=NULL) //中序遍历{inorder(t->lchild);cout<data<<'\t';//用 visit?inorder(t->rchild);};};void BTree::inorder0(BTreeNode *t){int top=0;BTreeNode *stack[10],*p;stack[top]=t;while(top>=0){ //非递归中序 while(stack[top]!=NULL) {p=stack[top]->lchild; stack[++top]=p;} top--; if(top>=0) {p=stack[top]; cout<data<<'\t'; stack[top]=p->rchild; }};};void BTree::preorder(BTreeNode *s){if(s!=NULL){cout<data<<'\t';preorder(s->lchild); //前序遍历preorder(s->rchild);};};void BTree::postorder(BTreeNode *t){if(t!=NULL){postorder(t->lchild );postorder(t->rchild );cout<data<<'\t';}; //后序遍历};void main(){int x=0;int b[100];int v,i;while(x!=1){cout<<"请选择功能,第一次必须先建立二叉树!:"<>x;}cout<<"请输入二叉树个数:"<>v;cout<<"请从逐层左到右输入二叉树:"<>b[i];cout<<"输入完成";BTree a(b,v);while (x!=12){switch(x){case 2:a.preorder1 ();break;case 3:a.preorder01();break;case 4:a.inorder1();break;case 5:a.inorder01();break;case 6:a.postorder1();break;case 7:a.postorder01();break;case 8:a.depth0();break;case 10:a.prnt();break;case 9:a.leaf1();break;case 11:a.changzhishu1();cout<<"交换完成!";break;case 12:cout<<"欢迎再次使用!"<>x;};}

266 评论(15)

相关问答