网上很多算法通过直接将二叉树结点连接,从而构成二叉树,这里我构建了一个二叉树类,通过用户控制输入来建立二叉树。
有层序遍历,递归、非递归的前序遍历、中序遍历、后序遍历算法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| #include<iostream> #include<queue> #include<stack> using namespace std; class Binarytreenode { public: int data; Binarytreenode *leftchild; Binarytreenode *rightchild; Binarytreenode(){}; Binarytreenode(const int &a,Binarytreenode *l=NULL,Binarytreenode*r=NULL) { this->data=a; this->leftchild=l; this->rightchild=r; } };
class BinaryTree { private: public: Binarytreenode *root; BinaryTree(){root =new Binarytreenode;}; ~BinaryTree(){delete[]root;} void visit(Binarytreenode*t) { cout<<t->data<<" "; } Binarytreenode* creatTree(Binarytreenode* temp) { int n; cout<<"请输入2叉树结点的值,输入-1以表示停止创建某子树"<<endl; cin>>n; if(n==-1) { return NULL; } else{ temp = new Binarytreenode; temp->data=n; temp->leftchild=creatTree(temp->leftchild); temp->rightchild=creatTree(temp->rightchild); } root=temp; return root; } void levelOrder() { queue <Binarytreenode *> nodeQueue; Binarytreenode *p=root; if (p) nodeQueue.push(p); while(!nodeQueue.empty()) { p=nodeQueue.front(); visit(p); nodeQueue.pop(); if (p->leftchild) { nodeQueue.push(p->leftchild); } if (p->rightchild) { nodeQueue.push(p->rightchild); } } } void preOrder0(Binarytreenode *root) { if (root!=NULL) { visit(root); preOrder0(root->leftchild); preOrder0(root->rightchild); } } void inOrder0(Binarytreenode *root) { if (root!=NULL) { inOrder0(root->leftchild); visit(root); inOrder0(root->rightchild); } } void postOrder0(Binarytreenode *root) { if (root!=NULL) { postOrder0(root->leftchild); postOrder0(root->rightchild); visit(root); } } void preOrder1(Binarytreenode *root) { stack<Binarytreenode *>nodeStack; Binarytreenode *p=root; while (!nodeStack.empty()||p) { if(p) { visit(p); if (p->rightchild!=NULL) { nodeStack.push(p->rightchild); } p=p->leftchild; }else { p=nodeStack.top(); nodeStack.pop(); } } } void inOrder1(Binarytreenode *root) { stack<Binarytreenode *>nodeStack; Binarytreenode *p=root; while (!nodeStack.empty()||p) { if(p) { nodeStack.push(p); p=p->leftchild; }else { p=nodeStack.top(); visit(p); p=p->rightchild; nodeStack.pop(); } } } void postOrder1(Binarytreenode *root) { stack<Binarytreenode *>nodeStack; Binarytreenode *p=root; Binarytreenode *pre=root; while (p) { while(p->leftchild!=NULL) { nodeStack.push(p); p=p->leftchild; } while (p!=NULL&& (p->rightchild==NULL || p->rightchild==pre)) { visit(p); pre=p; if (nodeStack.empty()) { return; } p=nodeStack.top(); nodeStack.pop(); } nodeStack.push(p); p=p->rightchild; }
} };
int main() { BinaryTree t1; t1.creatTree(t1.root); cout<<"层次遍历:"<<endl; t1.levelOrder(); cout<<endl<<"前序遍历(递归):"<<endl; t1.preOrder0(t1.root); cout<<endl<<"前序遍历(非递归):"<<endl; t1.preOrder1(t1.root); cout<<endl<<"中序遍历(递归):"<<endl; t1.inOrder0(t1.root); cout<<endl<<"中序遍历(非递归):"<<endl; t1.inOrder0(t1.root); cout<<endl<<"后序遍历(递归):"<<endl; t1.postOrder0(t1.root); cout<<endl<<"后序遍历(非递归):"<<endl; t1.postOrder0(t1.root); cout<<endl; return 0; }
|