Complete C code for creating a binary search tree
Basic concepts of BST
Binary Search Tree, also known as Binary Sort Tree ).
It is either an empty tree or a binary tree of the following nature: (1) if the left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of its root node; (2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node. (3) the left and right subtree are also Binary Decision Trees;
Simply put: left child <双亲结点<右孩子。
Therefore, we traverse the binary tree in the middle order to obtain a series in ascending order.
Create complete bst c code
/* Create a bst c code implementation */# include
# Include
Typedef int Elemtype; typedef struct BiTNode {Elemtype data; struct BiTNode * lchild, * rchild;} BiTNode, * BiTree; // insert a node in a given BST, its data field is elementint BSTInsert (BiTree * t, Elemtype element) {if (NULL = * t) {(* t) = (BiTree) malloc (sizeof (BiTNode )); (* t)-> data = element; (* t)-> lchild = (* t)-> rchild = NULL; return 1 ;} if (element = (* t)-> data) return 0; if (element <(* t)-> data) return BSTInsert (& (* t)-> lchild, element); return BSTInsert (& (* t)-> rchild, element);} // create BSTvoid CreateBST (BiTree * t, Elemtype * a, int n) {(* t) = NULL; for (int I = 0; I
Lchild); printf ("% d", t-> data); PrintBST (t-> rchild) ;}} int main () {int n; int *; biTree t; printf ("Enter the number of nodes in the binary search tree: \ n"); scanf ("% d", & n); a = (int *) malloc (sizeof (int) * n); printf ("Enter and find the node of the tree: \ n"); for (int I = 0; I
Test data and test results
After debugging, we can confirm that the resulting binary tree is:
BST created successfully.