c++中树的操作

转载 2007年09月29日 00:48:00
#include <iostream>
#include <string>

struct TreeNode {
        // An object of type TreeNode represents one node
        // in a binary tree of strings.
   string item;       // The data in this node.
   TreeNode *left;    // Pointer to left subtree.
   TreeNode *right;   // Pointer to right subtree.
   TreeNode(string str = "") {
          // Constructor, defined for convenience.
          // Make a node containing the specified string.
      item = str;
      left = NULL;
      right = NULL;
   }
};  // end struct TreeNode


void treeInsert(TreeNode *&root, string newItem) {
       // Add the item to the binary sort tree to which the parameter
       // "root" refers.  Note that root is passed by reference since
       // its value can change in the case where the tree is empty.
   if ( root == NULL ) {
          // The tree is empty.  Set root to point to a new node containing
          // the new item.  This becomes the only node in the tree.
      root = new TreeNode( newItem );
      return;
   }
   else if ( newItem < root->item ) {
      treeInsert( root->left, newItem );
   }
   else {
      treeInsert( root->right, newItem );
   }
}  // end treeInsert()


bool treeContains( TreeNode *root, string item ) {
       // Return true if item is one of the items in the binary
       // sort tree to which root points.   Return false if not.
   if ( root == NULL ) {
         // Tree is empty, so it certainly doesn't contain item.
      return false;
   }
   else if ( item == root->item ) {
         // Yes, the item has been found in the root node.
      return true;
   }
   else if ( item < root->item ) {
         // If the item occurs, it must be in the left subtree.
      return treeContains( root->left, item );
   }
   else {
         // If the item occurs, it must be in the right subtree.
      return treeContains( root->right, item );
   }
}  // end treeContains()


void treeList(TreeNode *node) {
       // Print the items in the tree in postorder, one item
       // to a line.  Since the tree is a sort tree, the output
       // will be in increasing order.
   if ( node != NULL ) {
       treeList(node->left);           // Print items in left subtree.
       cout << "  " << node->item << endl;  // Print item in the node.
       treeList(node->right);          // Print items in the right subtree.
   }
} // end treeList()


int countNodes(TreeNode *node) {
      // Count the nodes in the binary tree to which node
      // points.  Return the answer.
   if ( node == NULL ) {
           // Tree is empty, so it contains no nodes.
      return 0;
   }
   else {
          // Add up the root node and the nodes in its two subtrees.
      int leftCount = countNodes( node->left );
      int rightCount = countNodes( node->right );
      return  1 + leftCount + rightCount; 
   }
} // end countNodes()


int main() {

   TreeNode *root;  // Pointer to the root node in a binary tree.  This
                    // tree is used in this program as a binary sort tree.
                    // The tree is not allowed to contain duplicate
                    // items.  When the tree is empty, root is null.

   root = NULL;     // Start with an empty tree.

   cout << "This programs stores strings that you enter in a binary sort/n";
   cout << "tree.  After each items is inserted, the contents of the tree/n";
   cout << "are displayed.  The number of nodes in the tree is also output./n";
   cout << "    Any string you enter will be converted to lower case./n";
   cout << "Duplicate entries are ignored./n";

   while (true) {
           // Get one string from the user, insert it into the tree,
           // and print some information about the tree.  Exit if the
           // user enters an empty string.  Note that all strings are
           // converted to lower case.
       cout << ("/n/nEnter a string to be inserted, or press return to end./n");
       cout << ("?  ");
       string item;  // The user's input.
       if (cin.peek() == '/n')
          break;
       cin >> item;
       cin.ignore(10000,'/n');
       if (treeContains(root,item)) {
              // Don't insert a second copy of an item that is already
              // in the tree.
          cout << "/nThat item is already in the tree./n";
       }
       else {
          treeInsert(root,item);  // Add user's string to the tree.
          cout << "/nThe tree contains " << countNodes(root) << " items./n";
          cout << "/nContents of tree:/n/n";
          treeList(root);
       }
   }  // end while

   cout << "/n/nExiting program./n/n";

}  // end main()


 

二叉树的基本操作 C++代码实现

首先定义节点typedef struct BTree { int value; struct BTree *lchild; struct BTree *rchild; }...

(二叉树)谈一谈各类算法和数据结构的c++实现以及相关操作的复杂度(二)

接着上一篇, 上一篇主要说了各种排序算法, 但对几个常用的数据结构还未提及,所以这一篇主要讲二叉树, 二叉树已经包括很多链表的知识了。 二叉树这里不举太多数字方面的东西, 我们直接看图, 直观感性的认...
  • nosix
  • nosix
  • 2017年08月05日 01:18
  • 106

C++二叉树基本操作

  • 2012年07月03日 21:57
  • 8KB
  • 下载

c++二叉树的基本操作

  • 2015年11月25日 22:15
  • 3KB
  • 下载

二叉树的基本操作及编程题总结(C++)

**二叉树编程题万变不离其宗在于对递归的理解和使用要弄懂用好递归 重要的在于一下几条:1.搞清楚函数递归调用栈帧的变化 特别是二叉递归时的栈帧变化2.搞清楚各个函数参数 传值和传引用 的函数参数在递...

红黑树的插入操作c++实现

  • 2017年12月03日 10:57
  • 5KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:c++中树的操作
举报原因:
原因补充:

(最多只允许输入30个字)