BstBasic Concepts
- Two-fork search tree (binary search trees), Also known as binary sorting tree (binary sort trees), also known as two search books.
- It is either an empty tree or a two-fork tree with the following properties: (1) The Joz tree is not empty, then the value of all nodes on the left subtree is less 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, and (3) the left and right subtrees are two forks respectively;
- Simply put: Left child < parent node < right child.
Therefore, the search binary tree for the middle sequence traversal, the result is a small to large sequence.
Create full C code for BST
/* Create C code implementation of BST */#include <stdio.h> #include <stdlib.h>typedef int elemtype;typedef struct BITNODE{ELEMTYPE Data;struct Bitnode *lchild, *rchild;} Bitnode, *bitree;//inserts a node in a given BST with a data field of 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); r Eturn Bstinsert (& (*t)->rchild, Element);} Create Bstvoid Createbst (Bitree *t, elemtype *a, int n) {(*t) = null;for (int i=0; i<n; i++) Bstinsert (T, A[i]);} The middle sequence traversal prints bstvoid printbst (Bitree t) {if (t) {Printbst (t->lchild);p rintf ("%d", t->data); Printbst (T->rchild);}} int main () {int n;int *a; Bitree t;printf ("Please enter the node number of the binary lookup tree: \ n"), scanf ("%d", &n), a = (int *) malloc (sizeof (int) *n);p rintf ("Please enter the node of the tree to find: \ n"); for (int i=0; i<n; i++) scanf ("%d", &a[i]); Createbst (&t, A, n);p rintf ("The middle sequence traversal result of the BST is: \ n"); Printbst (t);p rintf ("\ n"); return 0;}
test data and test results
After commissioning, you can confirm that the resulting two-fork tree is:
BST established successfully.
Create a complete C code for a two-fork lookup tree