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 @   FredGrit  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示