* Create a binary tree ----> Load data, ----> Traverse ---> Display ---> Destruction *

xiaoxiao2021-03-05  25

/ * There is an error and want to countermere * /

/ * TWOTREE.C * /

/ * Create a binary tree ---> Load data,

----> Traverse ---> Display ---> Destroy * /

#include

#include

#ifndef debug

#define debug

/ ******** ********************** /

/ * Chain stack, determined whether the stack is empty, stack, out of the stack * /

/ * --------------------------------- * /

TypedEf int Dattype;

TypedEf DataType Elementtypt;

Typedef struct node

{

DataType Data;

Struct Node * LCHILD;

Struct Node * rchild;

Struct node * root;

} Node;

/ * Tree data structure * /

/ * ================================== ---- * /

Typedef struct stacknode

{

ElementTypt Data;

Struct stacknode * next;

} stacknode;

/ * Stack data structure * /

Void TinitStack / * Initialization Stack * /

{

TOP = (stacknode *) malloc (STACKNODE));

Top -> Next = NULL;

Return;

}

INT ISEMPTY (StackNode * TOP)

/ * Is it empty * /

{

Return (top -> next == null);

}

INT TPUSH (StackNode * TOP, ElementTypt Data)

/ * Press the element DATA into the stack TOP, the error returns 1 * /

{

StackNode * TEMP;

Temp = (stacknode *) malloc (STACKNODE));

IF (! Temp)

{

Return 1;

} / * Spatial assignment failed to return * /

Temp -> Data = DATA;

Temp -> next = top -> next;

Top -> Next = Temp;

Return 0;

}

INT TPOP (StackNode * TOP, ElementTypt * PTR)

{

StackNode * TEMP;

Temp = top -> next;

IF (! Temp)

{

Return 1;

} / * Stack empty return 1 * /

Top -> Next = Temp -> Next;

* PTR = Temp -> DATA;

Free (TEMP);

Return 0;

}

/ * Creat Tree * /

Void Initiate (Node * root)

/ * Initialized to empty tree * /

{

Root = (node ​​*) malloc (sizeof (node));

ROOT -> LCHILD = root -> rchild = null;}

Void Creat (Node * root, DataType Data)

{

IF (root)

{

Root -> DATA = DATA;

Return;

}

Root = (node ​​*) malloc (sizeof (node));

Root -> DATA = DATA;

ROOT -> LCHILD = root -> rchild = NULL;

}

Void Destory (Node * root)

/ * Destroy two forks root * /

{

StackNode * S;

Node * pre = root;

TinitStack (s);

While (pre! = null ||! iSempty (s))

{

IF (pre)

{

TPUSH (S, Pre -> DATA);

Pre = pre-> lchild;

}

Else

{

TPOP (s, & (pre -> data));

IF (! pre -> rchild)

/ * Free conditions

! (PDEL -> Right &&! P-> LEFT) * /

{

Node * pdel = pre;

Free (PDEL);

TPOP (s, & (pre -> data));

}

Else

{

Pre = pre -> rchild;

}

}

}

} / * Symmetrical encoding prevents leakage identifier * /

Void Insert (Node * root, DataType Data)

/ * Extreme Demoniration Fifty loading data, left sub-tree

{

Node * p = root;

IF (root! = null)

{

Creat (root, data);

Return;

}

While (p)

{

IF (Data <(P -> DATA))

{

P = P -> lchild;

}

Else

{

P = P -> rChild;

}

/ * Equal will load data to the right child * /

}

Creat (P, DATA);

}

Void Creattree (Node * root, DataType Data,

Node * LCHILD, NODE * RCHILD)

/ * Spanning tree structure: connect the node * /

{

root -> lchild = lchild;

Root -> rchild = rChild;

Root -> DATA = DATA;

Return;

}

Void Visit (Node * P)

{

Printf ("% D:", P -> data);

Return;

}

Void inorder (node ​​* root)

/ * 中 序 traversal tree * /

{

/ * Add code * /

StackNode * S;

Node * p = root;

TinitStack (s);

While ((p! = null) || (! iSempty (s)))))))

{

IF (p! = null) / * root pointer into the stack * /

{

TPUSH (S, (P -> DATA));

P = P -> lchild;

}

Else

{

TPOP (S, & (P -> DATA);

Visit (P);

P = P -> rChild;}

}

Return;

}

#ENDIF Debug

void main ()

{

Node * root;

INT A = 0. CH = 0, i = 0;

Printf ("INPUT ROOT DATA:");

Scanf ("% d", & a);

GetChar ();

Creat (root, a);

While (ch! = '$')

{

i;

Printf ("INPUT% D DATA:", I);

INSERT (root, ch);

CH = GetChar ();

}

GetChar ();

Inorder (root);

Destory (root);

Return;

}

============================

/ * TWOTREE.CPP

Compile in C environment!

Create a binary tree ---> Load data,

----> Traverse ---> Display ---> Destruction *

Both resembling recursive achievement, non-recursion, not familiar, so

* /

#include

#ifndef debug

#define debug

TypedEf int Dattype;

Typedef struct node

{

DataType Data;

Struct Node * LCHILD;

Struct Node * rchild;

} Node;

/ * Tree data structure * /

/

Node * initiate ()

/ * Initialized to empty tree * /

{

Node * root = 0;

Return root;

}

Node * Creat (Datatype Data)

/ * Building node * /

{

Node * TEMP = New Node;

Temp -> Data = DATA;

Temp -> LCHILD = 0;

Temp -> rchild = 0;

Return Temp;

}

/ ******************************************************** /

Void Insert (Node * & root, DataType Data)

// Don't this be like this, Node * & root

/ * Extreme Demoniration Fifty loading data, left sub-tree

{

Node * p = Creat (data);

IF (! root)

{

Root = P;

}

Else IF (P-> Data data)

{

INSERT (root-> lchild, p-> DATA);

}

Else

{

INSERT (root-> rchild, p-> data);

} / * Equal will load data to the right child * /

}

/ ************************************************** *** /

Void Printtree (Node * root)

/ * Recursive order traversal ---> Show from small to large * /

{

IF (! root) return;

PRINTTREE (root-> lchild);

COUT << root-> data << ":"; printtree (root-> rchild);

Return;

}

/ ************************************************** ******** /

Void Freetree (Node * root)

{

IF (! root) return;

Freetree (root -> lchild);

Freetree (root -> rchild);

Delete root; / / The node is finally deleted!

}

#ENDIF Debug

/// Test code

void main ()

{

Int a;

Node * root = initiate ();

Cout << "-1 to exit:" << Endl;

CIN >> A;

While (A! = -1) && cin.good ())

// encounter illegal inputs and exit cycles

{

INSERT (root, a);

CIN >> A;

}

IF (! cin.good ())

// Output error message

{

Cout << "The Type IS Error!" << endl;

}

PRINTTREE (root);

COUT << "OK?" << Endl;

Freetree (root); // Destroy the tree to prevent memory leakage

Return;

}

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

New Post(0)