[資料結構]實驗五_順序二叉樹
阿新 • • 發佈:2019-01-03
#include<iostream> using namespace std; class SeqBinaryList { public: //T init(); SeqBinaryList() {} //~SeqBinaryList(); void LevelOrder(); void leafPrint(); void allPrint(); private: char data[13] = { 'A','B','C',NULL,'D','E',NULL,NULL,NULL,'F',NULL,NULL,'G' }; }; void SeqBinaryList::LevelOrder() { for (int i = 0; i < 13; i++) { if (data[i] == NULL)continue; if (i == 0) { cout << 1; cout << data[i] << endl; continue; } if (i <= 3 - 1) { cout << 2; cout << data[i] << endl; continue; } if (i <= 7 - 1) { cout << 3; cout << data[i] << endl; continue; } else { cout << 4; cout << data[i] << endl; continue; } } } void SeqBinaryList::leafPrint() { for (int i = 7; i < 13; i++) { if (data[i] == NULL)continue; else { cout << 4; cout << data[i] << endl; continue; } } } void SeqBinaryList::allPrint() { cout << "根結點:" << data[0] << " " << "孩子結點:" << data[1] << " " << data[2] << endl; for (int i = 1; i < 7; i++) { if (data[i] == NULL)continue; cout << "結點:" << data[i] << " " << "孩子結點:" << data[2 * (i + 1) - 1] << " " << data[2 * (i + 1)] << " "; if ((i + 1) % 2 == 0) { cout << "雙親結點:" << data[(i + 1) / 2-1] << endl; continue; }//double if ((i + 1) % 2 != 0 && i != 0) { cout << "雙親結點:" << data[i / 2-1] << endl; continue; }// } for (int i = 7; i < 13; i++) { if (data[i] == NULL)continue; cout << "結點:" << data[i]; if ((i + 1) % 2 == 0) { cout << "雙親結點:" << data[(i + 1) / 2] << endl; continue; }//double if ((i + 1) % 2 != 0 && i != 0) { cout << "雙親結點:" << data[i / 2] << endl; continue; } } } int main() { SeqBinaryList a; a.LevelOrder(); a.SeqBinaryList::allPrint(); system("pause"); }