在c中以递归方式打印二叉树 [英] print binary tree recursively inorder in c

查看:60
本文介绍了在c中以递归方式打印二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个基本上应该打印程序中已有的二叉树的函数,但是我遇到了问题.这棵树是动态的,值是数字,将在右侧添加较大的新数字,在左侧添加较小的数字.

i've been working on a function that basically is supposed to print the binary tree i have in the program, and i'm having a problem. the tree is dynamice, and the values are numbers, bigger new number will be added in the right side, and smaller in the left side.

基本上,应该打印树的方式是这样的:假设只有根的值为3,则输出为:

basically the way the tree is supposed to be printed is this: let's say there is only the root with the value 3, then the output will be:

(3)

添加值为2的节点后

((2)<(3))

我得到的输出:

(((2))<(3))

加8之后:

((2)<(3)>(8))

我得到的输出:

(((2))<(3)>((8))))

添加7之后:

((2)<(3)>((7)<(8)))

我得到的输出:

(((2))<(3)>(((7))<(8)))

,依此类推...请注意,第一个节点,即根具有不同的结构,因此会发生2种情况.

and so on... notice that the first node, the root has a different struct, therefore the saparation to 2 cases.

主要问题是我的括号过多.

the main problem is I'm getting too many brackets.

这是我到目前为止编写的代码:

this is the code i wrote so far:

void PrintTree(Root* root, Node* currentNode,Boolean isFirst) {
    if (root == NULL)
        return;
    else if (root->right == NULL && root->left == NULL) {
        root->PrintObject(root->value);
        printf("\n");
    } else {
        printf("(");
        if (isFirst == TRUE) {
            if (root->left != NULL) {
                PrintTree(root,root->left, FALSE);
                printf("<");
            }
            root->PrintObject(root->value);

            if (root->right != NULL) {
                printf(">");
                PrintTree(root,root->right, FALSE);
            }
        } else {
            if (currentNode->left != NULL) {
                PrintTree(root,currentNode->left, FALSE);
                printf("<");
            }

            root->PrintObject(currentNode->value);

            if (currentNode->right != NULL) {
                printf(">");
                PrintTree(root,currentNode->right, FALSE);
            }
        }
      printf(")");
    }
  return;
}

void PrintObject(void* value) {
    printf("(%d)", *(int*)value);
    return;
}

将感谢您提供的任何帮助.谢谢!

would appreciate any kind of help. thanks!

推荐答案

问题已解决,缺少条件:

problem solved, a condition was missing:

void PrintTree(Root* root, Node* currentNode,Boolean isFirst)
{
  if (root==NULL)
    return;
  else if (root->right==NULL&&root->left==NULL)
    {
      root->PrintObject(root->value); 
      return;
    }
  else if (currentNode!=NULL)
    {
      if(currentNode->left==NULL&&currentNode->right==NULL&&isFirst==FALSE)
        root->PrintObject(currentNode->value);
        return;
    }
  else 
    {
      printf("(");
      if (isFirst==TRUE)
        {
          if (root->left!=NULL)
            {
              PrintTree(root,root->left,FALSE);
              printf("<");
            }
          root->PrintObject(root->value);

          if (root->right!=NULL)
            {
              printf(">");
              PrintTree(root,root->right, FALSE);
            }
        }
      else
        {

          if (currentNode->left!=NULL)
            {
              PrintTree(root,currentNode->left, FALSE);
              printf("<");
            }

          root->PrintObject(currentNode->value);

          if (currentNode->right!=NULL)
            {
              printf(">");
              PrintTree(root,currentNode->right, FALSE);
            }
        }
      printf(")");
    }
  return;
}

这篇关于在c中以递归方式打印二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆