long long BinaryTree::nodeCounts() { std::queue<BinaryNode*> collections; // Find first leaf and push it to stack collections.push(root_); long long count = 1; // Iterate the cell from root to its childrens to destory // binary tree BinaryNode* cell = NULL; while (!collections.empty()) { cell = collections.front(); collections.pop(); // Push the children to stack if (!cell->isLeaf()) { BinaryNode *leftChild = cell->leftChild(); BinaryNode *rightChild = cell->rightChild(); if (leftChild != NULL) collections.push(leftChild); if (rightChild != NULL) collections.push(rightChild); } // Count the current node count++; } return count; }
void BinaryTree::deallocate() { std::queue<BinaryNode*> del_collections; // Find first leaf and push it to stack del_collections.push(root_); BinaryNode* cell = NULL; // Iterate the cell from root to its childrens to destory // binary tree while (!del_collections.empty()) { cell = del_collections.front(); del_collections.pop(); // Push the children to stack if (!cell->isLeaf()) { BinaryNode *leftChild = cell->leftChild(); BinaryNode *rightChild = cell->rightChild(); if (leftChild != NULL) del_collections.push(leftChild); if (rightChild != NULL) del_collections.push(rightChild); } // Delete current octree cell delete cell; cell = NULL; } this->root_ = NULL; }
bool Container::unserializeItemNode_OTMM(const IOMap& maphandle, BinaryNode* node) { bool ret = Item::unserializeAttributes_OTMM(maphandle, node); if(ret) { BinaryNode* child = node->getChild(); if(child) do { uint8_t type; if(!child->getByte(type)) { return false; } //load container items if(type == OTMM_ITEM) { Item* item = Item::Create_OTMM(maphandle, child); if(!item) { return false; } if(!item->unserializeItemNode_OTMM(maphandle, child)) { delete item; return false; } contents.push_back(item); } else { // corrupted file data! return false; } } while(child->advance()); return true; } return false; }
BinaryNode<T> *BST<T>::find_max(BinaryNode<T> *node) { // Keep going down rhs until rhs == NULL. Then return the node BinaryNode<T> *tmp = node; while (tmp->get_rhs() != NULL){ tmp = tmp->get_rhs(); } return tmp; // THIS IS ALL COMMENTED OUT TEMPORARILY BECAUSE OF ISSUES WITH ACCESSING THE NODES IN TREE // if (root != NULL){ // BinaryNode<T> * entire_fucking_tree; // entire_fucking_tree = root; // root = NULL; // return entire_fucking_tree; // } // // TODO: Implement this function throw 1; // TODO: Implement this function (when you have a use for it) return NULL; }
BinaryNode<ItemType>* BinaryNodeTree<ItemType>::moveValuesUpTree(BinaryNode<ItemType>* subTreePtr) { BinaryNode<ItemType>* leftPtr = subTreePtr->getLeftChildPtr(); BinaryNode<ItemType>* rightPtr = subTreePtr->getRightChildPtr(); int leftHeight = getHeightHelper(leftPtr); int rightHeight = getHeightHelper(rightPtr); if (leftHeight > rightHeight) { subTreePtr->setItem(leftPtr->getItem()); leftPtr = moveValuesUpTree(leftPtr); subTreePtr->setLeftChildPtr(leftPtr); return subTreePtr; } else { if (rightPtr != nullptr) { subTreePtr->setItem(rightPtr->getItem()); rightPtr =moveValuesUpTree(rightPtr); subTreePtr->setRightChildPtr(rightPtr); return subTreePtr; } else { //this was a leaf! // value not important delete subTreePtr; return nullptr; } // end if } // end if } // end moveValuesUpTree
BinaryNode<ItemType>* copyTree(const BinaryNode<ItemType>* treePtr) const { BinaryNode<ItemType>* newTreePtr = nullptr; if (treePtr != nullptr) { newTreePtr = new BinaryNode<ItemType>(treePtr->getItem(), nullptr, nullptr); newTreePtr->setLeftChildPtr(copyTree(treePtr->getLeftChildPtr())); newTreePtr->setRightChildPtr(copyTree(treePtr->getRightChildPtr())); } return newTreePtr; }
ItemType BinaryNodeTree<ItemType>::getEntry(const ItemType& anEntry) const throw(NotFoundException) { bool isSuccessful = false; BinaryNode<ItemType>* binaryNodePtr = findNode(rootPtr, anEntry, isSuccessful); if (isSuccessful) return binaryNodePtr->getItem(); else throw NotFoundException("Entry not found in tree!"); } // end getEntry
double Division::eval() const { BinaryNode* p; p = dynamic_cast<BinaryNode*>(owner_); if (p == 0) { std::cerr << "this cannot happen" << std::endl; exit(1); } return p->getLeft() / p->getRight(); }
BinaryNode<ItemType>* BinaryNodeTree<ItemType>::copyTree(const BinaryNode<ItemType>* treePtr) const { BinaryNode<ItemType>* newTreePtr = nullptr; // Copy tree nodes during a preorder traversal if (treePtr != nullptr) { // Copy node newTreePtr = new BinaryNode<ItemType>(treePtr->getItem(), nullptr, nullptr); newTreePtr->setLeftChildPtr(copyTree(treePtr->getLeftChildPtr())); newTreePtr->setRightChildPtr(copyTree(treePtr->getRightChildPtr())); } // end if return newTreePtr; } // end copyTree
BinaryNode* BinaryTree::copyTree(const BinaryNode* nodePtr) { if (nodePtr == 0) { return 0; } else { // make a new node BinaryNode* newNode = new BinaryNode(nodePtr->getWebsite()); newNode->setLeftPtr(copyTree(nodePtr->getLeftPtr())); newNode->setRightPtr(copyTree(nodePtr->getRightPtr())); return newNode; } }
void BinaryHeap::sort(){ BinaryNode* currentNode = last; // Sift the item up so long as it has a parent (not root) // and is greater than it's parent. while(currentNode->hasParent()){ if(*currentNode > *currentNode->parent){ int temp = currentNode->data; currentNode->setData(currentNode->parent->data); currentNode->parent->setData(temp); currentNode = currentNode->parent; } else{ currentNode = root; } } }
int BinaryNode::substr(BinaryNode& b, qore_offset_t offset) const { printd(5, "BinaryNode::substr(offset: "QSD") this: %p len: "QSD")\n", offset, this, len); checkOffset(offset); if (offset == (qore_offset_t)len) return -1; b.append((char*)ptr + offset, len - offset); return 0; }
void BinaryHeap::setLast(){ if(!last){ BinaryNode* currentNode = root; // While our node has children we will check // to see if it has a right branch. If it doesn't // we will set to the left (which it must have). while(currentNode->hasChildren()){ if(currentNode->hasRightChild()){ currentNode = currentNode->rightChild; } else{ currentNode = currentNode->leftChild; } } // We've reached a node with no children, our last element. last = currentNode; } }
BST<T>::~BST() { Queue<BinaryNode<T>* > jerry; if (root == NULL) get_the_fuck_outta_here; jerry.enqueue(root); while (!jerry.is_empty()){ BinaryNode<T>* tmp = jerry.dequeue(); if (tmp->get_lhs() != NULL) jerry.enqueue(tmp->get_lhs()); if (tmp->get_rhs() != NULL) jerry.enqueue(tmp->get_rhs()); delete tmp; } // TODO: Write this function // TODO: Copy your lab7 implementation here }
ClientVersionID IOMapOTMM::getVersionInfo(const FileName& filename) { wxString wpath = filename.GetFullPath(); DiskNodeFileReadHandle f((const char*)wpath.mb_str(wxConvUTF8)); if(f.isOk() == false) { return CLIENT_VERSION_NONE; } BinaryNode* root = f.getRootNode(); if(!root) {return CLIENT_VERSION_NONE;} root->skip(1); // Skip the type byte uint16_t u16; uint32_t u32; if(!root->getU32(u32) || u32 != 1) { // Version return CLIENT_VERSION_NONE; } root->getU16(u16); root->getU16(u16); root->getU32(u32); if(root->getU32(u32)) { // OTB minor version return ClientVersionID(u32); } return CLIENT_VERSION_NONE; }
void BST::deleteMin() { if (rootPtr->getLeftChildPtr()==nullptr && rootPtr->getRightChildPtr()!=nullptr) { BinaryNode* toDelete = rootPtr-> getRightChildPtr(); rootPtr = rootPtr-> getRightChildPtr(); delete toDelete; } else if (rootPtr->getLeftChildPtr()==nullptr) { delete rootPtr; } else { BinaryNode* parent = rootPtr; BinaryNode* child = rootPtr->getLeftChildPtr(); while (child->getLeftChildPtr()!=nullptr) { child = child->getLeftChildPtr(); parent = parent->getLeftChildPtr(); } delete child; parent->setLeftChildPtr(nullptr); } }
int BinaryNode::substr(BinaryNode& b, qore_offset_t offset, qore_offset_t length) const { printd(5, "BinaryNode::substr(offset: "QSD", length: "QSD") this: %p len: "QSD"\n", offset, length, this, len); checkOffset(offset, length); if (offset == (qore_offset_t)len) return -1; if (length > (qore_offset_t)(len - offset)) length = len - offset; b.append((char*)ptr + offset, length); return 0; }
T BST<T>::remove_root() { if (root == NULL) throw 3; //no root T retData = root->get_data(); T maxData; BinaryNode<T>* tmpNode; if (root->get_lhs() == NULL){ BinaryNode<T> *tmpRoot = root; root = root->get_rhs(); delete tmpRoot; return retData; } tmpNode = find_max(root->get_lhs()); maxData = tmpNode->get_data(); if(remove(maxData) != maxData) throw 2; //removed the wrong data root->set_data(maxData); return retData; throw 1; }
void LiveSocket::receiveFloor(NetworkMessage& message, Editor& editor, Action* action, int32_t ndx, int32_t ndy, int32_t z, QTreeNode* node, Floor* floor) { Map& map = editor.map; uint16_t tileBits = message.read<uint16_t>(); if(tileBits == 0) { for(uint_fast8_t x = 0; x < 4; ++x) { for(uint_fast8_t y = 0; y < 4; ++y) { action->addChange(new Change(map.allocator(node->createTile(ndx * 4 + x, ndy * 4 + y, z)))); } } return; } // -1 on address since we skip the first START_NODE when sending const std::string& data = message.read<std::string>(); mapReader.assign(reinterpret_cast<const uint8_t*>(data.c_str() - 1), data.size()); BinaryNode* rootNode = mapReader.getRootNode(); BinaryNode* tileNode = rootNode->getChild(); Position position(0, 0, z); for(uint_fast8_t x = 0; x < 4; ++x) { for(uint_fast8_t y = 0; y < 4; ++y) { position.x = (ndx * 4) + x; position.y = (ndy * 4) + y; if(testFlags(tileBits, 1 << ((x * 4) + y))) { receiveTile(tileNode, editor, action, &position); tileNode->advance(); } else { action->addChange(new Change(map.allocator(node->createTile(position.x, position.y, z)))); } } } mapReader.close(); }
BinaryNode<ItemType>* moveValuesUpTree(BinaryNode<ItemType>* subTreePtr) { BinaryNode<ItemType>* leftPtr = subTreePtr->getLeftChildPtr(); BinaryNode<ItemType>* rightPtr = subTreePtr->getRightChildPtr(); int leftHeight = getHeightHelper(leftPtr); int rightHeight = getHeightHelper(rightPtr); if (leftHeight > rightHeight) { subTreePtr->setItem(leftPtr->getItem()); leftPtr = moveValuesUpTree(leftPtr); subTreePtr->setLeftChildPtr(leftPtr); return subTreePtr; } else { if (rightPtr != nullptr) { subTreePtr->setItem(rightPtr->getItem()); rightPtr =moveValuesUpTree(rightPtr); subTreePtr->setRightChildPtr(rightPtr); return subTreePtr; } else { delete subTreePtr; return nullptr; } } }
void LiveServer::OnReceiveChanges(LivePeer* connection, NetworkMessage* nmsg) { std::string data = nmsg->ReadString(); // -1 on address since we skip the first START_NODE when sending bn_reader.assign((uint8_t*)data.c_str() - 1, data.size()); BinaryNode* rootNode = bn_reader.getRootNode(); BinaryNode* tileNode = rootNode->getChild(); NetworkedAction* action = dynamic_cast<NetworkedAction*>(editor->actionQueue->createAction(ACTION_REMOTE)); action->owner = connection->GetClientID(); if (tileNode) do { Tile* t = ReadTile(tileNode, editor->map); if(!t) continue; action->addChange(newd Change(t)); } while (tileNode->advance()); bn_reader.close(); editor->actionQueue->addAction(action); gui.RefreshView(); }
BinaryNode<ItemType>* leftRotate(BinaryNode<ItemType>* x) { BinaryNode<ItemType>* y = x->getRightChildPtr(); BinaryNode<ItemType>* T2 = y->getLeftChildPtr(); y->setLeftChildPtr(x); x->setRightChildPtr(T2); x->setHeight(max(height(x->getLeftChildPtr()), height(x->getRightChildPtr()))+1); y->setHeight(max(height(y->getLeftChildPtr()), height(y->getRightChildPtr()))+1); return y; }
BinaryNode<ItemType>* rightRotate(BinaryNode<ItemType>* y) { BinaryNode<ItemType>* x = y->getLeftChildPtr(); BinaryNode<ItemType>* T2 = x->getRightChildPtr(); x->setRightChildPtr(y); y->setLeftChildPtr(T2); y->setHeight(max(height(y->getLeftChildPtr()), height(y->getRightChildPtr()))+1); x->setHeight(max(height(x->getLeftChildPtr()), height(x->getRightChildPtr()))+1); return x; }
int BinaryHeap::add(int data){ // Check if item already exists if(exists(data)){ return 0; } // Item doesn't exists, add to last BinaryNode* newNode; newNode = new BinaryNode(data); if(isEmpty()){ root = newNode; last = newNode; } else{ BinaryNode* currentNode = last; // If our current node is not a left child or not root, move to it's parent while(currentNode->isRightChild()){ currentNode = currentNode->parent; } // Get current sibling, or set node if no sibling if(currentNode->isLeftChild()){ if(currentNode->hasSibling()){ currentNode = currentNode->parent->rightChild; } else{ currentNode = currentNode->parent; currentNode->setRightChild(*newNode); last = newNode; sort(); return 1; } } // Move down the left side of the tree/subtree until we hit a leaf while(currentNode->hasLeftChild()){ currentNode = currentNode->leftChild; } currentNode->setLeftChild(*newNode); last = newNode; sort(); } return 1; }
void XmlRenderer::renderNode(const BinaryNode& node) { StringBuffer attrs; attrs << "op='" << xmlEncode(herschel::operatorName(node.op())) << "'"; if (fShowNodeType && node.type().isDef()) { attrs << " ty='" << xmlEncode(node.type().typeId()) << "'"; fReferencedTypes.insert(std::make_pair(node.type().typeId(), node.type())); } displayOpenTagAttrs("binary", StrHelper(attrs.toString())); displayNode(nullptr, node.left().get()); displayNode(nullptr, node.right().get()); displayCloseTag("binary"); }
ItemType getEntry(const ItemType& anEntry) const { BinaryNode<ItemType>* nodeWithEntry = findNode(rootPtr, anEntry); if (nodeWithEntry == nullptr) cout << "NotFoundException - Entry not found in tree.\n"; else return nodeWithEntry->getItem(); }
ItemType getRootData() const { if (isEmpty()) cout << "PrecondViolatedExcep - getRootData() called with empty tree.\n"; return rootPtr->getItem(); }
ItemType getEntry(const ItemType& anEntry) const { bool isSuccessful = false; BinaryNode<ItemType>* binaryNodePtr = findNode(rootPtr, anEntry, isSuccessful); if (isSuccessful) return binaryNodePtr->getItem(); else cout << "NotFoundException - Entry not found in tree!\n"; }
void setRootData(const ItemType& newData) { if (isEmpty()) rootPtr = new BinaryNode<ItemType>(newData, nullptr, nullptr); else rootPtr->setItem(newData); }
bool ItemDatabase::loadFromOtb(const FileName& datafile, wxString& error, wxArrayString& warnings) { std::string filename = nstr((datafile.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + datafile.GetFullName())); DiskNodeFileReadHandle f(filename, StringVector(1, "OTBI")); if(!f.isOk()) { error = wxT("Couldn't open file \"") + wxstr(filename) + wxT("\":") + wxstr(f.getErrorMessage()); return false; } BinaryNode* root = f.getRootNode(); #define safe_get(node, func, ...) do {\ if(!node->get##func(__VA_ARGS__)) {\ error = wxstr(f.getErrorMessage()); \ return false; \ } \ } while(false) // Read root flags root->skip(1); // Type info //uint32_t flags = root->skip(4); // Unused? uint8_t attr; safe_get(root, U8, attr); if(attr == ROOT_ATTR_VERSION) { uint16_t datalen; if(!root->getU16(datalen) || datalen != 4 + 4 + 4 + 1*128) { error = wxT("items.otb: Size of version header is invalid, updated .otb version?"); return false; } safe_get(root, U32, MajorVersion); // items otb format file version safe_get(root, U32, MinorVersion); // client version safe_get(root, U32, BuildNumber); // revision std::string csd; csd.resize(128); if(!root->getRAW((uint8_t*)csd.data(), 128)) { // CSDVersion ?? error = wxstr(f.getErrorMessage()); return false; } } else { error = wxT("Expected ROOT_ATTR_VERSION as first node of items.otb!"); } if(settings.getInteger(Config::CHECK_SIGNATURES)) { if(gui.GetCurrentVersion().getOTBVersion().format_version != MajorVersion) { error = wxT("Unsupported items.otb version (version ") + i2ws(MajorVersion) + wxT(")"); return false; } } BinaryNode* itemNode = root->getChild(); switch(MajorVersion) { case 1: return loadFromOtbVer1(itemNode, error, warnings); case 2: return loadFromOtbVer2(itemNode, error, warnings); case 3: return loadFromOtbVer3(itemNode, error, warnings); } return true; }