于是我想把指向二叉树节点的指针(BiTree)转换为int或void*,存入栈中,从栈中pop出来再强制转换为指向树节点指针,但在转换回来时报错了,(BiTree)p->lchild这一步出错,如以下代码所示:
栈的实现代码文件如下:
#ifndef _SQSTACK_H_
#define _SQSTACK_H_
#define STACKELEMTYPE void*
//#define STACKELEMTYPE int
typedef struct
{
STACKELEMTYPE* base;
STACKELEMTYPE* top;
int stacksize;
}SqStack;
//push,pop.....
二叉树的实现代码文件:
#define BTElemType char
typedef struct BiTNode
{
BTElemType elem;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef void (* VISIT)(BTElemType e);
//......
STATUS InOrderTraverse(BiTree T,VISIT visit)
{
SqStack mystack;
Init_SqStack(mystack);
Push(mystack,T);
void* p;
while (!StackEmpty(mystack))
{
while(GetTop(mystack,p)&&p)
{
Push(mystack,(BiTree)p->lchild);//error C2227: “->lchild”的左边必须指向类/结构/联合/泛型类型
}
Pop(mystack,p);
if(!StackEmpty(mystack))
{
Pop(mystack,p);
//visit((BiTree)(p)->elem);
Push(mystack,(BiTree)p->rchild);//error C2227: “->rchild”的左边必须指向类/结构/联合/泛型类型
}
}
return SUCCESS;
}
6 个解决方案
#1
(BiTree)p->lchild
改为((BiTree)p)->lchild
改为((BiTree)p)->lchild
#2
//C++ Operators
// Operators specify an evaluation to be performed on one of the following:
// One operand (unary operator)
// Two operands (binary operator)
// Three operands (ternary operator)
// The C++ language includes all C operators and adds several new operators.
// Table 1.1 lists the operators available in Microsoft C++.
// Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators. Operators associate with either the
//expression on their left or the expression on their right; this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
// Table 1.1 shows the precedence and associativity of C++ operators
// (from highest to lowest precedence).
//
//Table 1.1 C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator | Name or Meaning | Associativity |
//+------------------+-----------------------------------------+---------------+
//| :: | Scope resolution | None |
//| :: | Global | None |
//| [ ] | Array subscript | Left to right |
//| ( ) | Function call | Left to right |
//| ( ) | Conversion | None |
//| . | Member selection (object) | Left to right |
//| -> | Member selection (pointer) | Left to right |
//| ++ | Postfix increment | None |
//| -- | Postfix decrement | None |
//| new | Allocate object | None |
//| delete | Deallocate object | None |
//| delete[ ] | Deallocate object | None |
//| ++ | Prefix increment | None |
//| -- | Prefix decrement | None |
//| * | Dereference | None |
//| & | Address-of | None |
//| + | Unary plus | None |
//| - | Arithmetic negation (unary) | None |
//| ! | Logical NOT | None |
//| ~ | Bitwise complement | None |
//| sizeof | Size of object | None |
//| sizeof ( ) | Size of type | None |
//| typeid( ) | type name | None |
//| (type) | Type cast (conversion) | Right to left |
//| const_cast | Type cast (conversion) | None |
//| dynamic_cast | Type cast (conversion) | None |
//| reinterpret_cast | Type cast (conversion) | None |
//| static_cast | Type cast (conversion) | None |
//| .* | Apply pointer to class member (objects) | Left to right |
//| ->* | Dereference pointer to class member | Left to right |
//| * | Multiplication | Left to right |
//| / | Division | Left to right |
//| % | Remainder (modulus) | Left to right |
//| + | Addition | Left to right |
//| - | Subtraction | Left to right |
//| << | Left shift | Left to right |
//| >> | Right shift | Left to right |
//| < | Less than | Left to right |
//| > | Greater than | Left to right |
//| <= | Less than or equal to | Left to right |
//| >= | Greater than or equal to | Left to right |
//| == | Equality | Left to right |
//| != | Inequality | Left to right |
//| & | Bitwise AND | Left to right |
//| ^ | Bitwise exclusive OR | Left to right |
//| | | Bitwise OR | Left to right |
//| && | Logical AND | Left to right |
//| || | Logical OR | Left to right |
//| e1?e2:e3 | Conditional | Right to left |
//| = | Assignment | Right to left |
//| *= | Multiplication assignment | Right to left |
//| /= | Division assignment | Right to left |
//| %= | Modulus assignment | Right to left |
//| += | Addition assignment | Right to left |
//| -= | Subtraction assignment | Right to left |
//| <<= | Left-shift assignment | Right to left |
//| >>= | Right-shift assignment | Right to left |
//| &= | Bitwise AND assignment | Right to left |
//| |= | Bitwise inclusive OR assignment | Right to left |
//| ^= | Bitwise exclusive OR assignment | Right to left |
//| , | Comma | Left to right |
//+------------------+-----------------------------------------+---------------+
#3
#include <iostream>
#include <stack>
#include <queue>
#include <locale.h>
using namespace std;
typedef struct BiTNode {//二叉树结点
char data; //数据
struct BiTNode *lchild,*rchild; //左右孩子指针
} BiTNode,*BiTree;
int CreateBiTree(BiTree &T) {//按先序序列创建二叉树
char data;
scanf("%c",&data);//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
if (data == '#') {
T = NULL;
} else {
T = (BiTree)malloc(sizeof(BiTNode));
T->data = data; //生成根结点
CreateBiTree(T->lchild);//构造左子树
CreateBiTree(T->rchild);//构造右子树
}
return 0;
}
void Visit(BiTree T) {//输出
if (T->data != '#') {
printf("%c ",T->data);
}
}
void PreOrder(BiTree T) {//先序遍历
if (T != NULL) {
Visit(T); //访问根节点
PreOrder(T->lchild); //访问左子结点
PreOrder(T->rchild); //访问右子结点
}
}
void InOrder(BiTree T) {//中序遍历
if (T != NULL) {
InOrder(T->lchild); //访问左子结点
Visit(T); //访问根节点
InOrder(T->rchild); //访问右子结点
}
}
void PostOrder(BiTree T) {//后序遍历
if (T != NULL) {
PostOrder(T->lchild); //访问左子结点
PostOrder(T->rchild); //访问右子结点
Visit(T); //访问根节点
}
}
void PreOrder2(BiTree T) {//先序遍历(非递归)
//访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。
stack<BiTree> stack;
BiTree p = T;//p是遍历指针
while (p || !stack.empty()) { //栈不空或者p不空时循环
if (p != NULL) {
stack.push(p); //存入栈中
printf("%c ",p->data); //访问根节点
p = p->lchild; //遍历左子树
} else {
p = stack.top(); //退栈
stack.pop();
p = p->rchild; //访问右子树
}
}
}
void InOrder2(BiTree T) {//中序遍历(非递归)
//T是要遍历树的根指针,中序遍历要求在遍历完左子树后,访问根,再遍历右子树。
//先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,访问T->data,再中序遍历T的右子树。
stack<BiTree> stack;
BiTree p = T;//p是遍历指针
while (p || !stack.empty()) { //栈不空或者p不空时循环
if (p != NULL) {
stack.push(p); //存入栈中
p = p->lchild; //遍历左子树
} else {
p = stack.top(); //退栈,访问根节点
printf("%c ",p->data);
stack.pop();
p = p->rchild; //访问右子树
}
}
}
typedef struct BiTNodePost{
BiTree biTree;
char tag;
} BiTNodePost,*BiTreePost;
void PostOrder2(BiTree T) {//后序遍历(非递归)
stack<BiTreePost> stack;
BiTree p = T;//p是遍历指针
BiTreePost BT;
while (p != NULL || !stack.empty()) {//栈不空或者p不空时循环
while (p != NULL) {//遍历左子树
BT = (BiTreePost)malloc(sizeof(BiTNodePost));
BT->biTree = p;
BT->tag = 'L';//访问过左子树
stack.push(BT);
p = p->lchild;
}
while (!stack.empty() && (stack.top())->tag == 'R') {//左右子树访问完毕访问根节点
BT = stack.top();
stack.pop();//退栈
BT->biTree;
printf("%c ",BT->biTree->data);
}
if (!stack.empty()) {//遍历右子树
BT = stack.top();
BT->tag = 'R';//访问过右子树
p = BT->biTree;
p = p->rchild;
}
}
}
void LevelOrder(BiTree T) {//层次遍历
if (T == NULL) return;
BiTree p = T;
queue<BiTree> queue;//队列
queue.push(p);//根节点入队
while (!queue.empty()) { //队列不空循环
p = queue.front(); //对头元素出队
printf("%c ",p->data); //访问p指向的结点
queue.pop(); //退出队列
if (p->lchild != NULL) {//左子树不空,将左子树入队
queue.push(p->lchild);
}
if (p->rchild != NULL) {//右子树不空,将右子树入队
queue.push(p->rchild);
}
}
}
int main() {
BiTree T;
setlocale(LC_ALL,"chs");
CreateBiTree(T);
printf("先序遍历 :");PreOrder (T);printf("\n");
printf("先序遍历(非递归):");PreOrder2 (T);printf("\n");
printf("\n");
printf("中序遍历 :");InOrder (T);printf("\n");
printf("中序遍历(非递归):");InOrder2 (T);printf("\n");
printf("\n");
printf("后序遍历 :");PostOrder (T);printf("\n");
printf("后序遍历(非递归):");PostOrder2(T);printf("\n");
printf("\n");
printf("层次遍历 :");LevelOrder(T);printf("\n");
return 0;
}
//ABC##DE#G##F###
//先序遍历 :A B C D E G F
//先序遍历(非递归):A B C D E G F
//
//中序遍历 :C B E G D F A
//中序遍历(非递归):C B E G D F A
//
//后序遍历 :C G E F D B A
//后序遍历(非递归):C G E F D B A
//
//层次遍历 :A B C D E F G
//
/// A
/// /
/// B
/// / \
/// C D
/// / \
/// E F
/// \
/// G
#4
谢谢,原来如此!
#5
太强大了!非递归遍历搞得头大了。
#6
记不住优先级和结合方向时,多用用括号吧
#1
(BiTree)p->lchild
改为((BiTree)p)->lchild
改为((BiTree)p)->lchild
#2
//C++ Operators
// Operators specify an evaluation to be performed on one of the following:
// One operand (unary operator)
// Two operands (binary operator)
// Three operands (ternary operator)
// The C++ language includes all C operators and adds several new operators.
// Table 1.1 lists the operators available in Microsoft C++.
// Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators. Operators associate with either the
//expression on their left or the expression on their right; this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
// Table 1.1 shows the precedence and associativity of C++ operators
// (from highest to lowest precedence).
//
//Table 1.1 C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator | Name or Meaning | Associativity |
//+------------------+-----------------------------------------+---------------+
//| :: | Scope resolution | None |
//| :: | Global | None |
//| [ ] | Array subscript | Left to right |
//| ( ) | Function call | Left to right |
//| ( ) | Conversion | None |
//| . | Member selection (object) | Left to right |
//| -> | Member selection (pointer) | Left to right |
//| ++ | Postfix increment | None |
//| -- | Postfix decrement | None |
//| new | Allocate object | None |
//| delete | Deallocate object | None |
//| delete[ ] | Deallocate object | None |
//| ++ | Prefix increment | None |
//| -- | Prefix decrement | None |
//| * | Dereference | None |
//| & | Address-of | None |
//| + | Unary plus | None |
//| - | Arithmetic negation (unary) | None |
//| ! | Logical NOT | None |
//| ~ | Bitwise complement | None |
//| sizeof | Size of object | None |
//| sizeof ( ) | Size of type | None |
//| typeid( ) | type name | None |
//| (type) | Type cast (conversion) | Right to left |
//| const_cast | Type cast (conversion) | None |
//| dynamic_cast | Type cast (conversion) | None |
//| reinterpret_cast | Type cast (conversion) | None |
//| static_cast | Type cast (conversion) | None |
//| .* | Apply pointer to class member (objects) | Left to right |
//| ->* | Dereference pointer to class member | Left to right |
//| * | Multiplication | Left to right |
//| / | Division | Left to right |
//| % | Remainder (modulus) | Left to right |
//| + | Addition | Left to right |
//| - | Subtraction | Left to right |
//| << | Left shift | Left to right |
//| >> | Right shift | Left to right |
//| < | Less than | Left to right |
//| > | Greater than | Left to right |
//| <= | Less than or equal to | Left to right |
//| >= | Greater than or equal to | Left to right |
//| == | Equality | Left to right |
//| != | Inequality | Left to right |
//| & | Bitwise AND | Left to right |
//| ^ | Bitwise exclusive OR | Left to right |
//| | | Bitwise OR | Left to right |
//| && | Logical AND | Left to right |
//| || | Logical OR | Left to right |
//| e1?e2:e3 | Conditional | Right to left |
//| = | Assignment | Right to left |
//| *= | Multiplication assignment | Right to left |
//| /= | Division assignment | Right to left |
//| %= | Modulus assignment | Right to left |
//| += | Addition assignment | Right to left |
//| -= | Subtraction assignment | Right to left |
//| <<= | Left-shift assignment | Right to left |
//| >>= | Right-shift assignment | Right to left |
//| &= | Bitwise AND assignment | Right to left |
//| |= | Bitwise inclusive OR assignment | Right to left |
//| ^= | Bitwise exclusive OR assignment | Right to left |
//| , | Comma | Left to right |
//+------------------+-----------------------------------------+---------------+
#3
#include <iostream>
#include <stack>
#include <queue>
#include <locale.h>
using namespace std;
typedef struct BiTNode {//二叉树结点
char data; //数据
struct BiTNode *lchild,*rchild; //左右孩子指针
} BiTNode,*BiTree;
int CreateBiTree(BiTree &T) {//按先序序列创建二叉树
char data;
scanf("%c",&data);//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
if (data == '#') {
T = NULL;
} else {
T = (BiTree)malloc(sizeof(BiTNode));
T->data = data; //生成根结点
CreateBiTree(T->lchild);//构造左子树
CreateBiTree(T->rchild);//构造右子树
}
return 0;
}
void Visit(BiTree T) {//输出
if (T->data != '#') {
printf("%c ",T->data);
}
}
void PreOrder(BiTree T) {//先序遍历
if (T != NULL) {
Visit(T); //访问根节点
PreOrder(T->lchild); //访问左子结点
PreOrder(T->rchild); //访问右子结点
}
}
void InOrder(BiTree T) {//中序遍历
if (T != NULL) {
InOrder(T->lchild); //访问左子结点
Visit(T); //访问根节点
InOrder(T->rchild); //访问右子结点
}
}
void PostOrder(BiTree T) {//后序遍历
if (T != NULL) {
PostOrder(T->lchild); //访问左子结点
PostOrder(T->rchild); //访问右子结点
Visit(T); //访问根节点
}
}
void PreOrder2(BiTree T) {//先序遍历(非递归)
//访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。
stack<BiTree> stack;
BiTree p = T;//p是遍历指针
while (p || !stack.empty()) { //栈不空或者p不空时循环
if (p != NULL) {
stack.push(p); //存入栈中
printf("%c ",p->data); //访问根节点
p = p->lchild; //遍历左子树
} else {
p = stack.top(); //退栈
stack.pop();
p = p->rchild; //访问右子树
}
}
}
void InOrder2(BiTree T) {//中序遍历(非递归)
//T是要遍历树的根指针,中序遍历要求在遍历完左子树后,访问根,再遍历右子树。
//先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,访问T->data,再中序遍历T的右子树。
stack<BiTree> stack;
BiTree p = T;//p是遍历指针
while (p || !stack.empty()) { //栈不空或者p不空时循环
if (p != NULL) {
stack.push(p); //存入栈中
p = p->lchild; //遍历左子树
} else {
p = stack.top(); //退栈,访问根节点
printf("%c ",p->data);
stack.pop();
p = p->rchild; //访问右子树
}
}
}
typedef struct BiTNodePost{
BiTree biTree;
char tag;
} BiTNodePost,*BiTreePost;
void PostOrder2(BiTree T) {//后序遍历(非递归)
stack<BiTreePost> stack;
BiTree p = T;//p是遍历指针
BiTreePost BT;
while (p != NULL || !stack.empty()) {//栈不空或者p不空时循环
while (p != NULL) {//遍历左子树
BT = (BiTreePost)malloc(sizeof(BiTNodePost));
BT->biTree = p;
BT->tag = 'L';//访问过左子树
stack.push(BT);
p = p->lchild;
}
while (!stack.empty() && (stack.top())->tag == 'R') {//左右子树访问完毕访问根节点
BT = stack.top();
stack.pop();//退栈
BT->biTree;
printf("%c ",BT->biTree->data);
}
if (!stack.empty()) {//遍历右子树
BT = stack.top();
BT->tag = 'R';//访问过右子树
p = BT->biTree;
p = p->rchild;
}
}
}
void LevelOrder(BiTree T) {//层次遍历
if (T == NULL) return;
BiTree p = T;
queue<BiTree> queue;//队列
queue.push(p);//根节点入队
while (!queue.empty()) { //队列不空循环
p = queue.front(); //对头元素出队
printf("%c ",p->data); //访问p指向的结点
queue.pop(); //退出队列
if (p->lchild != NULL) {//左子树不空,将左子树入队
queue.push(p->lchild);
}
if (p->rchild != NULL) {//右子树不空,将右子树入队
queue.push(p->rchild);
}
}
}
int main() {
BiTree T;
setlocale(LC_ALL,"chs");
CreateBiTree(T);
printf("先序遍历 :");PreOrder (T);printf("\n");
printf("先序遍历(非递归):");PreOrder2 (T);printf("\n");
printf("\n");
printf("中序遍历 :");InOrder (T);printf("\n");
printf("中序遍历(非递归):");InOrder2 (T);printf("\n");
printf("\n");
printf("后序遍历 :");PostOrder (T);printf("\n");
printf("后序遍历(非递归):");PostOrder2(T);printf("\n");
printf("\n");
printf("层次遍历 :");LevelOrder(T);printf("\n");
return 0;
}
//ABC##DE#G##F###
//先序遍历 :A B C D E G F
//先序遍历(非递归):A B C D E G F
//
//中序遍历 :C B E G D F A
//中序遍历(非递归):C B E G D F A
//
//后序遍历 :C G E F D B A
//后序遍历(非递归):C G E F D B A
//
//层次遍历 :A B C D E F G
//
/// A
/// /
/// B
/// / \
/// C D
/// / \
/// E F
/// \
/// G
#4
谢谢,原来如此!
#5
太强大了!非递归遍历搞得头大了。
#6
记不住优先级和结合方向时,多用用括号吧