binary tree
2014-12-01 20:52
309 查看
//BTree_Ergodic:main.cpp 二叉树遍历算法练习 //time: 2014.12.01 19:10 //author: chaolee /*********************************************************************************/ //头文件包含处 #include <stdio.h> #include <stdlib.h> #include <string.h> /*********************************************************************************/ //自定义数据类型声明处 //定义节点最大个数 #define MaxNode 20 //节点数据类型 typedef char ElemType; //二叉树节点类型 typedef struct btnode{ ElemType data; btnode * lchild; btnode * rchild; }BtNode; //二叉树栈 typedef struct{ BtNode * stack[MaxNode]; //栈区 int top; //栈顶指针 }BTreeStack; /*********************************************************************************/ //自定义函数声明处 //从括号表述法创建二叉树(使用栈进行实现) bool CreateBTreefromBracketNotation(BtNode * &r,char * btStr){ BTreeStack bts; bts.top=-1; //初始化栈 int i=0,k=0; //i用于保存串的偏移量,k用于标记处理栈顶的左指针还是右指针 char ch; //ch用于保存括号表示法串的字符 BtNode * temp=NULL; //temp用于保存待处理的节点 while((ch=*(btStr+i))!='\0') //循环扫描串,并将字符存到ch { switch(ch){ case '(': //将子树根节点进栈,准备处理根节点左指针 bts.stack[++bts.top]=temp; k=1; break; case ')': //子树根节点出栈 bts.top--; break; case ',': //准备处理子树根节点右指针 k=2; break; default: //处理数据节点 temp=(BtNode *)malloc(sizeof(BtNode)); temp->data=ch; temp->lchild=temp->rchild=NULL; switch(k){ case 0: r=temp; break; case 1: bts.stack[bts.top]->lchild=temp; break; case 2: bts.stack[bts.top]->rchild=temp; break; } break; } i++; } return true; } //根据括号表示法计算子树的长度 int FindLenofSon(char * s,int Len){ char ch; //ch用于保存单位串 int i=0,match=0; //i用于记录偏移长度,match用于保存状态 if(*(s+1)==')'||*(s+1)==',') return 1; for(i=1;i<Len;i++){ ch=*(s+i); if(ch=='(') match++; else if(ch==')'){ match--; if(match==0) break; } } return i+1; } //从括号表述法创建二叉树(递归实现) void CreateBTreefromBracketNotation2(BtNode * &r,char * btStr, int len){ if(len<=0) return; r=(BtNode *)malloc(sizeof(BtNode)); r->data=*btStr; int offset1=FindLenofSon(btStr+2,len-2); CreateBTreefromBracketNotation2(r->lchild,btStr+2,offset1); if(len-3-offset1<=0) return; else{ int offset2=FindLenofSon(btStr+offset1,len-2-offset1); CreateBTreefromBracketNotation2(r->rchild,btStr+2+offset1,offset2); } } /*********************************************************************************/ //主函数 int main() { char * BracketNotationStr="a(b(d,e))"; BtNode * root=NULL,* root2=NULL; CreateBTreefromBracketNotation(root,BracketNotationStr); CreateBTreefromBracketNotation2(root2,BracketNotationStr,strlen(BracketNotationStr)); system("PAUSE"); return 0; } /************************************/ //说明: //在本例中括号表示法如果右子树不存在则不需要打逗号 /************************************/
相关文章推荐
- poj 2499 Binary Tree
- 数据结构:线索二叉树(Threaded Binary Tree)
- poj 2499 Binary Tree(水题)
- POJ 2499 Binary Tree 数学题解
- 公共祖先--binary tree
- [算法专题] Binary Tree
- 57. Minimum Depth of Binary Tree
- need cut branch 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【leetcode】104. Maximum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- Binary Tree
- LeetCode oj 226. Invert Binary Tree (DFS||BFS)
- 226. Invert Binary Tree
- Binary Tree
- 104. Maximum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- Leetcode-标签为Tree 111. Minimum Depth of Binary Tree
- hdu 5573---Binary Tree(构造)
- LeetCode 104. Maximum Depth of Binary Tree