通过输入字符串来构建二叉树

对二叉树的一系列操作都是建立在先将二叉树构造出来的前提上。大四考研的某天早上偷偷躲在宿舍敲二叉树的代码,也是醉醉的。学习就应该趁年轻,老了就学不动了。

首先是对二叉树的节点的一个声明:

typedef struct BTree{
    char str;
    struct BTree * lchild;
    struct BTree * rchild;
}BTree;

  然后我是打算用递归外加先序的方式对二叉树进行构建的,也就对输入字符串提出一个要求:

printf("Please input the tree(use char and #)\n要求按照先序遍历的方式输入,加上#进行区分\n例如123# #4##5##:\n");

构建二叉树的函数:

BTree* creat_tree(){
    BTree* temp;
    while(*p==‘ ‘)p++;
    if(*p==‘#‘){
        p++;
        return NULL;
    }
    if(*p!=‘ ‘){
        temp = (BTree *)malloc(sizeof(BTree));
        temp->str=*p++;
        temp->lchild=creat_tree();
        temp->rchild=creat_tree();
    }
    return temp;
}

  同时为了将头结点单独获取保存,供后边操作使用,因此将头结点的构建单独放在main函数中处理了一下,下边是我的完整代码,运行无误。

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

char * p;
typedef struct BTree{
    char str;
    struct BTree * lchild;
    struct BTree * rchild;
}BTree;

BTree* creat_tree(){
    BTree* temp;
    while(*p==‘ ‘)p++;
    if(*p==‘#‘){
        p++;
        return NULL;
    }
    if(*p!=‘ ‘){
        temp = (BTree *)malloc(sizeof(BTree));
        temp->str=*p++;
        temp->lchild=creat_tree();
        temp->rchild=creat_tree();
    }
    return temp;
}

void pre_visit(BTree* node){
    printf("%c",node->str);
    if(node->lchild!=NULL)pre_visit(node->lchild);
    if(node->rchild!=NULL)pre_visit(node->rchild);
}
int main()
{
    char tree[MAX];p=tree;
    BTree * head;
    printf("Please input the tree(use char and #)\n要求按照先序遍历的方式输入,加上#进行区分\n例如123# #4##5##:\n");
    //scanf("%s",tree);
    gets(tree);

    if(*p!=‘\0‘&&*p!=‘ ‘&&*p!=‘#‘){
        head=(BTree *)malloc(sizeof(BTree));
        head->str=*p++;
        //printf("head is %c",head->str);
        head->lchild=creat_tree();
        head->rchild=creat_tree();
    }

    printf("tree is :\n");
    pre_visit(head);
    return 0;
}

  

时间: 2024-08-06 03:43:30

通过输入字符串来构建二叉树的相关文章

536. Construct Binary Tree from String 从括号字符串中构建二叉树

[抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's valu

Tree Recovery(由先、中序列构建二叉树)

题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her cr

c++ 先序构建二叉树

二叉树首先要解决构建问题,才能考虑后续的遍历,这里贴出通过先序构建二叉树,同时包含四种二叉树的遍历方法(先序,中序,后序,逐层) 第一.定义BinaryTreeNode 类 1 #include <iostream> 2 #include <string> 3 #include <queue> 4 using namespace std; 5 6 template<typename T >class BinaryTree; 7 template <ty

(树)根据中序后序构建二叉树

题目:根据中序和后序遍历构建二叉树 思路:利用递归加上分治的思想.先找到根节点的值,然后在根据中序遍历找到根节点的左右两边的值,然后在递归的处理左右两边的左右子树.这里的关键在于怎么处理递归的左右子树的范围,代码里面详细解释 代码: class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { vector<int>::size_t

根据先序和中序序列构建二叉树

说明: 本次实验利用中序和先序序列,采用递归方式来构建二叉树 . 经过几天的失败和思考,我认为递归构建二叉树的过程中最重要的是递归单元,最麻烦的是递归参数的选择和传递. 简单将算法过程用如下流程图来表示:(本帖所用算法及图片均为原创内容,转贴注明出处) 算法:1.根据先序序列,建立根结点T 2.寻找中序序列的根结点位置,并据此位置计算左子树和右子树的区间 3.判断l_start和r_end是否相等,相等则表示只有一个根结点,设置其左右孩子结点为空并结束这一层:若不相等则继续下面步骤: 4.根据2

Jenkins 输入字符串给newLISP 作为参数使用问题

比如 需要生成sqoop import语句,用newlisp脚本: (set 'import-cmd (format "%s/sqoop-import --options-file media_options.txt --table %s --where \"ID = 2\" --target-dir %s -m 1 --fields-terminated-by '%s' --lines-terminated-by '\\n'" sqoop-path table-n

查找输入字符串中出现字符次数最多的那个字和重复次数

public class chongfu { //找出字符串中重复次数最多的那个字符: /** * public static void main(String[] args){ System.out.println("请输入字符串"); Scanner a = new Scanner(System.in); String b=a.nextLine(); int count=0; int d=0; char t = 0; for(int i=0;i<b.length();i++)

先序遍历和后序遍历构建二叉树

递归的方法利用先序遍历和中序遍历构建二叉树,同样也可以利用到中序遍历和后序遍历构建二叉树. //利用先序遍历和中序遍历构建二叉树 TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { TreeNode *root=NULL; if(preorder.size()==0||inorder.size()==0||preorder.size()!=inorder.size()) return

【字符串处理算法】将输入字符串中的各个单词反序的算法设计及C代码实现

一.需求描述 输入一个字符串,编写程序将该字符串中的各个单词反序拼装并输出.例如,如果输入的字符串是"Hello, how do you do",那么输出的字符串为"do you do how Hello,".注意保留各个单词之间的空格及相应的标点符号. 二.算法设计 通过观察示例字符串(即"Hello, how do you do"),我们可以看到该字符串中各个单词与空格之间的关系为:单词总数=空格总数+1.也就是说,示例字符串中的空格总数为4