简体   繁体   English

递归二叉树

[英]Recursive Binary Tree

The point is to print a binary tree such as: 关键是要打印一个二叉树,例如:

-------x--------
---x-------x----
-x---x---x---x--
x-x-x-x-x-x-x-x-
xxxxxxxxxxxxxxxx

My code is: 我的代码是:

#include <stdio.h>
#include <math.h>

#define LENGTH 16

void makeBranches(int left, int right, char a[][LENGTH], int);
void display(char a[][LENGTH], int);

void main(){
  int i, lines;
  double a;

  a = log10(LENGTH*2)/log10(2);
  lines = (int)a; 
  char array[lines][LENGTH];

  makeBranches(0, LENGTH-1, array, 0);
  display(array, lines);
}

void makeBranches(int left, int right, char a[][LENGTH], int line){

  if(left >= right){
    a[line][left] = 'X';
    return;
  } else{
    a[line][(right+left)/2] = 'X';
    makeBranches(left, (right+left)/2, a, line+1);
    makeBranches((right+left)/2+1, right, a, line+1); 
  }
}

void display(char a[][LENGTH], int lines){
  int i, j;

  for(i = 0; i < lines; i++){
    for(j = 0; j < LENGTH; j++){
      if(a[i][j] == 'X')
    printf("%c", a[i][j]);
      else
    printf("-");      
    }
    printf("\n");
  }
}

This works fine for LENGTH values of 4,8,16 but when you get to trying 32,64 etc. it has some stray X's. 对于4、8、16的LENGTH值,这很好用,但是当您尝试使用32,64等时,它会有一些杂散X。 For example: 例如:

The LENGTH 32 长度32

---------------X----X-------X---
-------X---------------X--------
---X-------X-------X-------X----
-X---X---X---X---X---X---X---X--
X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The LENGTH 64 长度64

-------------------------------X--------------------------------
---------------X-------------------------------X----------------
-------X---------------X---------------X---------------X--------
---X-------X-------X-------X-------X-------X-------X-------X----
-X---X---X---X---X--XX---X--XX---X---X---X---X---X---X---X---X--
X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

This has got to be a simple fix somewhere but I just cannot see it. 这一定是一个简单的修复方法,但是我看不到它。 Hopefully someone can. 希望有人可以。

char array[lines][LENGTH];

This creates an empty array, where every value is whatever is currently in memory (which is sometimes 0, but not guaranteed to be). 这将创建一个空数组,其中每个值都是内存中当前的值(有时为0,但不能保证是)。 This means that sometimes, at random, the memory will already contain an "X" byte. 这意味着有时,存储器中已经随机包含一个“ X”字节。 You can solve this by initializing the array to be full of 0's (that's the null character, not '0'): 您可以通过将数组初始化为充满0(即空字符,而不是“ 0”)来解决此问题:

memset(array, 0, LENGTH * lines);

Or: 要么:

for(size_t i = 0; i < lines; i++){
    for(size_t j = 0; j < LENGTH; j++){
        a[i][j] = 0;
    }
}

Your array is a local variable in main , and you never initialize most of its values. 您的arraymain的局部变量,并且您绝不会初始化其大多数值。 Local variables that are not explicitly initialized start out containing "random garbage" -- which is not really "random", but is "garbage" enough that you can't be sure some of the positions do not accidentally contain the value 'X' initially. 未明确初始化的局部变量开始时包含“随机垃圾”,这不是真正的“随机”,但足够“垃圾”,因此您无法确定某些位置不会意外包含值“ X”原来。

Start by looping over the entire array and initializing all positions to something known. 首先遍历整个数组并将所有位置初始化为已知的位置。

(Nitpicking language lawyers will know that "random garbage" is not the technically correct term for what happens with unitialized locals, but it is close enough to work as a mental model in practical programming). (挑剔的语言律师会知道,“随机垃圾”并不是统一本地人所发生的技术上正确的术语,但它足以在实际编程中充当思维模型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM