当前位置: 首页>>代码示例>>C++>>正文


C++ BSTNode::contents方法代码示例

本文整理汇总了C++中BSTNode::contents方法的典型用法代码示例。如果您正苦于以下问题:C++ BSTNode::contents方法的具体用法?C++ BSTNode::contents怎么用?C++ BSTNode::contents使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BSTNode的用法示例。


在下文中一共展示了BSTNode::contents方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Insert

/**
 * creates a new BSTNode, Inserts it into the tree, and returns true
 * if the integer is already in the tree, does not Insert, and returns false
 */
bool BSTree::Insert(int contents, BSTNode*& root)
{
    BSTNode* newNode = new BSTNode(contents);
    if (root == NULL)
    {
        root = newNode;
        size_ += 1;
        return true;
    } 
    else if (newNode->contents() < root->contents())
    {
        // if (root->left_child() == NULL)
        // {
        // root->set_left_child(newNode);
        // size_ += 1;
        // }
        // else
        Insert(contents, root->left_child());
    }
    else if (newNode->contents() > root->contents())
    {
        Insert(contents, root->right_child());
        
    }else
    return false;
}
开发者ID:Dminer001,项目名称:CSCI-21,代码行数:30,代码来源:bs_tree.cpp

示例2: Remove

 /*
   * traverses the tree and removes the node containing the target
   * integer if present and returns true
   * return false if target integer is not in tree (or the tree is empty)
   */
  bool BSTree::Remove(int content, BSTNode*& node)
  {
     if(node == NULL)
     {
       return false;
     }
     else 
     {
       if(node->contents() == content && size_ == 1)                            //checking if root needs to be removed
       {
         delete node;
         node = NULL;
         size_ = 0;
         return true;
       }
       else if(content < node->contents())                                       //if less than node traverse to the left
       {
         Remove(content, node->left_child());
       }
       else if(content > node->contents())                                       //if greater than node traverse to the right
       {
         Remove(content, node->right_child());
       }
       else                                                                     //if the node is equal to content
       {
         if(node->left_child() == NULL && node->right_child() == NULL)           //if the node to remove has no left/right child
         {
           delete node;
           node = NULL;
         }
         else if(node->left_child() == NULL)                                     //node to be removed has a right subtree
         {
           BSTNode* temp = node;
           node = node->right_child();
           delete temp;
         }
         else if(node->right_child() == NULL)                                    //node to be removed has a left subtree
         {
           BSTNode* temp = node;
           node = node->left_child();
           delete temp;
         }
         else if(root_->contents() == node->contents())
         {
            BSTNode* temp = new BSTNode(FindMin(node->left_child()));
            node->set_contents(temp->contents());
            delete temp;
            node->left_child() = NULL;
         }
         else
         {
           BSTNode* temp = new BSTNode(FindMin(node->right_child()));
           node->set_contents(temp->contents());
           delete temp;
         }
         size_--;
         return true;
       }  
     }
  }
开发者ID:agonzales004,项目名称:CSCI-21-SPRING-2016,代码行数:65,代码来源:bs_tree.cpp

示例3: SECTION

#include "catch.hpp"

// For NULL
#include <cstddef>
#include "bs_tree.h"
// To test for correct header guards
#include "bst_node.h"
#include "bst_node.h"
#include "bs_tree.h"

TEST_CASE("Default Constructor for BSTNode") {
  const BSTNode const_node;
  BSTNode node;
  BSTNode* p_node = &node;
  SECTION("Contents const Accessor") {
    CHECK(const_node.contents() == 0);
  }

  SECTION("Contents Accessor (Editable)") {
    node.contents() = 10;
    CHECK(node.contents() == 10);
  }

  SECTION("Left Child const Accessor") {
    CHECK(const_node.left_child() == NULL);
  }

  SECTION("Left Child Accessor (Editable)") {
    node.left_child() = &node;
    CHECK(node.left_child() == p_node);
  }
开发者ID:WillyTrip,项目名称:CSCI-21-Fall2015,代码行数:31,代码来源:lab_22_unit_test.cpp


注:本文中的BSTNode::contents方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。