Complete C code for creating a binary search tree

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.