The creation of BST (Binary Search Tree)

The following is the whole process of creating this tree

1. Generate structure

The tree node contains a numerical field and two pointer fields, which point to the left and right children respectively.
Structure indication:
Insert picture description here

It is defined as follows:

struct TreeNode {
    
    
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode() :val(0), left(nullptr), right(nullptr) {
    
    }
	TreeNode(int x) :val(x), left(nullptr), right(nullptr) {
    
    }
	TreeNode(int x, TreeNode* left, TreeNode* right) :val(x), left(left), right(right) {
    
    }
};

2. Definition of the class

The root variable is encapsulated in the class BST , the root can be obtained using the getroot member function, and the function buildBST is declared .

class BST {
    
    
	TreeNode* root;
public:
	TreeNode* getroot()
	 {
    
    
		return root;
	}
	void buildBST(vector<int>& vec);
};

3. Construction of the search tree

void  BST::buildBST(vector<int>& vec) {
    
    
	if (vec.size() == 0)
		root = nullptr;//若为空树,则置空

	root = new TreeNode(vec[0]);//创建根节点,并初始化为vec[0]
	for (int i = 1; i < vec.size(); i++) {
    
    
		TreeNode* newNode = new TreeNode(vec[i]);//创建节点
		TreeNode* Root = root;//保存当前根节点,用Root寻找插入位置
		while (Root) {
    
    
			if (newNode->val < Root->val) {
    
    //若小于根节点,放在节点左侧
				if (Root->left == nullptr) {
    
    //当根节点没有左孩子时,放置到此,此次插入结束,跳出循环
					Root->left = newNode;
					break;
				}
					Root = Root->left;//继续深入寻找
			}
			else {
    
    
				if (Root->right == nullptr) {
    
    
					Root->right = newNode;
					break;
				}
				else {
    
    
					Root = Root->right;
				}
			}
		}
	}
}

4. Print out search tree in middle order

According to the characteristics of the search tree, print it in the middle order to get a series of numbers from small to large.
The recursive method is used for in-order traversal:

void InOrderprint(TreeNode* root) {
    
    //中序打印树
	if (root == nullptr)
		return;
	InOrderprint(root->left);
	cout << root->val << " ";
	InOrderprint(root->right);
}

Complete code:

#include <iostream>
#include <vector>
using namespace std;

struct TreeNode {
    
    
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode() :val(0), left(nullptr), right(nullptr) {
    
    }
	TreeNode(int x) :val(x), left(nullptr), right(nullptr) {
    
    }
	TreeNode(int x, TreeNode* left, TreeNode* right) :val(x), left(left), right(right) {
    
    }
};

class BST {
    
    
	TreeNode* root;
public:
	TreeNode* getroot() {
    
    
		return root;
	}
	void buildBST(vector<int>& vec);
};

void  BST::buildBST(vector<int>& vec) {
    
    
	if (vec.size() == 0)
		root = nullptr;

	root = new TreeNode(vec[0]);//创建根节点,并初始化为vec[0]
	for (int i = 1; i < vec.size(); i++) {
    
    
		TreeNode* newNode = new TreeNode(vec[i]);//创建节点
		TreeNode* Root = root;//保存当前根节点,用Root寻找插入位置
		while (Root) {
    
    
			if (newNode->val < Root->val) {
    
    //若小于根节点,放在节点左侧
				if (Root->left == nullptr) {
    
    //当根节点没有左孩子时,放置到此
					Root->left = newNode;
					break;
				}
					Root = Root->left;//继续深入寻找
			}
			else {
    
    
				if (Root->right == nullptr) {
    
    
					Root->right = newNode;
					break;
				}
				else {
    
    
					Root = Root->right;
				}
			}
		}
	}
}

void InOrderprint(TreeNode* root) {
    
    //中序打印树
	if (root == nullptr)
		return;
	InOrderprint(root->left);
	cout << root->val << " ";
	InOrderprint(root->right);
}


int main(int argc, char* argv[])
{
    
    
	vector<int> vec = {
    
     8,4,3,2,1,5,6,7 };
	BST bst;
	bst.buildBST(vec);
	TreeNode* roo = bst.getroot();
	InOrderprint(roo);
	return 0;
}

Running effect chart:

Insert picture description here

Guess you like

Origin blog.csdn.net/Genius_bin/article/details/113176739