Writing of the standard BST Binary Search Tree and the bst Binary Tree

Source: Internet
Author: User

Writing of the standard BST Binary Search Tree and the bst Binary Tree

I have been suffering from experiments on various data structures recently. Especially in the Code section, I have strict requirements on the data structure. For example, to write a BST, I need to divide it into two classes, one node class, the key to tree classes is that all operations must be completed using functions, that is, nodes cannot be operated directly in the tree class. You need to use functions in the node class to perform various operations.

It is too troublesome, but it is also advantageous to take the time to write it. I will never forget it after writing it carefully for several years. At the same time, it is safer to use functions to operate data and set the data as private members to be more compliant. The following code is provided.

  

1 # include <iostream> 2 using namespace std; 3 4 class BinNode {5 private: 6 int element; 7 BinNode * leftChild; 8 BinNode * rightChild; 9 public: 10 BinNode (int a, BinNode * left, BinNode * right) {11 element = a; 12 leftChild = left; 13 rightChild = right; 14} 15 int val () {return element;} 16 BinNode * left () {return leftChild;} 17 void setLeft (BinNode * t) {leftChild = t;} 18 BinNode * right () {return r IghtChild;} 19 void setRight (BinNode * t) {rightChild = t;} 20}; 21 class BST {22 private: 23 BinNode * root; 24 BinNode * insertHelp (int x, binNode * root) {25 BinNode * t = root; 26 if (t = NULL) {t = new BinNode (x, NULL, NULL); return t ;} 27 else if (x <t-> val () 28 t-> setLeft (insertHelp (x, t-> left ())); 29 else if (x> t-> val () 30 t-> setRight (insertHelp (x, t-> right (); 31 return t; 32} 33 void findHel P (const int x, int & count, BinNode * root) {34 count ++; 35 if (root = NULL) {count = 0; return ;} 36 else if (root-> val () = x) return; 37 if (x <root-> val () 38 findHelp (x, count, root-> left (); 39 if (x> = root-> val () 40 findHelp (x, count, root-> right ()); 41} 42 public: 43 BST () {root = NULL;} 44 ~ BST () {clear (root);} 45 46 void clear (BinNode * root) {47 if (root! = NULL) {48 clear (root-> left (); 49 clear (root-> right (); 50 delete root; 51} 52} 53 void insert (int & x) {54 root = insertHelp (x, root); 55} 56 void find (const int x, int & count) {57 findHelp (x, count, root); 58} 59}; 60 int main () {61 BST a; 62 int n; 63 cout <"Enter the number of nodes: "<endl; 64 cin> n; 65 cout <" input node values in sequence: "<endl; 66 for (int I = 0; I <n; I ++) {67 int x; cin> x; 68. insert (x); 69} 70 int num; 71 while (Cout <"Enter the value to be searched: (ctrl + z end search)" <endl) & (cin> num) & num! = EOF) {72 int count = 0; 73 a. find (num, count); 74 if (count = 0) 75 cout <"search failed! "<Endl; 76 else77 cout <" search successful! Search times: "<count <endl; 78} 79 system (" pause "); 80 return 0; 81}View Code

The following is the document address of the lab report.

Http://wenku.baidu.com/view/d97fb2b114791711cd791711

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.