Binary tree traversal and clue Binary Tree

Source: Internet
Author: User

". "Represents the empty tree. The program recursively traverses the binary tree in the first, middle, and back order, and then traverses the binary tree in the middle non-recursive way, the leaf nodes are output in a recursive manner in the middle order and the number of leaf nodes is counted. Finally, the binary tree is clues and the clues are traversed in the middle order.

# Include "stdio. H "# include" stdlib. H "typedef struct bithrnode {char data; int ltag, rtag; struct bithrnode * lchild, * rchild;} bithrnode; int link = 0; int thread = 1; int N0 = 0; bithrnode * pre; bithrnode * bithrcreat () {char X; bithrnode * P = NULL; scanf ("% C", & X); If (x = '. ') return NULL; P = (bithrnode *) malloc (sizeof (bithrnode); P-> DATA = x; P-> ltag = p-> rtag = link; p-> lchild = bithrcreat (); P-> rchild = bithrcreat (); Return P;} void Visi T (char ch) {printf ("% C", CH);} void preorder (bithrnode * root)/* traverse the binary tree in sequence, root is the pointer to the root node of a binary tree (or a subtree) */{If (root! = NULL) {visit (root-> data);/* access the root node */preorder (root-> lchild ); /* traverse the left subtree first */preorder (root-> rchild);/* traverse the right subtree first */} void inorder (bithrnode * root) /* traverse the binary tree in the middle order. Root is the pointer to the root node of the binary tree (or a subtree) */{If (root! = NULL) {inorder (root-> lchild);/* traverse the left subtree In The Middle Order */visit (root-> data ); /* access the root node */inorder (root-> rchild);/* traverse the right subtree in ascending order */} void postorder (bithrnode * root)/* Then traverse the binary tree in descending order, root is the pointer to the root node of a binary tree (or a subtree) */{If (root! = NULL) {postorder (root-> lchild);/* traverse the left subtree in descending order */postorder (root-> rchild ); /* traverse the right subtree in descending order */visit (root-> data);/* access the root node */} void preleaf (bithrnode * root) /* output of the leaf node traversed in the forward Order */{If (root! = NULL) {If (root-> lchild = NULL & root-> rchild = NULL) {visit (root-> data); N0 ++ ;} preleaf (root-> lchild); preleaf (root-> rchild) ;}} void inorder1 (bithrnode * t) {bithrnode * s [32]; // s is a pointer array, int Top =-1; while (T! = NULL | top! =-1) {// press the stack until the left subtree is empty while (T! = NULL) {s [++ top] = T; t = T-> lchild;} If (top! =-1) {T = s [top --]; // output stack visit (t-> data); t = T-> rchild; // The right child pointing to the node and returning to the while loop pressure stack }}void inthreading (bithrnode * A) {if (a) {inthreading (a-> lchild ); if (! A-> lchild) {A-> ltag = thread; A-> lchild = pre;} If (! Pre-> rchild) {pre-> rtag = thread; pre-> rchild = A;} Pre = A; inthreading (a-> rchild );}} bithrnode * inorderthreading (bithrnode * A) {bithrnode * H = (bithrnode *) malloc (sizeof (bithrnode); If (! H) Exit (-1); H-> ltag = link; H-> rtag = thread; H-> rchild = H; If (! A) H-> lchild = H; else {H-> lchild = A; Pre = H; inthreading (a); pre-> rtag = thread; pre-> rchild = H; H-> rchild = pre;} return h;} void inordertraverse (bithrnode * A) {bithrnode * P = A-> lchild; while (P! = A) {While (p-> ltag = link) P = p-> lchild; visit (p-> data ); while (p-> rtag = thread & P-> rchild! = A) {P = p-> rchild; visit (p-> data);} p = p-> rchild;} int main () {bithrnode * A = bithrcreat (); printf ("first-order traversal Binary Tree:"); preorder (a); printf ("\ n middle-order traversal Binary Tree :"); inorder (a); printf ("\ n post-order traversal Binary Tree:"); postorder (a); printf ("\ n leaf node:"); preleaf (); printf ("\ n number of leaf nodes:"); printf ("% d", N0); printf ("\ n middle-Order Non-recursive traversal:"); inorder1 (); printf ("\ n middle-order clue-Based Binary Tree, middle-order traversal of the established clue-Based Binary Tree:"); A = inorderthreading (a); inordertraverse (); printf ("\ n"); Return 0 ;}




Binary tree traversal and clue Binary 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.