Summary and review of data structure coding//about the establishment and hierarchy of binary trees, other basic operations such as traversal (recursive, non-recursive) seeking depth # include <cstdio> #include <cstdlib>//# Define _OJ_TYPEDEF struct tree{char data; struct tree *left; struct tree *right;} Tree, *bitree;typedef struct stack1{int top, base; Bitree *elem;} Stack1, *stack; Stackinit_stack (void) {Stack s; s = (Stack) malloc (sizeof (STACK1)); S->elem = (bitree*) malloc (Bitree); S->top = s->base = 0; return s;} Bitreeinit_bitree (Bitree T) {char ch; scanf ("%c", &ch); if (ch = = ' # ') T = NULL; else {T = (bitree) malloc (sizeof (tree)); T->data = ch; T->left = Init_bitree (t->left); T->right = Init_bitree (t->right); } return T;} Voidpush (Stack s, bitree data) {s->elem[s->top++] = data;} Bitreepop (Stack s) {return s->elem[--s->top];} Bitreeget_top (Stack s) {return s->elem[s->top-1];} Intisempty (Stack s) {if (s->base = = s->top) return 1; Else return 0;} Voidpreoder_travers (Bitree t) {if (t! = NULL) {preoder_travers (t->left); printf ("%c", t->data); The middle sequence traversal is used for the test tree preoder_travers (t->right); }}voidpreoder_traver (Bitree T) {Stack s; s = Init_stack (); while (t! = NULL | | IsEmpty (s)! = 1) {//to the left to the end so far before turning to the right subtree while (t) {push (S, t); printf ("%c\n", t->data); T = t->left; } if (IsEmpty (s)! = 1) {T = pop (s);//printf ("%c\n", t->data); T = t->right; }}}//------------------------------------------------------------------------------------//post-traversal binary tree Voidpostoder_ Traver (Bitree T) {Stack s; s = Init_stack (); Bitree T1, pre; T1 = T; Pre = NULL; T1 current node, pre previous Access node while (T1! = NULL | | IsEmpty (s)! = 1) {while (T1) {push (S, T1); T1 = t1->left; Go straight to the end} T1 = Get_top (s); if (t1->right = = NULL | | T1->right = = Pre) {//t1 Right sub-tree is emptyOr has been accessed by printf ("%c\n", t1->data); Pre = T1; T1 = NULL; Pop (s); Eject it} else T1 = t1->right; }}intdept_tree (Bitree T) {int dept = 0, dept1 = 0, deep = 0; if (T) {dept = dept_tree (t->left); DEPT1 = Dept_tree (t->right); Starting from the leaf node, recursively to the root node deep = dept > dept1? Dept + 1:dept1 + 1; } return deep;} int main (int argc, char const *argv[]) {#ifndef _oj_//online JUDGE freopen ("Input.txt", "R", stdin); Freopen ("Output.txt", "w", stdout); #endif int deep; Bitree T; t = Init_bitree (t); Build a binary tree//preoder_travers (T); Recursive traversal//Preoder_traver (T); Non-recursive traversal//Postoder_traver (T); Non-recursive sequential traversal of deep = Dept_tree (T); The depth of the binary tree printf ("Tree depth:%d\n", deep); return 0;} // ---------------------------------------------------------------------------------------///Hierarchy Traversal idea//from the root each time the team out of an element, will be this element of the Leftchild, Rightchild queue until the team will not be able to queue their last all out
Data structure (review)--------basic operation of binary tree