Summary of the method for creating a binary tree

Source: Internet
Author: User

When doing algorithm questions, you often need to create a binary tree for testing, including complex, integer, and search for Binary Trees. There are also many methods. However, an error occurs because the carriage return character is always read when reading data from the keyboard. So let's summarize these methods here:

Binary Tree node definition:

struct bintree{char value;bintree *left,*right;}


Character Type:

Bintree * CreateBiTree () {char ch; bintree * T; scanf ("% c", & ch); fflush (stdin); if (ch = '#') T = NULL; else {T = (bintree *) malloc (sizeof (bintree); T-> value = ch; printf ("\ n enter the left subnode of % c:", T-> value); T-> left = CreateBiTree (); printf ("\ n enter the right child node of % c:", T-> value); T-> right = CreateBiTree ();} return T; // return the root node}

Integer:

Bintree * CreatBT () // create (integer) binary tree {bintree * t; int x; scanf ("% d", & x); // getchar (); if (x = 0) {t = NULL;} else {t = (bintree *) malloc (sizeof (bintree); t-> value = x; printf ("\ n enter the left subnode of the % d node:", t-> value); t-> left = CreatBT (); printf ("\ n enter the right child node of the % d node:", t-> value); t-> right = CreatBT ();} return t ;}

The above code is tested in the vs2010 environment. Other methods will be added later.

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.