Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Assuming that the binary tree is stored in connection mode, write a recursive and non-recursive program to traverse the binary tree in order.
Assuming that the binary tree is stored in connection mode, write a recursive and non-recursive program to traverse the binary tree in order.
The following is whether the program I wrote before is helpful to you.

# contains "iostream.h"

# contains "string.h"

# contains "malloc.h"

# contains "stdio.h"

Typedef structure btree

{

Int data; //Range of tree nodes

Left and right clues of int ltag, rtag// tree nodes

Struct btree *left, * right// Left and right pointers of tree nodes, ltag, rtag= 1 Point to predecessors and successors, otherwise point to left and right child nodes.

} node;

Node * Search node (node *q, node *r)

{//Find the node where the node R will be inserted in the middle-order clue binary tree with the root node address q..

node * p;

p = q;

while( 1)

{

while(r-& gt; Data & LTP->; Data and information. & ampp->; ltag! = 1){ p = p->; Left; }//Continue to look left.

while(r-& gt; Data & GTP->; Data and information. & ampp->; rtag! = 1){ p = p->; Right; }//Continue to look to the right.

if(r-& gt; Data & LTP->; Data and information. & ampp->; ltag = = 1 | | r-& gt; Data & GTP->; Data and information. & ampp->; rtag = = 1 | | r->; data = = p-& gt; Data) off;

//When r-> Data & LTP->; Data and p have no left child or r->; Data & GTP->; Data and p are not made up of children

//or r-> data = = p-& gt; Data, node P finds it and jumps out of the loop.

}

Return (p); //return p node

}

node *InsertNode(node *rot,node *s)

{//Insert node S in the middle-order clue binary tree whose root node address is rot.

node * p;

if(rot==NULL)

{//If the root node is empty, the S node is inserted as the root node.

rot = s;

rot->; data = s-& gt; Data;

rot->; ltag = 1;

rot->; left = NULL

rot->; rtag = 1;

rot->; Right = empty;

Return (rot);

}

p=SearchNode(rot,s); //Call the SearchNode function to find the location of node P where S will be inserted.

if(s-& gt; data = = p-& gt; Data)

{//If the node already exists in the tree, release the node and return.

Free;

Return (rot);

}

if(s-& gt; Data & LTP->; Data)

{//If s-& gt; Data & LTP->; The data is inserted as the left child of p, at this time, the predecessor of S is the predecessor before P insertion, and the successor of S is P.

s-& gt; ltag = 1;

s-& gt; left = p-& gt; Left;

s-& gt; rtag = 1;

s-& gt; Right = p;;

p->; ltag = 0;

p->; left = s;

}

if(s-& gt; Data & GTP->; Data)

{//If s-& gt; Data & GTP->; The data is inserted as the right child of p, at this time, the successor of S is the successor before P insertion, and the predecessor of S is P.

s-& gt; rtag = 1;

s-& gt; right = p-& gt; Right;

s-& gt; ltag = 1;

s-& gt; Left = p;;

p->; rtag = 0;

p->; Right = s;;

}

Return (rot);

}

Node * Create Tree (Node *rt)

{//Begins to construct a binary sort tree with an empty root node, and returns the address of the root node.

int m; //Used to enter the value range of the root node.

Node * q;;

while( 1)

{

Printf ("Please enter a number: \ nm =");

//scanf("%d ",& ampm);

CIN & gt; & gtm; //Enter the value of the node.

If (m =-1) returns (rt);

Node * s;; //Create a new node to insert.

s =(node *)malloc(sizeof(node)); //Open a node space for S ..

s-& gt; Data = m;;

q=InsertNode(rt,s); //Call the InsertNode function to insert each node into the generated middle-order clue binary tree to form a new tree.

rt = q;

}

Return (rt);

}

node *Search(node *root,int x)

{//Find the node P whose node value is X in the middle-order clue binary tree whose root node address is root, and return P, otherwise return NULL.

node * p; //The location of the node whose return value is x..

P = root;

while( 1)

{

while(x & lt; p->; Data and information. & ampp->; ltag! = 1){ p = p->; Left; }

while(x & gt; p->; Data and information. & ampp->; rtag! = 1){ p = p->; Right; }

if(x & lt; p->; Data and information. & ampp->; ltag = = 1 | | x & gt; p->; Data and information. & ampp->; rtag = = 1 | | x = = p-& gt; Data) off;

}

If (x! = p->; data)p = NULL;

Return (p);

}

node *P_Next(node *root,int x)

{//Find the subsequent node Q traversed by node P with node value x in the middle-order clue binary tree with root node address as the root.

Nodes *p, * q;; //p is a node with a node value of x, and q is used to return the subsequent nodes traversed by p in the subsequent order.

p=Search(root,x); //Call the search function to find the location of node P..

The node with the value of If(p==NULL)// x does not exist. Wrong!

{

Returns NULL

}

If (p->; Ltag==0) returns (p->; Left); //If this node has a left child node, the left child node is returned.

other

{

If (p->; Rtag==0) returns (p->; Right); //If this node has no left child node and a right child node, the right child node is returned.

Otherwise//If the node is a leaf node,

{

q = p->; Right; //Find the successor node of this node.

If (q==NULL)// the successor node is empty, it is the last node traversed in the previous sequence, and it is empty.

{

Printf ("This node is the last node \ n");

Returns NULL

}

other

{

while(q->; rtag = = 1 & amp; & ampq->; Yes! = NULL){ q = q-& gt; Right; }//If the successor of the node has no right child, continue to look for the node in the successor direction.

If (q->; right==NULL)

{

Returns NULL

}

Otherwise, return (q->; Right); //Returns the right child node of the node.

}

}

}

}

Empty display (node *t)

{//Non-recursive middle order traversal middle order clue binary sort tree

Printf ("intermediate sequence is:");

while(t->; ltag! = 1){ t = t->; Left; }//Find the leftmost node, that is, the first node traversed in the middle order.

while(t! = empty)

{

printf("%d ",t-& gt; Data); //Print node values

while(t->; RTAG = =1)/The right pointer of the node points to the subsequent node.

{

t = t-& gt; Right;

If(t==NULL) returns; //t is empty, and traversal is over.

else printf("%d ",t-& gt; Data); //Print node values

}

if(t->; Rtag==0)// If the node has a right child node,

{

t = t-& gt; Right; //Start from the child node on the right.

while(t->; ltag! = 1){ t = t->; Left; }//Continue to look to the left for the leftmost node of this subtree.

}

}

}

void main()

{

int w;

Printf ("Enter a string of positive integers to construct a binary sort tree of the middle order clues, and finally use-1 as the ending mark \ n");

Node * root;

root = NULL

node * p;

Node * q;;

P=CreatTree (root);

Display (p);

while( 1)

{

Cout & lt< "\ nFind the subsequent nodes that traversed x in the previous sequence \ NX = ";;

CIN & gt; & gtw;

q=P_Next(p,w);

if(q = = NULL)cout & lt; & lt' This node is not in the tree or in the last node traversed in the previous order \ n';

other

{

Cout & lt< "\ nThe following nodes of this node are:";

cout & lt& ltq->; Data;

}

}

}