the data type of the given binary tree is as follows
typedef char Element;
struct Node
{
Element data;
struct Node *lchild;
struct Node *rchild;
typedef struct NODE Btnode;
typedef struct NODE * BTREE;
Two fork tree creation I
Complete the Btree create_btree (char s[]) function, the function creates a binary tree from the string s, where the string s is represented by a generalized table of two-forked trees consisting only of ' (', ', ') ', ', ' and uppercase and lowercase characters, such as a (B (D,), C (E,f (, H) ), and the string s ends with ' i '.
Btree NewNode (Element x)
{
btree p= (btree) malloc (sizeof (Btnode));
p‐>data=x;
p‐>lchild=null;
p‐>rchild=null;
return p;
}
Btree Create_btree (char s[])
{
int i,k,top;
Btree path[n],p;
k=0;
top=‐1;
For (i=0;s[i]!= ' and i++ ')
{
switch (s[i))
{case
' (':
path[++top]=p;
k=1;
break;
Case ', ':
k=2;
break;
Case ') ':
top‐‐;
break;
if (Isalpha (S[i]))
{
p=newnode (s[i]);
if (k==1)
path[top]‐>lchild=p;
else if (k==2)
path[top]‐>rchild=p;
}
}
return path[0];
}
two fork Tree Creation II
Complete the Btree create_btree (char s[],int left,int right) function, which creates a binary tree from the string s (from S[left to S[right]), where the string s is only ' (', ') ', ', ', ' and a generalized table representation of a two-forked tree of uppercase and lowercase characters, such as a (B (D,), C (E,f (, H)), and the string s ends with '.
int find (char s[],int left,int right)
{
int k=0;
int i;
for (i=left;i<=right;i++)
{
if (s[i]== ', ' &&k==1] return
i;
if (s[i]== ')
k++;
else if (s[i]== ') ')
k‐‐
}
return to left;
}
Btree NewNode (Element x)
{
btree p= (btree) malloc (sizeof (Btnode));
p‐>data=x;
p‐>lchild=null;
p‐>rchild=null;
return p;
}
Btree Create_btree (char s[],int left,int right)
{
int mid;
if (left>right) return
NULL;
Btree P=newnode (S[left]);
Mid=find (s,left,right);
P‐>lchild=create_btree (s,left+2,mid‐1);
P‐>rchild=create_btree (s,mid+1,right‐1);
return p;
}
two fork Tree Creation III
the first sequence traversal output of the two-fork tree is given (the empty node uses '. ') , please construct the two-fork tree and output the node of the binary tree in order of the sequence traversal.
Enter a description:
Line only by '. ' A string consisting of uppercase and lowercase characters that represents the forward traversal output of a binary tree, where '. ' Represents an empty node with a string length not exceeding 100.
Output Description:
Outputs the nodes of the binary tree in a single row in order of the sequence traversal, and the null node does not output.
Enter a sample column:
ABD ... C..
Output Sample columns:
Dbac
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <queue> #include <cctype
> Using namespace std;
#define N-ary typedef char Element;
struct Node {Element data;
struct Node *lchild;
struct Node *rchild;
};
typedef struct NODE Btnode;
typedef struct NODE * BTREE;
Btree Create_btree ();
Btree NewNode (Element x);
void Pre_order (btree root);
int main () {btree root;
Root=create_btree ();
Pre_order (root);
printf ("\ n");
return 0;
} btree NewNode (Element x) {btree p= (btree) malloc (sizeof (Btnode));
p‐>data=x;
p‐>lchild=null;
p‐>rchild=null;
return p;
} btree Create_btree () {btree root;
char c;
C=getchar ();
if (c== '. ')
return NULL;
Root=newnode (c);
Root‐>lchild=create_btree ();
Root‐>rchild=create_btree ();
return root;
} void Pre_order (Btree root) {if (root!=null) {pre_order (root‐>lchild);
printf ("%c", root‐>data); Pre_order (Root‐>rchild); }
}