zoukankan      html  css  js  c++  java
  • 【数据结构】二叉树的创建与遍历

    策略是按照#号分割, 先左后右,先左后右....去构造二叉树

    C 极简版

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    typedef struct BiNode
    {
        char data;// 仅用char作为例子
        struct BiNode *lchild, *rchild;
    }BiNode, *BiTree;
    
    
    const char *s = "ABDH#K###E##CFI###G#J##";
    int _index = 0;
    
    /* 构造二叉树*/
    void CreateBiTree(BiTree *T)
    {
        if (_index == strlen(s)) return;
        char ch = s[_index++];
    
        if (ch == '#') {
            *T = NULL;
        } else {
            *T = (BiTree)malloc(sizeof(BiNode));
            if (!*T) { exit(OVERFLOW); } // 内存分配失败
    
            (*T)->data = ch;
            CreateBiTree(&(*T)->lchild);
            CreateBiTree(&(*T)->rchild);
    
        }
    }
    
    /* 前序遍历*/
    void PreOrderTraverse(BiTree *T)
    {
        if (*T == NULL) return;
    
        printf("%c ", (*T)->data);
        PreOrderTraverse(&(*T)->lchild);
        PreOrderTraverse(&(*T)->rchild);
    }
    
    
    
    int main(){
       BiTree T;
       CreateBiTree(&T);
       PreOrderTraverse(&T);
        return 0;
    }
    
    

    C 完整版

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    
    #define MAXSIZE 100    /* 存储空间初始分配量 */
    
    typedef int Status;
    
    typedef char TElemType; /* 假定二叉树的元素都是字符类型*/
    TElemType Nil = ' ';
    
    typedef struct BiTNode
    {
        TElemType data;
        struct BiTNode *lchild, *rchild;
    }BiTNode, *BiTree;
    
    
    /* 模拟生成char数组, 用于构造二叉树所生产的数据***************/
    int index$ = 1;
    typedef char String[24];
    String str;
    
    /* 将chars 用String结构体表示*/
    Status StrAssign(String S, char *chars)
    {
        int i;
        if (strlen(chars) > MAXSIZE) {
            return ERROR; // 超过了最大容量
        } else {
            // 第一个位置存放字符串的长度
            S[0] = strlen(chars);
            for (i= 1; i < S[0]; i++) {
                S[i] = *(chars + i -1);
            }
    
            return OK;
        }
    
    
    }
    
    /* ******************************************************/
    
    /* 构造空的的二叉树T */
    Status InitBiTree(BiTree *T)
    {
        *T = NULL;
        return OK;
    }
    
    /* 构造二叉树T, 使用的是递归创建的方法 */
    void CreateBiTree(BiTree  *T)
    {
    
    
        if (index$ == strlen(str)) // 所有的元素都分配完毕, 即结束
            return;
        TElemType ch = str[index$++];
    
    
        if (ch == '#') {
            // 碰到#号, 停止生成新的结点
            *T = NULL;
        } else {
            *T = (BiTree)malloc(sizeof(BiTNode));
            if (!*T){
                exit(OVERFLOW); // malloc失败
            }
    
            (*T)->data = ch; // 生成根节点
            CreateBiTree(&(*T)->lchild); // 构造左子树
            CreateBiTree(&(*T)->rchild); // 构造右子树
        }
    
    
    }
    
    /* 前序遍历*/
    void PreOrderTraverse(BiTree *T)
    {
        if (*T == NULL)
            return;
    
        printf("%c ", (*T)->data);
        PreOrderTraverse(&(*T)->lchild);
        PreOrderTraverse(&(*T)->rchild);
    }
    
    
    
    int main() {
    
        BiTree T;
        InitBiTree(&T);
    
        // 构造字符串
        StrAssign(str, "ABDH#K###E##CFI###G#J##");
    
    
        // 构造二叉树
        CreateBiTree(&T);
    
        PreOrderTraverse(&T);
    
    
        return 0;
    }
    

    java 版

    public class Demo {
        static class BiNode{
            String data;
            BiNode  lchild;
            BiNode  rchild;
        }
        String s = "ABDH#K###E##CFI###G#J##";
        int index = 0;
    
        /* 构造二叉树*/
        public BiNode createBiTree(BiNode node){
    
            if (index == s.length()) return node;
            String ch = s.substring(index, index+1);
            index++;
    
            if (ch.equals("#")){
                node = null;
            } else {
                node = new BiNode();
                node.data = ch;
                node.lchild = createBiTree(node.lchild);
                node.rchild = createBiTree(node.rchild);
            }
    
            return node;
        }
    
    
        /* 前序遍历*/
        public void preOrderTraverse(BiNode node){
            if (node == null) {
                return;
            }
            System.out.printf("%s ", node.data);
            preOrderTraverse(node.lchild);
            preOrderTraverse(node.rchild);
        }
    
        public static void main(String[] args) {
            Demo demo = new Demo();
    
            BiNode root = null;
            BiNode after = demo.createBiTree(root);
    
            demo.preOrderTraverse(after);
    
    
        }
    
    
    }
    public class Demo {
        static class BiNode{
            String data;
            BiNode  lchild;
            BiNode  rchild;
        }
        String s = "ABDH#K###E##CFI###G#J##";
        int index = 0;
    
        /* 构造二叉树*/
        public BiNode createBiTree(BiNode node){
    
            if (index == s.length()) return node;
            String ch = s.substring(index, index+1);
            index++;
    
            if (ch.equals("#")){
                node = null;
            } else {
                node = new BiNode();
                node.data = ch;
                node.lchild = createBiTree(node.lchild);
                node.rchild = createBiTree(node.rchild);
            }
    
            return node;
        }
    
    
        /* 前序遍历*/
        public void preOrderTraverse(BiNode node){
            if (node == null) {
                return;
            }
            System.out.printf("%s ", node.data);
            preOrderTraverse(node.lchild);
            preOrderTraverse(node.rchild);
        }
    
        public static void main(String[] args) {
            Demo demo = new Demo();
    
            BiNode root = null;
            BiNode after = demo.createBiTree(root);
    
            demo.preOrderTraverse(after);
    
    
        }
    
    
    }
    
    
    
    “年轻时,我没受过多少系统教育,但什么书都读。读得最多的是诗,包括烂诗,我坚信烂诗早晚会让我邂逅好诗。” by. 马尔克斯
  • 相关阅读:
    Gitlab 与 Git Windows 客户端一起使用的入门流程
    怎样把SEL放进NSArray里
    PerformSelector may cause a leak because its selector is unknown 解决方法
    drawRect
    记录常规越狱的判断方法
    网页 js
    UICollectionView 基础
    FMDB的简单使用
    图层的一些基本动画效果
    NSPredicate简单介绍
  • 原文地址:https://www.cnblogs.com/jzsg/p/11300905.html
Copyright © 2011-2022 走看看