int main(int argc, const char * argv[]) { //测试树 cout<<"开始测试树\n"; Tree<char> t; t.InsertChild('A'); for (int i = 0; i < 3; i ++) { t.Root(); t.InsertChild('B'+i); } for (int i = 0; i < 2; i++) { t.Root(); t.FirstChild(); t.InsertChild('E'+i); } t.DisplayTree(); return 0; }
void Tree::MakeBalance(int remain) { if( remain == 0 ) return; if( children.size() == 0 ) { Tree * root = this; int label = root->label; for(int i = 0; i < remain; ++i ) { root->InsertChild(shared_ptr<Tree>(new Tree())); root->SetLabel(-1); root = root->children[0].get(); } root->SetLabel(label); } else { for(int i = 0; i < children.size(); ++i) children[i]->MakeBalance(remain-1); } }