// why *&, because we want to change pLastNodeInList void convertNode(BSTreeNode* pNode, BSTreeNode*& pLastNodeInList) { // if pNode == NULL BSTreeNode *pCurrent = pNode; // Handle `L` in LVR // ... convert left sub-tree // this is where we `LVR` the tree if(pCurrent->m_pLeft != NULL) convertNode(pCurrent->m_pLeft, pLastNodeInList); //// ... pCurrent->m_pLeft = pLastNodeInList; if(pLastNodeInList != NULL) { pLastNodeInList->m_pRight = pCurrent; } // Handle `V` in LVR // `pLastNodeInList` becomes `V` for the right sub-tree pLastNodeInList = pCurrent; // Handle `R` in LVR if(pCurrent->m_pRight != NULL) { convertNode(pCurrent->m_pRight, pLastNodeInList); } }
void convertNode(BinaryTreeNode* node, BinaryTreeNode** lastNodeInList) { if (node == NULL) { return ; } BinaryTreeNode* current = node; if (current->left != NULL) { convertNode(current->left, lastNodeInList); } current->left = *lastNodeInList; // lastNodeList是当前节点的上一个节点 if (*lastNodeInList != NULL) { (*lastNodeInList)->right = current; } *lastNodeInList = current; // 一直在移动,遍历二叉树! if (current->right != NULL) { convertNode(current->right, lastNodeInList); } }
//用中序遍历的方法得到排序的序列 void convertNode(BinaryTreeNode* pRoot, BinaryTreeNode** pLastNode) { if(pRoot == NULL) { return; } //遍历左子树,遍历完后返回的pLastNode中已经是一个有序的链表 if(pRoot->m_pLeft != NULL) { convertNode(pRoot->m_pLeft, pLastNode); } pRoot->m_pLeft = *pLastNode; if(*pLastNode != NULL) { (*pLastNode)->m_pRight = pRoot; } *pLastNode = pRoot; if(pRoot->m_pRight != NULL) { convertNode(pRoot->m_pRight, pLastNode); } }
bool convertNode(OS_NAMESPACE_NAME::shared_ptr<OS_NAMESPACE_NAME::XMLNode> node, DOMElement *element) { if(element == nullptr) return false; OS_NAMESPACE_NAME::shared_ptr<OS_NAMESPACE_NAME::XMLAttributes> attributes = node->getAttributes(); for(OS_NAMESPACE_NAME::XMLAttributes::const_iterator a = attributes->begin(); a != attributes->end(); ++a) { element->setAttribute((*a)->getName().c_str(), (*a)->getValue().c_str()); } if(node->hasData()) { element->appendChild(element->getOwnerDocument()->createTextNode(node->getData().c_str())); } OS_NAMESPACE_NAME::shared_ptr<OS_NAMESPACE_NAME::XMLNodes> nodes = node->getNodes(); for(OS_NAMESPACE_NAME::XMLNodes::const_iterator i = nodes->begin(); i != nodes->end(); ++i) { DOMElement *child_element = element->getOwnerDocument()->createElement((*i)->getName().c_str()); if(child_element == nullptr) return false; element->appendChild(child_element); if(convertNode(*i, child_element) == false) return false; } return true; }
static JSON::Value parse(yaml_parser_t &parser) { JSON::Value result; yaml_document_t document; if (!yaml_parser_load(&parser, &document)) { yaml_error_type_t error = parser.error; MORDOR_ASSERT(error != YAML_NO_ERROR); Exception exception(parser.problem, parser.context); yaml_parser_delete(&parser); switch (error) { case YAML_MEMORY_ERROR: MORDOR_THROW_EXCEPTION(std::bad_alloc()); case YAML_READER_ERROR: MORDOR_THROW_EXCEPTION(InvalidUnicodeException()); default: MORDOR_THROW_EXCEPTION(exception); } } try { convertNode(result, yaml_document_get_root_node(&document), document); yaml_document_delete(&document); yaml_parser_delete(&parser); return result; } catch (...) { yaml_document_delete(&document); yaml_parser_delete(&parser); throw; } }
static void convertNode(JSON::Value &value, yaml_node_t *node, yaml_document_t &document) { switch (node->type) { case YAML_SCALAR_NODE: value = std::string((char *)node->data.scalar.value, node->data.scalar.length); break; case YAML_SEQUENCE_NODE: { value = JSON::Array(); JSON::Array &array = value.get<JSON::Array>(); yaml_node_item_t *item = node->data.sequence.items.start; array.resize(node->data.sequence.items.top - item); JSON::Array::iterator it = array.begin(); while (item < node->data.sequence.items.top) { convertNode(*it, yaml_document_get_node(&document, *item), document); ++it; ++item; } break; } case YAML_MAPPING_NODE: { value = JSON::Object(); JSON::Object &object = value.get<JSON::Object>(); yaml_node_pair_t *pair = node->data.mapping.pairs.start; while (pair < node->data.mapping.pairs.top) { yaml_node_t *keyNode = yaml_document_get_node(&document, pair->key); yaml_node_t *valueNode = yaml_document_get_node(&document, pair->value); if (keyNode->type != YAML_SCALAR_NODE) MORDOR_THROW_EXCEPTION(std::runtime_error("Can't use a non-string as a key")); std::string key((char *)keyNode->data.scalar.value, keyNode->data.scalar.length); convertNode(object.insert(std::make_pair(key, JSON::Value()))->second, valueNode, document); ++pair; } break; } default: MORDOR_NOTREACHED(); } }
BSTreeNode* convert(BSTreeNode* pHeadOfTree) { BSTreeNode *pLastNodeInList = NULL; convertNode(pHeadOfTree, pLastNodeInList); BSTreeNode *pHeadOfList = pLastNodeInList; while(pHeadOfList && pHeadOfList->m_pLeft) { pHeadOfList = pHeadOfList->m_pLeft; } return pHeadOfList; }
bool convertDocument(const OS_NAMESPACE_NAME::XMLDocument &document, DOMDocument *doc) { OS_NAMESPACE_NAME::shared_ptr<OS_NAMESPACE_NAME::XMLNode> rootNode = document.getRoot(); if(rootNode == nullptr) return false; DOMElement *root_element = doc->createElement(rootNode->getName().c_str()); if(root_element == nullptr) return false; doc->appendChild(root_element); return convertNode(rootNode, root_element); }
BSTreeNode* convertNode(BSTreeNode *pNode, bool asRight) { // 6. if it's an empty tree if(!pNode) return NULL; BSTreeNode *pLeft = NULL; BSTreeNode *pRight = NULL; // 2. if we have left sub-tree if(pNode->m_pLeft) pLeft = convertNode(pNode->m_pLeft, false); if(pLeft) { pLeft->m_pRight = pNode; pNode->m_pLeft = pLeft; } // 3. if we have right sub-tree if(pNode->m_pRight) pRight = convertNode(pNode->m_pRight, true); if(pRight) { pNode->m_pRight = pRight; pRight->m_pLeft = pNode; } // 4. returns right-left node (or current node) // 5. returns left-right node (or current node) BSTreeNode *pTemp = pNode; if(asRight){ while(pTemp->m_pLeft){ pTemp = pTemp->m_pLeft; } } else { while(pTemp->m_pRight){ pTemp = pTemp->pRight; } } return pTemp; }
/******************* 参考代码 **/ BinaryTreeNode* Convert(BinaryTreeNode* rootOfTree) { BinaryTreeNode* lastNodeInList = NULL; convertNode(rootOfTree, &lastNodeInList); // lastNodeInList 指向双向链表的尾节点 // 返回头结点 BinaryTreeNode* headOfList = lastNodeInList; while (headOfList != NULL && headOfList->left != NULL) { headOfList = headOfList->left; } return headOfList; }
BinaryTreeNode* convertBSTToList(BinaryTreeNode* pRoot) { if(pRoot == NULL) { return NULL; } BinaryTreeNode* pOrderedList = NULL; convertNode(pRoot, &pOrderedList); while(pOrderedList != NULL && pOrderedList->m_pLeft != NULL) { pOrderedList = pOrderedList->m_pLeft; } return pOrderedList; }
// implementation // =================================================== // (1) solution 1 // 从根结点开始,将左子树转化成排序的左子链表 // 将右子树转化成排序的右子链表 // 链接左右子链表 // 左子树的最右(or current node) -> 当前结点 -> 右子树的最左 (or current node) BSTreeNode* convert(BSTreeNode* pHeadOfTree) { return convertNode(pHeadOfTree, true); }
void AssetImporter::performConversion(const aiScene* scene) { convertNode(scene, scene->mRootNode, scene->mRootNode->mTransformation); }
static void serialiseNode(raptor_serializer* rdf_serializer, QMap< Node*, int >& nodeIDs, Node* node_, bool auth = false) { raptor_statement statement = {0}; if (!auth) { QPair< const void*, raptor_identifier_type > pair; // Serialize node type pair = convertNode(nodeIDs, node_); statement.subject = pair.first; statement.subject_type = pair.second; QString encoded = encodeUnicode(rdf.type->attributes.get(UtopiaSystem.uri).toString()); statement.predicate = raptor_new_uri((const unsigned char*) encoded.toAscii().data()); statement.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; pair = convertNode(nodeIDs, node_->type()); statement.object = pair.first; statement.object_type = pair.second; raptor_serialize_statement(rdf_serializer, &statement); // Serialize attributes QList< Node* > attributes = node_->attributes.keys(); QListIterator< Node* > attributeIterator(attributes); while (attributeIterator.hasNext()) { Node* predicateNode = attributeIterator.next(); QPair< const void*, raptor_identifier_type > pair = convertNode(nodeIDs, predicateNode); statement.predicate = pair.first; statement.predicate_type = pair.second; QString encoded = encodeUnicode(node_->attributes.get(predicateNode).toString()); statement.object = strdup(encoded.toStdString().c_str()); statement.object_type = RAPTOR_IDENTIFIER_TYPE_LITERAL; const char* uri = 0; switch (node_->attributes.get(predicateNode).type()) { case QVariant::Int: case QVariant::UInt: case QVariant::LongLong: case QVariant::ULongLong: uri = "http://www.w3.org/2001/XMLSchema#" "integer"; break; case QVariant::Double: uri = "http://www.w3.org/2001/XMLSchema#" "double"; break; case QVariant::Bool: uri = "http://www.w3.org/2001/XMLSchema#" "boolean"; break; case QVariant::ByteArray: uri = "http://www.w3.org/2001/XMLSchema#" "base64Binary"; break; case QVariant::Date: case QVariant::Time: case QVariant::DateTime: uri = "http://www.w3.org/2001/XMLSchema#" "dateTime"; break; case QVariant::StringList: uri = "http://www.w3.org/2001/XMLSchema#" "string*"; break; case QVariant::String: case QVariant::Url: default: uri = "http://www.w3.org/2001/XMLSchema#" "string"; break; } statement.object_literal_datatype = raptor_new_uri((const unsigned char*) uri); raptor_serialize_statement(rdf_serializer, &statement); } } if (node_->minions()) { List::iterator iter = node_->minions()->begin(); List::iterator end = node_->minions()->end(); for (; iter != end; ++iter) { serialiseNode(rdf_serializer, nodeIDs, *iter); QPair< const void*, raptor_identifier_type > pair = convertNode(nodeIDs, *iter); statement.subject = pair.first; statement.subject_type = pair.second; QList< Property > properties = (*iter)->relations(); QMutableListIterator< Property > property(properties); while (property.hasNext()) { Property prop = property.next(); Node* node = prop.data(); QPair< const void*, raptor_identifier_type > pair = convertNode(nodeIDs, node); statement.predicate = pair.first; statement.predicate_type = pair.second; Node::relation::iterator rel_iter = (*iter)->relations(prop).begin(); Node::relation::iterator rel_end = (*iter)->relations(prop).end(); for (; rel_iter != rel_end; ++rel_iter) { QPair< const void*, raptor_identifier_type > pair = convertNode(nodeIDs, *rel_iter); statement.object = pair.first; statement.object_type = pair.second; raptor_serialize_statement(rdf_serializer, &statement); } } } } }