Some basic operations about the tree, I originally written some of my research! Convenient everyone to learn! The tree can be stored in the form of an array or stored in the form of a linked list! Select different storage in accordance with different procedures! The array of operation is relatively simple! This article does not work! This article is only a simple study for chain storage!
Tree Linum form: struct node2 {datatype data; / * data * / struct node2 * lch, * rch; / * Left child and right child * /}; Struct Node3 {datatype data; / * data * / Struct Node3 * Lch, * rch, * parent; / * Left child and right child and dual-processed point * /}; two-binary tree binary table generation algorithm has two: algorithm 1 (iteration): #define n 50struct node2 * creat () {INT I, X, J; Struct Node2 * P, * T, * S [N]; Printf ("THE I = ?, THE x =? / N"); scanf ("THE I =% D, THE x =% D ", & I, & X); Printf (" / n "); while ((i! = 0) && (x! = 0)) {Q = (Struct Node2 *) Malloc (SIZEOF (Struct Node2 )); Q-> DATA = X; Q-> rCH = null; Q-> rCH = null; s [i] = q; if (i == 1) T = q; ELSE {J = I / 2; IF (I% 2) == 0) S [J] -> LCH = Q; ELSE S [J] -> RCH = =} PRINTF ("THE I =?, THE x =? / n"); scanf ("THE I =% D, THE x =% D", & I, & X); Printf ("/ n");} Return (T);} 二 二 (recursive): void create (struct node2 * t) { INTCH; Printf ("THE DATE / N"); scanf ("% D", & ch); Printf ('/ n "); if (CH == 0) / * value is 0, indicating empty tree * / { T = NULL;} else {if (! (t = (struct node2 *) malloc (Struct Node2)))) {exit (0);} t-> data = ch; CREATE (T-> LCH); Create (t-> rch);}} traversal two fork: First traverses: void preorder (struct node2 * p) {if (p! = null) {Printf ("% D / T", P-> DATA); Preorder (P-> LCH); Preorder (P-> RCH);}}} in the root traversal: void inorder (struct node2 * p) {if (p! = NULL) {inorder (p-> lch); Printf ("% d / t", p-> data); inorder (p-> rch);}} After the root traverses: void postorder (struct node2 * p) { IF (p! = null) {PostORDER (P-> LCH); PostORDER (P-> RCH); Printf ("% D / T", P-> Data);}}} Cabo 2 plug:
Struct Nodex {DataType Data; / * Data * / Struct Nodex * LCH, * RCH; / * Left and right child * / int LTAG, RTAGE; / * left and right signs * /} / * ltage = 0 means LCH pointing to the left child * // * LTAGE = 1 indicates that the LCH pointing directly forward * /// * RTAGE = 0 means RCH pointing right child * // * RTAGE = 1 means RCH pointing directly to the direct subsequent linearization algorithm: algorithm one (recursive): Void INTHREAD (STRUCT NODEX * P) {IF (p! = null) {INTHREAD (P-> LCH); Printf ("% 4D", P-> DATA); if (p-> lch! = null) {P -> ltag = 0;} else {p-> ltage = 1; p-> lch = pr;} if (pr! = null) {IF (pr-> rch! = null) {PR-> RTAG = 0; } Else {pr-> RTAG = 1; Pr-> rCH = p;}} pr = P; INTHREAD (P-> rch);}}} algorithm two (iterative): void INTHREAD (Struct Nodex * t) {P = T-> LCH; while (p! = t) {while (p-> ltag == 0) {p = p-> lch;} printf ("% 4d", p-> data); while ((P- > RTAG == 1) && (p-> rch! = t) {p = p-> rch; printf ("% 4d", p-> data);} P = P-> rch;}} in the root clue Tree retrieve a node's forward drive and subsequent: find the pre-drive: struct nodex * IMPRE (struct nodex * q) {if (q-> ltag == 1) {p = q-> lch; else {r = q-> LCH; while (r-> rg! = 1) {r = r ... RETURN (P);} Find: Struct No DEX * INSUCC (Struct Nodex * q) {if (q-> RTAG == 1) {p = q-> rch;} else {r = q-> rch; while (r-> lch! = 1) {r = r-> LCH;} P = r;} return (p);} Traversing bifurcation on the middle of the root clue: TypedEf char DataTypevoid INTHORDER (Struct Nodex * T) {struct nodex * p; p = T; if (p! = NULL) {while (p-> lch! = Null) {p = p-> lch;}}}}} printf ("% 4c", p-> data); while (p->
RCH! = NULL) {P = INSUCC (P); Printf ("% 4C", P-> DATA);}}} binary sort tree: struct nodb {int Data; struct nodb * lch, * rch;}; Fork Sort Tree Insert: Void INSERT (Struct Nodb * T, Struct Nodb * S) {if (t = null) T = S; Elseif (S-> Data