The establishment of binary search tree
The establishment of binary search tree is to insert elements into the binary tree. When the first element is inserted, the binary search tree is an empty tree, which directly applies for a memory space to put the elements into the tree. When the elements are inserted again, the comparison starts from the root node. If the element value to be inserted is large, the nodes in the tree go to the right. If it is small, the nodes in the tree go to the left until an empty position is found, and the nodes are inserted
Finding elements in binary search tree
When searching for an element in a binary search tree, first compare the value of the element with that of the root node. If the value of the element is small, the root node will go to the left (root = root - > left); if the value of the element is large, the root node will go to the right until it finds an equal size element or goes to the end of the tree
As a tree, binary search tree can also be traversed first, middle and last, and the order traversal is equivalent to the ordered output of the elements in the tree from large to small
#include <cstdio> #include <cstdlib> #include <algorithm> #include <stack> #include <queue> #include <malloc.h> using namespace std; #define OK 1 #define ERROR -1 #define TRUE 1 #define FALSE 0 typedef int Status; typedef struct STnode* link; struct STnode{ int item;//Element value link l;//The left subtree link of this node link r;//The right subtree link of this node }; //Insertion of binary search tree link STinsert(link st, int num) { if(st == NULL) { //Request memory space link st = (link)malloc(sizeof(struct STnode)); //Assign values to nodes st->item = num; //Leave left and right subtrees empty st->l = NULL; st->r = NULL; //Return node return st; } //printf("*%d* ", st->item); //When the value of this node is greater than the value of the element if(st->item > num) { //Left subtree of recursive call node st->l = STinsert(st->l, num); } //When the value of this node is less than the element value else if(st->item < num) { //Right subtree of recursive call node st->r = STinsert(st->r, num); } } //Searching for elements in binary search tree Status STsearch(link h,int v) { //Empty tree, no corresponding value, failed to find if(h == NULL) { return FALSE; } //Find the corresponding element value, find successfully if(v == h->item) { return TRUE; } //When the value of this node is greater than the value of the element if(v < h->item) { //Left subtree of recursive call node STsearch(h->l, v); } //When the value of this node is less than the element value else { //Right subtree of recursive call node STsearch(h->r, v); } } //Recursive tree preorder traversal void Traverse1(link tree) { if(tree) { //Output root node first printf("%d ", tree->item); //Recursion of its left subtree Traverse1(tree->l); //Recursion of its right subtree Traverse1(tree->r); } } //Use BST sort, i.e. medium order output void BSTsort(link h) { if(h == NULL) { return; } //Recursive call left subtree BSTsort(h->l); //Output element value printf("%d ", h->item); //Recursive call right subtree BSTsort(h->r);//Recursive adjustment } int main() { int a[12] = {2, 5, 3, 7, 6, 1, 4, 11, 8, 10, 9, 12}; link st = NULL; int i; for(i = 0; i < 12; i++) { st = STinsert(st, a[i]); } Traverse1(st); printf("\n");//Left subtree of recursive call node if(STsearch(st, 7)) { printf("7 Exists in the tree.\n"); } else { printf("** Does not exist in the tree.\n"); } BSTsort(st); printf("\n"); return 0; }