C++ BST Insert Print,compile via g++ ,the *.cpp asterisk can represent all the files end with cpp

#include <iostream>
using namespace std;

class BSTNode
{
public:
    BSTNode *root;
    BSTNode *Left;
    BSTNode *Right;
    BSTNode *Parent;
    int Key;
    BSTNode();
    BSTNode* Insert(BSTNode *node,int keyValue);
    void InsertTree(int keyValue);
    void PrintTreeInOrder(BSTNode *node);
    void printTree();
};
#include "BSTNode.h"

BSTNode::BSTNode()
{
    root=NULL;
}

BSTNode *BSTNode::Insert(BSTNode *node, int keyValue)
{
    if (node == NULL)
    {
        node = new BSTNode;
        node->Key = keyValue;
        node->Parent = NULL;
        node->Left = NULL;
        node->Right = NULL;
    }
    else if (node->Key < keyValue)
    {
        node->Right = Insert(node->Right, keyValue);
        node->Right->Parent = node;
    }
    else
    {
        node->Left = Insert(node->Left, keyValue);
        node->Left->Parent = node;
    }
    return node;
}


void BSTNode::InsertTree(int keyValue)
{
    root=Insert(root,keyValue);
}

void BSTNode::PrintTreeInOrder(BSTNode *node)
{
    if (node == NULL)
    {
        return;
    }
    PrintTreeInOrder(node->Left);
    cout << node->Key << "\t";
    PrintTreeInOrder(node->Right);
}

void BSTNode::printTree()
{
    PrintTreeInOrder(root);
}
#include <iostream>
#include <uuid/uuid.h>
#include <ctime>
#include <string.h>
#include <fstream>
#include <ostream>
#include <istream>
#include <sstream>
#include <chrono>
#include <pthread.h>
#include "BSTNode.h"

using namespace std;

void tree10();

int main()
{
    tree10();
    return 0;
}

void tree10()
{
    int arr[]={10,20,30,15,19,14,13,50,70,100};
    BSTNode bst;
    int len=sizeof(arr)/sizeof(arr[0]);
    cout<<"Len="<<len<<endl;
    for(int i=0;i<len;i++)
    {
        bst.InsertTree(arr[i]);
    }

    bst.printTree();
    cout<<"\nFinished in tree10() "<<endl; 
}
//Compile 

g++ -g -std=c++2a -I. *.cpp -o h1 -luuid -lpthread

The *.cpp can stand for multiple files end with .cpp;

Run ./h1

 

posted @ 2022-01-17 18:26  FredGrit  阅读(28)  评论(0编辑  收藏  举报