树的创建,与基本操作
来源:互联网 发布:des密码算法 编辑:程序博客网 时间:2024/06/06 09:32
#include<iostream>using namespace std;typedef struct BTNODE{char data;BTNODE * left;BTNODE * right;}BTNODE;typedef struct Stack{BTNODE * data[100];int top;}Stack;void createBtTree(BTNODE * &tree,char * str){Stack stack;BTNODE *p;stack.top=-1;int k,j=0;char ch=str[0];tree=NULL;while(ch!='\0'){switch(ch){case '(':stack.top++;stack.data[stack.top]=p;k=1;break;case ',':k=2;break;case ')':stack.top--;default:p=(BTNODE *)malloc(sizeof(BTNODE));p->data=ch;p->left=NULL;p->right=NULL;if(tree==NULL)tree=p;else{switch(k){case 1:stack.data[stack.top]->left=p;break;case 2:stack.data[stack.top]->right=p;break;}}}j++;ch=str[j];}}void displayTree(BTNODE * tree){if(tree==NULL)cout<<"empty tree"<<endl;else{cout<<tree->data;if(tree->left!=NULL||tree->right!=NULL){cout<<"(";displayTree(tree->left);cout<<",";displayTree(tree->right);cout<<")";}}}BTNODE * findNode(BTNODE * tree,char x){BTNODE *p;if(tree==NULL)return NULL;if(tree->data==x)return tree;else{p=findNode(tree->left,x);if(p==NULL)p=findNode(tree->right,x);elsereturn p;}}int deepth(BTNODE * tree){if(tree==NULL)return 0;int m,n;m=deepth(tree->left)+1;n=deepth(tree->right)+1;return (m>n)?m:n;}int main(){char str[50];cin>>str;BTNODE * tree;createBtTree(tree,str);//displayTree(tree);//BTNODE * node=findNode(tree,'c');cout<<deepth(tree)<<endl;return 0;}
0 0
- 树的创建,与基本操作
- 二叉树的创建及基本操作。
- Swift学习记录 -- 15.tableView的创建与基本操作
- pandas入门——数据的创建与基本操作
- 创建表的基本操作
- 树的建立与基本操作
- 二叉树的建立与基本操作
- 树的建立与基本操作
- 9. 树的建立与基本操作
- 二叉树的定义与基本操作
- 二叉树的创建及一些基本操作
- 二叉树的创建与相关操作
- Oracle - 基本操作之查询与创建操作
- 线程的创建、终止、等待基本操作
- 【mysql】表的创建以及基本操作
- 数据表的基本操作之创建数据表
- mongoDB的基本操作之创建索引
- cuda -创建流的基本操作
- 通过4412提供的函数控制蜂鸣器和流水灯
- Javascript学习之bind的模拟实现
- Objective-C Category 的实现原理
- 开发中遇到的一些问题的解决方案
- C语言之数据结构
- 树的创建,与基本操作
- Linear Regression
- 用HTML5做2048游戏(一)
- RecyclerView的万能适配器
- bzoj1558: [JSOI2009]等差数列
- 复习笔记-局部与成员变量
- unity使用Zxing的.net库搞二维码与条形码的事
- Django使用Mysql数据库已经存在的数据表
- 第一天的改变