⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soln9_4.cpp

📁 Wrox.Ivor.Hortons.Beginning.Visual.C.Plus.Plus.2008 With sourcecode
💻 CPP
字号:
// Soln 9_4.cpp
// The rand_s() function generates random integers from 0 to UINT_MAX
// To use rand_s function you must first define _CRT_RAND_S

#define _CRT_RAND_S             // For secure random number generator
#include <iostream>
#include <iomanip>
#include <cstdlib>              // For rand_s function

using std::cout;
using std::endl;
using std::setw;

class CBinaryTree
{
private:
  class CNode
  {
  public:
    // Node constructor
    CNode(int n) : value(n), m_pLeft(0), m_pRight(0) {}

    // List the node values in order
    void listNode()
    {

      if(m_pLeft != 0)                 // If there's a left node
        m_pLeft->listNode();           // Output its value

      cout << setw(12) << value;       // Output the current node value
      if(++listCount % 5 == 0)
        cout << endl;

      if(m_pRight != 0)                // If there's a right node
        m_pRight->listNode();          // Output its value
    }

    int value;                         // Value of the node
    CNode* m_pLeft;                    // Pointer to left node
    CNode* m_pRight;                   // Pointer to right node
    static int listCount;              // Count of number of output values
  };

public:
  CBinaryTree() : m_pRoot(0){}         // Constructor

  void add(int n);                     // Adds a value to the tree
  void add(int n, CNode* pNode);       // Adds n relative to pNode node

  // List the nodes in sequences
  void listNodes()
  {
    CNode::listCount = 0;
    if(m_pRoot == 0)
      cout << "Binary tree is empty." << endl;
    else 
      m_pRoot->listNode();
  }

private:
   CNode* m_pRoot;                     // Pointer to the roor node
};

int CBinaryTree::CNode::listCount = 0; // Initialize static member

// Add a value to the tree
void CBinaryTree::add(int n)
{
  if(m_pRoot == 0)                     // If there's no root node
    m_pRoot = new CNode(n);            // the new node is the root
  else                                 // otherwise
    add(n, m_pRoot);                   // add the node (recursive)
}

// Add a value relative to a given node
void CBinaryTree::add(int n, CNode* pNode)
{
  if(n == pNode->value)                // If value equals current node
  { // Always add equal values as left node
    CNode* pNewNode = new CNode(n);    // Create the new node for the value
    CNode* pTemp = pNode->m_pLeft;     // Save left ptr for current node
    pNode->m_pLeft = pNewNode;         // then make it point to new node
    pNewNode->m_pLeft = pTemp;         // Left ptr for new node point to old left node
  }
  else if(n > pNode->value)            // If new value is greater than right node value
  {                                    // it must go to the right
    if(pNode->m_pRight == 0)           // so if there's no right node
      pNode->m_pRight = new CNode(n);  // make the new node the right node
    else                               // Otherwise
      add(n, pNode->m_pRight);         // add it relative to right node(recursive)
  }
  else                                 // New number is less than current node value
  {
    if(pNode->m_pLeft == 0)            // so if there's no left node
      pNode->m_pLeft = new CNode(n);   // make the new node the left node
    else                               // Otherwise
      add(n, pNode->m_pLeft);          // add it relative to the left node(recursive)
  }
}


int main()
{
  CBinaryTree tree;                    // Create a binary tree
  unsigned int number = 0;             // Stores a number to be added to the tree

  cout << "Random value inserted in the tree are:" << endl;

  // Add 100 random number to the tree in the range from 1 to 1,000,000
  double max = 1000000.0;
  for(int i = 0 ; i<100 ; i++)
  {
    if(rand_s(&number) != 0)
      cout << "Random number generator failed!" << endl;

    // Change the range from (0 to UINTMAX) to (1 to max)
    number = static_cast<unsigned int>(static_cast<double>(number)/UINT_MAX*max)+1;

    tree.add(number);
    cout << setw(12) << number;
    if((i+1) % 5 == 0)
      cout << endl;
  }
  cout << endl << "Output from the tree is:" << endl;
  tree.listNodes();

   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -