Basic operation algorithm for tree

xiaoxiao2021-03-05  21

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 DATA) INSERT (T-> LCH, S) Else INSERT (T-> RCH, S);} Two-inserted Sort Tree Establishment: Struct Nodb * Creat () {INT N, I, K; T = NULL; Printf ("How Many Node You Want Put! / N "); Scanf ("% D ", & n); printf (" / n "); for (i = 1; i <= n; i ) {printf (" please the data! / N "); scanf (" % D ", & K); Printf (" / n "); s = (Struct NODB *) Malloc (Struct Nodb)); S-> Data = K; S-> LCH = NULL; S-> rCH = NULL; INSERT (T, S);} Return (T);} binary sort tree looks out: struct nodb * Find (struct nodb * t, int x) {struct nodb * p = t; while (P ! = NULL) && (P-> Data! = X)) {IF (x data) P = p-> lch; else p = p-> rch;} return (p);} Hufman Tree: struct nodeh {int Data; int LCH, RCH; int Tag; / * tag = 0 represents independence; tag = 1 indicates that the node has been incorporated into the tree * /}; the implementation of the Hafman tree algorithm: # Define Max 50INT Huffman (Struct Nodeh R [Max]) {INT I, J, N, M1, M2, X1, X2, T; Printf ("please input number of the leaf! / n"); scanf ("% D ", & n); For (j = 1; j <= n; j ) {scanf ("% d", & r [j] .data); r [j] .tag = 0; r [j] .lch = 0; r [J ] .RCH = 0;} i = 0; while (i

转载请注明原文地址:https://www.9cbs.com/read-33217.html

New Post(0)