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
| #include<iostream> #include<queue> using namespace std; int allCount,counter=0; class BtreeNode{ public: char data;
BtreeNode *leftChild,*rightChild; int size; BtreeNode(){leftChild=rightChild=nullptr;size=0;} BtreeNode(char& theData,BtreeNode* left=nullptr,BtreeNode*right=nullptr){ data=theData; leftChild=left; rightChild=right; size=1; } }; int height(BtreeNode *node){ if(node == nullptr) return 0; int hl=height(node->leftChild); int hr=height(node->rightChild); if(hl>hr) { return++hl; } else { return ++hr; } }
void preOrder(BtreeNode* node){ if(node != nullptr){ if(counter != allCount - 1) cout << node->data << ","; else cout << node->data << endl; counter++; preOrder(node->leftChild); preOrder(node->rightChild); } } void inOrder(BtreeNode* node){ if(node != nullptr){ inOrder(node->leftChild); if(counter != allCount - 1) cout << node->data << ","; else cout << node->data << endl; counter++; inOrder(node->rightChild); } } void postOrder(BtreeNode* node){ if(node != nullptr){ postOrder(node->leftChild); postOrder(node->rightChild); if(counter != allCount - 1) cout << node->data << ","; else cout << node->data << endl; counter++; } } void levelOrder(BtreeNode* node){ queue<BtreeNode*> queue; while(node != nullptr){ if(counter != allCount - 1) cout << node->data << ","; else cout << node->data << endl; counter++; if(node->leftChild != nullptr){ queue.push(node->leftChild); } if(node->rightChild != nullptr){ queue.push(node->rightChild); } if(queue.empty()){ break; } node=queue.front(); queue.pop();
} }
void set(BtreeNode *current, int i, string s){ if((2*i+1)<=s.length()){ current->leftChild=new BtreeNode(s[2 * i - 1]); set(current->leftChild, 2 * i, s); current->rightChild=new BtreeNode(s[2 * i]); set(current->rightChild, 2 * i + 1, s); current->size+=(current->leftChild->size + current->rightChild->size); return; } else if(2*i==s.length()){ current->leftChild=new BtreeNode(s[2 * i - 1]); current->size+=current->leftChild->size; return; } else if(2*i>s.length()) return; } BtreeNode* rebuild(char preOrder[],char inOrder[],int pStart,int pEnd,int iStart,int iEnd){ BtreeNode* tree=new BtreeNode(preOrder[pStart]); if(pStart==pEnd&&iStart==iEnd){ return tree; } int root=0; for(root=iStart;root<=iEnd;root++){ if(preOrder[pStart]==inOrder[root]) { break; } }
int leftLength=root-iStart; int rightLength=iEnd-root; if(leftLength>0){ tree->leftChild=rebuild(preOrder,inOrder,pStart+1,pStart+leftLength,iStart,root-1);
} if(rightLength>0){ tree->rightChild=rebuild(preOrder,inOrder,pStart+leftLength+1,pEnd,root+1,iEnd);
} return tree; } int main(){ cout<<"Input1"<<endl; string s1,s2,s3,ss; cin>>s1; cout<<"Output1"<<endl; BtreeNode *r1=new BtreeNode(s1[0]); set(r1,1,s1); allCount=s1.length(); preOrder(r1);counter=0; inOrder(r1);counter=0; postOrder(r1);counter=0; cout<<r1->size<<endl; cout<<height(r1)<<endl; cout<<"Input2"<<endl; cin>>s2>>s3; char char1[s2.length()]; s2.copy(char1, s2.length(), 0); char char2[s3.length()]; s3.copy(char2, s3.length(), 0); cout<<"Output2"<<endl; BtreeNode *r2=rebuild(char1, char2, 0, s2.length() - 1, 0, s3.length() - 1); allCount=s2.length(); postOrder(r2);counter=0; levelOrder(r2); cout<<"End"<<endl; return 0; }
|