Data Structure-Binary Tree-hierarchical traversal binary tree (chain traversal-queue)

Source: Internet
Author: User
# Define Char/* character type */# include <stdio. h>/* EOF (= ^ Z or F6), null */# include <stdlib. h> # include <math. h>/* floor (), Ceil (), ABS () */# define true 1 # define false 0 # define OK 1 # define error 0 typedef int status; /* status is the function type, and its value is the function result status code, such as OK */# ifdef chartypedef char telemtype; telemtype nil = ''; /* the struct type is empty with a space character */# endif # ifdef inttypedef int telemtype; telemtype nil = 0;/* the integer type is null with 0 */# endiftypedef struct bitnode {T Elemtype data; struct bitnode * lchild, * rchild;/* Left and Right child pointer */} bitnode, * bitree; typedef bitree qelemtype; /* set the queue element to the binary tree pointer type */typedef struct qnode // queue node struct {qelemtype data; struct qnode * Next;} qnode, * queueptr; typedef struct // queue {queueptr front, rear;/* head and tail pointer */} linkqueue; Status initbitree (bitree * t) {/* operation result: construct an empty binary tree T */* t = NULL; Return OK;} void createbitree (bitree * t) {telemtype ch; # ifdef charscanf ("% C ",& Ch); # endif # ifdef intscanf ("% d", & Ch); # endifif (CH = nil)/* null */* t = NULL; else {* t = (bitree) malloc (sizeof (bitnode); If (! * T) Exit (overflow); (* t)-> DATA = CH;/* generate the root node */createbitree (& (* t)-> lchild ); /* construct the left subtree */createbitree (& (* t)-> rchild);/* construct the right subtree */} status initqueue (linkqueue * q) {/* construct an empty queue Q */(* q ). front = (* q ). rear = (queueptr) malloc (sizeof (qnode); If (! (* Q ). front) Exit (overflow); (* q ). front-> next = NULL; Return OK;} status queueempty (linkqueue q) {/* If Q is an empty queue, true is returned; otherwise, false */If (Q. front = Q. rear) return true; elsereturn false;} status enqueue (linkqueue * q, qelemtype e) {/* Insert a new team-end element whose Element E is Q */queueptr P = (queueptr) malloc (sizeof (qnode); If (! P)/* storage allocation failed */exit (overflow); P-> DATA = E; P-> next = NULL; (* q ). rear-> next = P; (* q ). rear = P; Return OK;} status dequeue (linkqueue * q, qelemtype * E) {/* If the queue is not empty, delete the queue Header element of Q and use e to return its value, and return OK. Otherwise, Error */queueptr P is returned. If (* q ). front = (* q ). rear) Return Error; P = (* q ). front-> next; * E = p-> data; (* q ). front-> next = p-> next; If (* q ). rear = p) (* q ). rear = (* q ). front; free (p); Return OK;} void levelordertraverse (bitree T, status (* visit) (telemtype )){ /* Initial condition: binary tree T exists, and visit is an application function for node operations * // * operation result: sequence recursion traversal T (using Queue ), call the visit function for each node once and only once */linkqueue Q; qelemtype A; If (t) {initqueue (& Q); enqueue (& Q, T); While (! Queueempty (q) {dequeue (& Q, & A); visit (a-> data); if (a-> lchild! = NULL) enqueue (& Q, A-> lchild); if (a-> rchild! = NULL) enqueue (& Q, A-> rchild);} printf ("\ n") ;}} status visitt (telemtype E) {# ifdef charprintf ("% C", e); # endif # ifdef intprintf ("% d", e); # endifreturn OK;} void main () {bitree T; initbitree (& T); # ifdef charprintf ("Please input a binary tree in sequence (for example, three spaces of AB indicate that A is the root node, and B is the binary tree of the Left subtree) \ n "); # endif # ifdef intprintf ("Please input a binary tree in sequence (for example, 1 2 0 0 0 indicates 1 is the root node and 2 is the binary tree in the left subtree) \ n "); # endifcreatebitree (& T); printf ("layered traversal Binary Tree: \ n"); levelordertraverse (T, visitt );}

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.