Binary sorting tree

Source: Internet
Author: User


 

/* Binary sorting tree */
# Include <stdlib. h>
# Include <stdio. h>

Int counter;/* counter */
Struct tree/* structure of the phonograph */
{Struct tree * left;
Int data;
Struct tree * right;
};
Typedef struct tree treenode;/* declare the structure of the new type tree */
Typedef treenode * B _tree;/* declares the linked list of a binary tree */
B _tree insert_node (B _tree root, int node)/* Insert a binary tree node */
{B _tree newnode;
B _tree currentnode;
B _tree parentnode;
Newnode = (B _tree) malloc (sizeof (treenode);/* allocate new Node space */
Newnode-> data = node;
Newnode-> right = NULL;
Newnode-> left = NULL;
If (root = NULL) return newnode;
Else {currentnode = root;
While (currentnode! = NULL)
{Parentnode = currentnode;
If (currentnode-> data> node)
Currentnode = currentnode-> left;
Else currentnode = currentnode-> right;
}
If (parentnode-> data> node)
Parentnode-> left = newnode;
Else
Parentnode-> right = newnode;
}
Return root;/* return the pointer to the root node */
}
B _tree create_btree (int * data, int len)/* Create a binary tree */
{
B _tree root = NULL;
Int I;
For (I = 0; I <len; I ++)
Root = insert_node (root, data [I]);
Return root;
}
Void inorder (B _tree point)
{

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.