-
梦里花落0921
说明:输入时按前序遍历方式依次输入各节点值,默认的结束符为0。即当一个节点为叶子节点时,把它的左子节点和右子节点都输为0,当然你可以自己修改为加别的值。例如某棵树的形状如下: A / \ B C / \ \ D E F 则按如下输入:ABD00E00C0F00。 程序运行后结果如下: 前序遍历结果: ABDECF 中序遍历结果: DBEACF 程序源文件: #include <stdio.h> #include <stdlib.h> struct treeNode { char data; struct treeNode *lchild; struct treeNode *rchild; }; //定义一个节点 class binaryTree { private: struct treeNode *T; public: binaryTree() {T = NULL;}; //构造函数 int createTree(); //创建树 int preTravel(); //先序遍历树 int inTravel(); //中序遍历树 }; struct treeNode * createBT(struct treeNode *bt, int k) { char b; struct treeNode *p, *t; b = getchar(); if (b != '0') { p = (struct treeNode *)malloc(sizeof(struct treeNode)); p->data = b; p->lchild = NULL; p->rchild = NULL; if (k == 0) t = p; if (k == 1) bt->lchild = p; if (k == 2) bt->rchild = p; createBT(p, 1); createBT(p, 2); } return t; } void preTravelBT(struct treeNode *bt) { if (bt != NULL) { putchar(bt->data); preTravelBT(bt->lchild); preTravelBT(bt->rchild); } } void inTravelBT(struct treeNode *bt) { if (bt != NULL) { inTravelBT(bt->lchild); putchar(bt->data); inTravelBT(bt->rchild); } } int binaryTree::createTree() { printf("请输入各节点数据:\n"); T = createBT(T, 0); return 0; } int binaryTree::preTravel() { printf("前序遍历结果:\n"); preTravelBT(T); printf("\n"); return 0; } int binaryTree::inTravel() { printf("中序遍历结果:\n"); inTravelBT(T); printf("\n"); return 0; } void main() { binaryTree MyTree; MyTree.createTree(); MyTree.preTravel(); MyTree.inTravel(); }
-
神不在的星期二
void PreOrder_Nonrecursive(Bitree T)//先序遍历二叉树的非递归算法 {//思路为利用自己的堆栈模拟函数递归调用时栈区的变化。 InitStack(S);//初始化堆栈。参照严蔚敏编《数据结构C语言版》实现堆栈的相关功能函数,这里会用到。 Push(S,T); //根指针进栈 while(!StackEmpty(S)) { while(Gettop(S,p)&&p)//若栈顶元素不是空指针 { visit(p->data);//访问 push(S,p->lchild);//进入左子树 } //向左走到尽头 pop(S,p);将栈顶元素(实际上是空指针)丢弃 if(!StackEmpty(S)) { pop(S,p); push(S,p->rchild); //向右一步,从这个结点开始继续进行先序遍历 } }//while }//PreOrder_Nonrecursive int NodeCount_BiTree(Bitree T)//求二叉树中结点的数目 { if(!T) return 0; //递归结束条件:空树结点数为0 else return (NodeCount(T->lchild)+NodeCount(T->rchild)+1);//二叉树中结点数=左子树的结点数+右子树的结点数+根结点自己 }//NodeCount_BiTree int LeafCount_BiTree(Bitree T)//求二叉树中叶子结点的数目 { if(!T) return 0; //空树没有叶子 else if(!T->lchild&&!T->rchild) return 1; //叶子结点 else return Leaf_Count(T->lchild)+Leaf_Count(T->rchild);//左子树的叶子数加上右子树的叶子数 }//LeafCount_BiTree 若已知二叉树的结点数为n,叶子的结点数为n0,那么n2=n0-1,n1=n-n2-n0=n-2n0+1。或者用以下算法 int n1=0;//定义全局变量n1 void Count_n1(Bitree T) {//先序遍历二叉树过程中统计度为1的结点数 if(T) { if((T->lchild && !T->rchild) || (!T->lchild && T->rchild))//判断是否是度为1的结点,实际上是异或 n1++; Count_n1(T->lchild); Count_n1(T->rchild); } } int Get_Depth(Bitree T)//求二叉树深度的递归算法 { if(!T) return 0;//递归结束条件:空树深度为0 else { m=Get_Depth(T->lchild); n=Get_Depth(T->rchild); return (m>n?m:n)+1;//二叉树深度为其左右子树较大深度+1 } }//Get_Depth
-
撒科打诨
/** main.cpp** Created on: 2008-12-1* Author: Admin*/#include<iostream>using std::cout;using std::cin;struct treeNode{char data;treeNode *lchild,*rchild;}; //定义一个节点class binaryTree{private:treeNode *TreeRoot;void subCreate(treeNode *,int,const char *);void Free(treeNode *);void subpreTravel(treeNode *);void subinTravel(treeNode *);public:binaryTree(){TreeRoot=new treeNode;}//构造函数void createTree();//创建树void preTravel(){subpreTravel(TreeRoot);cout<<"\n";}//先序遍历树void inTravel(){subinTravel(TreeRoot);cout<<"\n";}//中序遍历树~binaryTree(){Free(TreeRoot);}};void binaryTree::subCreate(treeNode * T,int root,const char * strNode){static int longth=strlen(strNode);int R=2*(root+1);int L=2*(root+1)-1;if(root ==0)T->data=strNode[0];if(L<longth){T->lchild=new treeNode;T->lchild->data=strNode[L];subCreate(T->lchild,L,strNode);}elseT->lchild=NULL;if(R<longth){T->rchild=new treeNode;T->rchild->data=strNode[R];subCreate(T->rchild,R,strNode);}elseT->rchild=NULL;}void binaryTree::createTree(){subCreate(this->TreeRoot,0,"abcdefghijk");}void binaryTree::subpreTravel(treeNode * T){if(T==NULL)return;cout<<T->data;subpreTravel(T->lchild);subpreTravel(T->rchild);}void binaryTree::subinTravel(treeNode * T){if(T==NULL)return;subinTravel(T->lchild);cout<<T->data;subinTravel(T->rchild);}void binaryTree::Free(treeNode * T){if(T==NULL)return;if(T->lchild==NULL&&T->rchild==NULL)delete T;else{Free(T->lchild);Free(T->rchild);}}int main(){binaryTree BT;BT.createTree();BT.preTravel();BT.inTravel();return 0;}//用二叉堆的方式建树