编程实现以上二叉树中序遍历操作,输出遍历序列,求写代码~~ 用C语言编程实现二叉树的中序遍历算法
\u6c42\u95ee\u4e2aC\u8bed\u8a00\u95ee\u9898 \u5df2\u77e5\u4e8c\u53c9\u6811\u904d\u5386\u7684\u4e2d\u5e8f\u5e8f\u5217\u548c\u540e\u5e8f\u5e8f\u5217 \u8f93\u51fa\u5148\u5e8f\u5e8f\u5217 \u6c42\u4ee3\u7801#include
#include
#define MAX 50
using namespace std;
typedef char Elem_Type;
typedef struct BiTree
{
Elem_Type data;//\u6570\u636e
struct BiTree *Lchild;//\u5de6\u5b69\u5b50
struct BiTree *Rchild;//\u53f3\u5b69\u5b50
}BiTree;
//\u8981\u67e5\u627e\u7684\u5143\u7d20 \u67e5\u627e\u7684\u5730\u65b9 \u6570\u7ec4\u7684\u957f\u5ea6
int Search_Num(Elem_Type num, Elem_Type *array, int len)
{
for (int i = 0; i<len; i++)
if (array[i] == num)
return i;
//return -1;//\u6ca1\u6709\u627e\u5230
} //\u524d\u5e8f\u7ed3\u679c\u6570\u7ec4 \u4e2d\u5e8f\u7ed3\u679c\u6570\u7ec4 \u6570\u7ec4\u957f\u5ea6
//\u6839\u636e\u4e2d\u540e\u5e8f\u751f\u6210\u4e8c\u6811
BiTree *Resume_BiTree(Elem_Type *post, Elem_Type *center, int len)
{
if (len <= 0)
return NULL;
BiTree *temp = new BiTree;
temp->data = post[len - 1];//\u540e\u5e8f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5373\u4e3a\u6839\u5143\u7d20
int index = Search_Num(temp->data, center, len);
//\u904d\u5386\u5de6\u5b69\u5b50 \uff08\u540e\u5e8f\u7ed3\u679c\u4e2d\uff0c\u4ece\u7b2c0\u4e2a\u5143\u7d20\u5f00\u59cb\u5230\u7b2cindex-1\u4e2a\u5143\u7d20\u90fd\u662f\u5de6\u5b50\u6811\uff09
temp->Lchild = Resume_BiTree(post, center, index);
//\u904d\u5386\u53f3\u5b69\u5b50 \uff08\u540e\u5e8f\u7ed3\u679c\u4e2d\uff0c\u4ece\u7b2cindex\u4e2a\u5143\u7d20\u5f00\u59cb\u5230\u7b2cLen-2\u4e2a\u5143\u7d20\u90fd\u662f\u53f3\u5b50\u6811(\u4ece0\u8ba1\u6570)\uff09
temp->Rchild = Resume_BiTree(post + index, center + index + 1, len - index - 1);
return temp;
}
void PreOrderTraverse(BiTree *root)//\u524d\u5e8f\u904d\u5386\u8f93\u51fa
{
if (root != NULL)
{
cout data;
PreOrderTraverse(root->Lchild);
PreOrderTraverse(root->Rchild);
}
}
int main(void)
{
Elem_Type *postorder = new Elem_Type[MAX];//\u540e\u5e8f\u6570\u7ec4
Elem_Type *inorder = new Elem_Type[MAX];//\u4e2d\u5e8f\u524d\u5e8f\u6570\u7ec4
cin >> postorder; //\u5148\u8f93\u5165\u540e\u5e8f
cin >> inorder; //\u518d\u8f93\u5165\u524d\u5e8f
BiTree * root = Resume_BiTree(postorder, inorder, strlen(inorder));//\u7531\u4e8e\u672c\u4f8b\u7ed3\u6784\u5143\u7d20\u4e3achar\uff0c\u6240\u4ee5\u53ef\u4ee5\u7528strlen\u6765\u83b7\u5143\u7d20\u4e2a\u6570\uff0c\u5176\u4ed6\u60c5\u51b5\u53ef\u673a\u53d8
PreOrderTraverse(root);
cout << endl;
delete[] postorder;//\u91ca\u653e\u5185\u5b58
delete[] inorder;
return 0;
}
#include
#include
struct BiTNode *stack[100];
struct BiTNode//\u5b9a\u4e49\u7ed3\u6784\u4f53
{
char data;
struct BiTNode *lchild,*rchild;
};
void later(struct BiTNode *&p) //\u524d\u5e8f\u521b\u5efa\u6811
{
char ch;
scanf("%c",&ch);
if(ch==' ')
p=NULL;
else
{
p=(struct BiTNode *)malloc(sizeof(struct BiTNode));
p->data=ch;
later(p->lchild);
later(p->rchild);
}
}
void print(struct BiTNode *p) //\u524d\u5e8f\u904d\u5386\uff08\u8f93\u51fa\u4e8c\u53c9\u6811\uff09
{
int i=-1;
while(1)
{
while(p!=NULL)
{
stack[++i]=p->rchild;/*printf("ok\uff1f\n");*/
printf("%c",p->data);
p=p->lchild;
}
if(i!=-1)
{
p=stack[i];
i--;
}
else
return;
}
}
void main()//\u4e3b\u51fd\u6570
{
struct BiTNode *p,*t;
later(p);
print(p);
}
#include<stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef enum {Link,Thread} PointerTag;
typedef struct BiThrNode
{
TElemType data;
struct BiThrNode *lchild,*rchild;
PointerTag LTag,RTag;
}BiThrNode;
typedef BiThrNode *BiThrTree;
BiTree CreateBiTree(BiTree T) //先序遍历构造二叉树
{
char ch;
scanf("%c",&ch);
if(ch=='#') //#代表空指针
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode)); //申请结点
if(!T)
exit(OVERFLOW);
T->data=ch; //生成根结点
T->lchild=CreateBiTree(T->lchild); //构造左子树
T->rchild=CreateBiTree(T->rchild); //构造右子树
}
return T;
}
Status InOrderTraverse(BiTree T,Status(*visit)(TElemType))
{//中序遍历二叉树
if(T)
{
if(InOrderTraverse(T->lchild,visit))
if(visit(T->data))
if(InOrderTraverse(T->rchild,visit))
return OK;
return ERROR;
}
else
return OK;
}
Status PrintElement(TElemType e)
{
printf("%-2c",e);
return OK;
}
void main()
{
BiTree T=NULL;
printf("请输入构造二叉树的字符序列:");
T=CreateBiTree(T);
if(T)
printf("二叉树建立成功!
");
else
printf("二叉树构造失败!!!
");
printf("中序遍历二叉树:");
InOrderTraverse(T,PrintElement);
printf("
");
}
#include <iostream>
#define MAXSIZE 100
using namespace std;
typedef char TElemType;
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
bool InOrderTraverse1(BiTree T) {
if(T) {
cout<<T->data;
InOrderTraverse1(T->lchild);
InOrderTraverse1(T->rchild);
}
}
bool InOrderTraverse2(BiTree T) {
if(T) {
InOrderTraverse2(T->lchild);
cout<<T->data;
InOrderTraverse2(T->rchild);
}
}
bool InOrderTraverse3(BiTree T) {
if(T) {
InOrderTraverse3(T->lchild);
InOrderTraverse3(T->rchild);
cout<<T->data;
}
}
bool CreateBiTree(BiTree &T) {
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else {
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int Depth(BiTree T) {
int n,m;
if(T==NULL)
return 0;
else {
m=Depth(T->lchild);
n=Depth(T->rchild);
if(m>n)
return(m+1);
else
return(n+1);
}
}
int NodeCount(BiTree T) {
if(T==NULL)
return 0;
else
return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}
int main()
{
BiTree a;
int height,num;
cout<<"请输入二叉树的元素:\n";
CreateBiTree(a);
cout<<"先序遍历法输出:\n";
InOrderTraverse1(a);
cout<<"\n中序遍历法输出:\n";
InOrderTraverse2(a);
cout<<"\n后序遍历法输出:\n";
InOrderTraverse3(a);
height=Depth(a);
num=NodeCount(a);
cout<<"\n该二叉树深度为:";
cout<<height<<endl;
cout<<"该二叉树结点数为:";
cout<<num;
return 0;
}
绛旓細BiTree CreateBiTree(BiTree T) //鍏搴忛亶鍘鏋勯浜屽弶鏍{ char ch; scanf("%c",&ch); if(ch=='#') //#浠h〃绌烘寚閽 T=NULL; else { T=(BiTNode *)malloc(sizeof(BiTNode)); //鐢宠缁撶偣 if(!T) exit(OVERFLOW); T->data=ch; //鐢熸垚鏍圭粨鐐 T->lchild=CreateBiTree(T->lchild); //鏋勯犲乏瀛...
绛旓細涓簭閬嶅巻鐨勬楠ゆ槸锛氶鍏堥掑綊閬嶅巻宸﹀瓙鏍戯紝鐒跺悗璁块棶鏍硅妭鐐癸紝鏈鍚庨亶鍘嗗彸瀛愭爲銆傚湪璁块棶姣忎釜鑺傜偣鏃讹紝鍙互杩涜鐩稿叧鎿嶄綔锛屽鏄剧ず鑺傜偣鍊笺備笅闈㈤氳繃涓涓疄渚嬫潵鐩磋璇存槑锛氬亣璁炬垜浠湁涓涓簩鍙夋爲锛屽叾涓簭閬嶅巻杩囩▼浼氱敓鎴愮壒瀹氱殑鑺傜偣搴忓垪銆侰璇█涓紝浜屽弶鏍戠殑涓簭閬嶅巻鍙互浣跨敤閫掑綊鏂规硶瀹炵幇锛岄鍏堝垵濮嬪寲鏍戝舰缁撴瀯锛岀劧鍚庤繍琛屼唬鐮侊紝...
绛旓細1銆侀潪閫掑綊鍓嶅簭閬嶅巻 鍙h瘈锛氭牴宸﹀彸銆傚墠搴忛亶鍘嗛鍏堣闂牴缁撶偣锛岀劧鍚庨亶鍘嗗乏瀛愭爲锛屾渶鍚庨亶鍘嗗彸瀛愭爲銆傚湪閬嶅巻宸︺佸彸瀛愭爲鏃讹紝浠嶇劧鍏堣闂牴鑺傜偣锛岀劧鍚庨亶鍘嗗乏瀛愭爲锛屾渶鍚庨亶鍘嗗彸瀛愭爲銆1.1 鍏蜂綋娴佺▼ 1.2 鍏蜂綋浠g爜 2銆侀潪閫掑綊涓簭閬嶅巻 涓簭閬嶅巻鏄滃乏鏍瑰彸"锛屽嵆鍏堥亶鍘嗗乏瀛愭爲鑺傜偣锛屽啀閬嶅巻鏍硅妭鐐癸紝鍐嶉亶鍘嗗彸瀛愭爲鑺傜偣 2...
绛旓細CreateBiTree(BiTree &T){//鎸夊厛搴娆″簭杈撳叆锛屾瀯閫犱簩鍙夐摼琛ㄨ〃绀鐨勪簩鍙夋爲T锛岀┖鏍艰〃绀虹┖鏍 // if(T)return;char ch;ch=getchar();//涓嶈兘鐢╟in鏉ヨ緭鍏ワ紝鍦╟in涓笉鑳借瘑鍒┖鏍笺俰f(ch=='')T=NULL;else{ if(!(T=(BTNode )malloc(sizeof(BTNode)))cout<<"malloc fail!";T->data=ch;Create...
绛旓細瀹炵幇鍔熻兘锛氣憼 鏍戠殑鏋勯犫憽 閫掑綊瀹炵幇鍏堝簭閬嶅巻銆佷腑搴忛亶鍘嗐佸悗搴忛亶鍘嗏憿 鍫嗘爤瀹炵幇鍏堝簭閬嶅巻銆佷腑搴忛亶鍘嗐佸悗搴忛亶鍘嗏懀 闃熷垪瀹炵幇灞傛閬嶅巻#coding=utf-8class Node(object): """鑺傜偣绫""" def init(self, elem=-1, lchild=None, rchild=None): self.elem = elem self.lchild = lchild self.rchild = ...
绛旓細鍚搴忛亶鍘锛氬厛閬嶅巻宸﹀瓙鏍戯紝鍐嶉亶鍘嗗彸瀛愭爲锛屾渶鍚庤緭鍑虹埗鑺傜偣銆傚鍥炬墍绀虹殑浜屽弶鏍锛屽畠鐨勫墠涓悗杈撳嚭椤哄簭鍒嗗埆灏辨槸锛氬墠搴忥細1鏄撳ぇ甯堛2瀵掑啺灏勬墜銆3鐩插儳銆4鐩栦鸡 涓簭锛2瀵掑啺灏勬墜銆1鏄撳ぇ甯堛3鐩插儳銆4鐩栦鸡 鍚庡簭锛2瀵掑啺灏勬墜銆4鐩栦鸡銆3鐩插儳銆1鏄撳ぇ甯 浜屻佷唬鐮瀹炵幇鍓嶃佷腑銆佸悗搴忛亶鍘 瀹炵幇鎬濊矾寰堢畝鍗曪細1銆佸垱寤鸿嫳闆...
绛旓細鏁版嵁缁撴瀯鐨勫熀纭鐭ヨ瘑涓噸瑕佺殑涓鐐瑰氨鏄兘鍚︽牴鎹袱绉嶄笉鍚閬嶅巻搴鍒楃殑缁勫悎(鏈変笁绉:鍏堝簭+涓簭,鍏堝簭+鍚庡簭,涓簭+鍚庡簭),鍞竴鐨勭‘瀹氫竴妫典簩鍙夋爲銆傜劧鍚庡氨鏄牴鎹浜屽弶鏍戠殑涓嶅悓閬嶅巻搴忓垪(鍏堝簭銆佷腑搴忋佸悗搴),閲嶆瀯浜屽弶鏍戙傛樉鐒,杩欎笁绉嶇粍鍚堝苟涓嶆槸閮借兘鍞竴纭畾浜屽弶鏍戠殑,鍏朵腑鍏堝簭+鍚庡簭灏变笉鑳藉敮涓纭畾涓妫典簩鍙夋爲,涓嬮潰鏄叧浜庤...
绛旓細浠ヤ笅涓浜屽弶鏌ユ壘鏍戯紙涓簭閬嶅巻锛夊叾瀹冪殑妤间富搴旇鏄兘鍐欎簡鍚э細typedef int T;class Bst{ struct Node{ T data;Node* L;Node* R;Node(const T& d):data(d),L(0),R(0){} };typedef Node* TreeRoot;Node* root;int len;public:Bst():root(NULL),len(0){} ~Bst(){Clear(root);} bool...
绛旓細浜屽弶鏍 鐨褰㈢姸锛氶偅涔堬紝鏍规嵁鍚搴忕殑閬嶅巻瑙勫垯锛屾垜浠彲浠ョ煡閬擄紝鍚搴忛亶鍘椤哄簭涓猴細AEFDHZMG 浜屽弶鏍戠殑涓浜涗粙缁嶏細鍦ㄨ绠楁満绉戝涓紝浜屽弶鏍戞槸姣忎釜鑺傜偣鏈澶氭湁涓や釜瀛愭爲鐨 鏍戠粨鏋 銆傞氬父瀛愭爲琚О浣溾滃乏瀛愭爲鈥濓紙left subtree锛夊拰鈥滃彸瀛愭爲鈥濓紙right subtree锛夈備簩鍙夋爲甯歌鐢ㄤ簬瀹炵幇 浜屽弶鏌ユ壘鏍 鍜 浜屽弶鍫 銆備簩鍙夋爲鐨...
绛旓細/* 涓簭閬嶅巻浜屽弶鎺掑簭鏍戠殑缁撶偣 */ extern void PrintAllNodeMid(CBSTree);/* 鍚庡簭鏂瑰紡閲婃斁缁撶偣 */ extern void FreeBSTree(CBSTree);endif BinarySortTreeADT.cpp include "BinarySortTreeADT.h"include <stdlib.h> include <stdio.h> /* 鍚戜簩鍙夋帓搴鏍戜腑鍔犲叆涓涓粨鐐 */ int InsertNode(CBSTree...