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.