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


C++ QuadTree::plot方法代码示例

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


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

示例1: simple_test

void simple_test() {

  std::cout << "Beginning simple_test()..." << std::endl;


  // --------------------------------------------------------
  // a collection of 21 points that make a nice sample tree
  std::vector< std::pair<Point<int>,char> > simple_points;
  simple_points.push_back(std::make_pair(Point<int>(20,10), 'A'));
  simple_points.push_back(std::make_pair(Point<int>(10,5), 'B'));
  simple_points.push_back(std::make_pair(Point<int>(30,4), 'C'));
  simple_points.push_back(std::make_pair(Point<int>(11,15), 'D'));
  simple_points.push_back(std::make_pair(Point<int>(31,16), 'E'));
  simple_points.push_back(std::make_pair(Point<int>(5,3), 'F'));
  simple_points.push_back(std::make_pair(Point<int>(15,2), 'G'));
  simple_points.push_back(std::make_pair(Point<int>(4,7), 'H'));
  simple_points.push_back(std::make_pair(Point<int>(14,8), 'I'));
  simple_points.push_back(std::make_pair(Point<int>(25,1), 'J'));
  simple_points.push_back(std::make_pair(Point<int>(35,2), 'K'));
  simple_points.push_back(std::make_pair(Point<int>(26,7), 'L'));
  simple_points.push_back(std::make_pair(Point<int>(36,6), 'M'));
  simple_points.push_back(std::make_pair(Point<int>(3,13), 'N'));
  simple_points.push_back(std::make_pair(Point<int>(16,12), 'O'));
  simple_points.push_back(std::make_pair(Point<int>(4,17), 'P'));
  simple_points.push_back(std::make_pair(Point<int>(15,18), 'Q'));
  simple_points.push_back(std::make_pair(Point<int>(25,13), 'R'));
  simple_points.push_back(std::make_pair(Point<int>(37,14), 'S'));
  simple_points.push_back(std::make_pair(Point<int>(24,19), 'T'));
  simple_points.push_back(std::make_pair(Point<int>(36,18), 'U'));


  // --------------------------------------------------------
  // the quad tree data structure starts out empty
  QuadTree<int,char> simple;
  assert (simple.size() == 0);
  // an empty tree has height == -1
  assert (simple.height() == -1); 
  // plot the structure with with these dimensions (width=40,height=20)
  std::cout << "\nan empty tree:" << std::endl;
  simple.plot(40,20);

  

  // --------------------------------------------------------
  for (int i = 0; i < simple_points.size(); i++) {

    // add each point from the collection
    //cout << "hello"<< endl;
    simple.insert(simple_points[i].first,simple_points[i].second);
    // verify the size (total # of points in the tree)
     //cout << "hello"<< endl;
    assert (simple.size() == i+1);

    // a few some specific checks along the way
    if (i == 0) { 
      std::cout << "\nafter inserting first data point:" << std::endl;
      simple.plot(40,20);
      // a tree with 1 node has height == 0
      //cout << simple.height() << endl;
      assert (simple.height() == 0); 
      // check that the newly inserted element can be found
      //cout << "hello"<< endl;
      QuadTree<int,char>::iterator itr = simple.find(20,10);
      //cout << itr.getLabel() << endl;
      //cout << "hello"<< endl;
      assert (itr != simple.end());
      // read the label & coordinates from the iterator
      assert (itr.getLabel() == 'A');
      // dereference the iterator to get the point
      const Point<int> &pt = *itr;
      assert (pt.x == 20);
      assert (pt.y == 10);
      //cout << "hello"<< endl;
    } else if (i <= 4) {
      std::cout << "\nafter inserting " << i+1 << " data points:" << std::endl;
      simple.plot(40,20);
      // the next 4 additions for this simple all happen at the
      // second level, tree has height = 1
      assert (simple.height() == 1);
    } else if (i == 8) {
      std::cout << "\nafter inserting " << i+1 << " data points:" << std::endl;
      simple.plot(40,20);
      assert (simple.height() == 2);
      // check for an element that exists
      QuadTree<int,char>::iterator itr = simple.find(4,7);
      assert (itr != simple.end());
      assert (itr.getLabel() == 'H');
      assert ((*itr).x == 4);
      assert ((*itr).y == 7);
      // check for a couple elements that aren't in the tree
      itr = simple.find(14,14);
      assert (itr == simple.end());
      itr = simple.find(15,18);
      assert (itr == simple.end());
      // another visualization of the tree structure
      // note: this is a pre-order traversal of the data (print the node, then recurse on each child)
      std::cout << "\na 'sideways' printing of the tree structure with 9 nodes:" << std::endl;
      simple.print_sideways();
    }
  }
//.........这里部分代码省略.........
开发者ID:rasmul2,项目名称:Data_Structures,代码行数:101,代码来源:main.cpp


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