Create a complete C code for a two-fork lookup tree

Source: Internet
Author: User

BstBasic Concepts

    1. Two-fork search tree (binary search trees), Also known as binary sorting tree (binary sort trees), also known as two search books.

    2. 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;

    3. 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

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.