int main() { BST T; T.InsertBST(8,"龜仙人"); T.InsertBST(1000,"悟空"); T.InsertBST(2,"克林"); T.InsertBST(513,"比克"); cout << "Inorder Traversal:\n"; T.InorderPrint(); cout << endl; cout << "Level-order Traversal:\n"; T.Levelorder(); cout << endl; TreeNode *node = T.Search(1000); if(node != NULL){ cout << "There is " << node->GetElement() << "(" << node->GetKey() << ")" << endl; } else { cout << "no element with Key(1000)" << endl; } node = T.Search(73); if(node != NULL){ cout << "There is " << node->GetElement() << "(" << node->GetKey() << ")" << endl; } else { cout << "no element with Key(73)" << endl; } return 0; }
QModelIndex model::DbContainerModel::AddElement(dbc::ElementType type, const QString& name, const QModelIndex& parent) { assert(!name.isEmpty() && type != dbc::ElementTypeUnknown && parent.isValid()); if (name.isEmpty() || type == dbc::ElementTypeUnknown || !parent.isValid()) { return QModelIndex(); } TreeNode* parentNode = Index2Node(parent); dbc::Folder* folder = parentNode->GetElement()->AsFolder(); assert(folder != nullptr); if (folder == nullptr) { return QModelIndex(); } LoadChildren(parent); dbc::ElementGuard element = folder->CreateChild(utils::QString2StdString(name), type); DBC_MODEL_TRY(tr("Element adding")); int insertedRow = parentNode->GetChildrenCount(); beginInsertRows(parent, insertedRow, insertedRow); TreeNode* insertedNode = parentNode->AddChild(element); assert(insertedRow + 1 == parentNode->GetChildrenCount()); endInsertRows(); return createIndex(insertedRow, 0, insertedNode); DBC_MODEL_CATCH; element->Remove(); return QModelIndex(); }
dbc::ElementGuard model::DbContainerModel::GetElementByIndex(const QModelIndex& index) { dbc::ElementGuard element; TreeNode* node = Index2Node(index); if (node != nullptr) { element = node->GetElement(); } return element; }
void model::DbContainerModel::SetNodeName(const QModelIndex& index, const QString& name) { DBC_MODEL_TRY(tr("Set element name")); TreeNode* node = Index2Node(index); assert(node != nullptr); node->GetElement()->Rename(utils::QString2StdString(name)); node->RefreshPath(); emit dataChanged(index, index); DBC_MODEL_CATCH; }
bool model::DbContainerModel::hasChildren(const QModelIndex& parent /*= QModelIndex()*/) const { TreeNode* node = Index2Node(parent); if (node == nullptr) // For the root { return !m_rootNodes.isEmpty(); } bool showExpand = node->GetElement()->Type() == dbc::ElementTypeFolder; if (node->wasLoaded && node->GetChildrenCount() == 0) { showExpand = false; } return showExpand; }
QVariant model::DbContainerModel::data(const QModelIndex& index, int role) const { TreeNode* node = Index2Node(index); if (node != nullptr) { switch (role) { case Qt::DisplayRole: case ItemNameRole: return GetDisplayData(node, index.row()); case ItemTypeRole: return QVariant::fromValue(node->GetElement()->Type()); case ItemSizeRole: return GetSizeData(node); default: return QVariant(); } } return QVariant(); }
void model::DbContainerModel::LoadChildren(const QModelIndex& parent) { TreeNode* node = Index2Node(parent); assert(node != nullptr); dbc::ElementGuard element = node->GetElement(); if (!node->wasLoaded && element->Type() == dbc::ElementTypeFolder) { dbc::Folder* folder = element->AsFolder(); assert(folder != nullptr); dbc::DbcElementsIterator iterator = folder->EnumFsEntries(); if (iterator->HasNext()) { beginInsertRows(parent, 0, iterator->Count() - 1); int row = 0; for (; iterator->HasNext(); ++row) { node->AddChild(iterator->Next()); } endInsertRows(); } } node->wasLoaded = true; emit dataChanged(parent, parent); }