#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
原文链接: https://www.cnblogs.com/Fred1987/p/15814766.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/186208
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!