将二叉树转换为c中的数组
Convert binary tree to array in c
我想使用 C 将二叉树转换为数组。我试过了,但没有成功。
我的二叉树包含以下元素(预排序)
4 3 5 10 8 7
但我的数组包含(排序后)
4 4 5 7 8 10
如有任何帮助,我们将不胜感激。我当前的代码如下所示:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct tree
{
int data;
struct tree *left;
struct tree *right;
}tree;
int AddToArray(tree *node, int arr[], int i);
tree *CreateNode(int data);
tree *Insert(tree *node, int data);
void PrintPreorder(tree *node);
int count(tree *node);
int compare(const void * a, const void * b);
//---------------------------------------------------------------------------
int main()
{
int i;
int size;
int *arr=NULL;
tree *root=NULL;
printf("***TEST PROGRAM***\n");
root = Insert(root, 4);
root = Insert(root, 3);
root = Insert(root, 5);
root = Insert(root, 10);
root = Insert (root, 8);
root = Insert (root, 7);
size = count(root);
printf("\n***BINARY TREE (PREORDER)***\n");
PrintPreorder(root);
printf("\nThe binary tree countain %d nodes", size);
printf("\n\n***ARRAY***\n");
arr = calloc(size, sizeof(int));
AddToArray(root, arr, 0);
qsort(arr,size,sizeof(int),compare);
for (i=0; i<size; i++)
{
printf("arr[%d]: %d\n", i, arr[i]);
}
}
//---------------------------------------------------------------------------
int compare(const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int AddToArray(tree *node, int arr[], int i)
{
if(node == NULL)
return i;
arr[i] = node->data;
i++;
if(node->left != NULL)
AddToArray(node->left, arr, i);
if(node->right != NULL)
AddToArray(node->right, arr, i);
arr[i] = node->data;
i++;
}
tree *CreateNode(int data)
{
tree *node = (tree *)malloc(sizeof(tree));
node -> data = data;
node -> left = node -> right = NULL;
return node;
}
tree *Insert(tree *node, int data)
{
if(node==NULL)
{
tree *temp;
temp = CreateNode(data);
return temp;
}
if(data >(node->data))
{
node->right = Insert(node->right,data);
}
else if(data < (node->data))
{
node->left = Insert(node->left,data);
}
/* Else there is nothing to do as the data is already in the tree. */
return node;
}
void PrintPreorder(tree *node)
{
if(node==NULL)
{
return;
}
printf("%d ",node->data);
PrintPreorder(node->left);
PrintPreorder(node->right);
}
int count(tree *node)
{
int c = 1;
if (node == NULL)
return 0;
else
{
c += count(node->left);
c += count(node->right);
return c;
}
}
两行代码int AddToArray
arr[i] = node->data;
i++;
在每个递归级别出现两次。我的猜测是树中的每个值都被写入数组两次,并且它们相互重叠。但根是要写入两次的最终值,因此它是唯一值得注意的值。
只需删除函数底部的重复调用即可。
将您的 AddToArray
方法更改为:
int AddToArray(tree *node, int arr[], int i)
{
if(node == NULL)
return i;
arr[i] = node->data;
i++;
if(node->left != NULL)
i = AddToArray(node->left, arr, i);
if(node->right != NULL)
i = AddToArray(node->right, arr, i);
return i;
}
发生的事情是你的递归调用正在改变 i
的值(你应该插入以下元素的索引),但你的递归并没有改变 [=12] 的值=] 在实际调用递归的迭代中。用 AddToArray
返回的值更新 i
修复了这个问题。
因为i
没有统一对待
AddToArray
替换为
void AddToArray(tree *node, int arr[], int *i){
if(node == NULL)
return ;
arr[*i] = node->data;
++*i;
AddToArray(node->left, arr, i);
AddToArray(node->right, arr, i);
}
并在 main 调用 i=0; AddToArray(root, arr, &i);
。
int TreeToArray (struct node *tree, int *arr, int i)
{
if (tree == NULL) return i;
if (tree->left != NULL) i = TreeToArray(tree->left, arr, i);
arr[i] = tree->Value;
i++;
if (tree->right != NULL) i = TreeToArray(tree->right, arr, i);
return i;
}
我想使用 C 将二叉树转换为数组。我试过了,但没有成功。
我的二叉树包含以下元素(预排序)
4 3 5 10 8 7
但我的数组包含(排序后)
4 4 5 7 8 10
如有任何帮助,我们将不胜感激。我当前的代码如下所示:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct tree
{
int data;
struct tree *left;
struct tree *right;
}tree;
int AddToArray(tree *node, int arr[], int i);
tree *CreateNode(int data);
tree *Insert(tree *node, int data);
void PrintPreorder(tree *node);
int count(tree *node);
int compare(const void * a, const void * b);
//---------------------------------------------------------------------------
int main()
{
int i;
int size;
int *arr=NULL;
tree *root=NULL;
printf("***TEST PROGRAM***\n");
root = Insert(root, 4);
root = Insert(root, 3);
root = Insert(root, 5);
root = Insert(root, 10);
root = Insert (root, 8);
root = Insert (root, 7);
size = count(root);
printf("\n***BINARY TREE (PREORDER)***\n");
PrintPreorder(root);
printf("\nThe binary tree countain %d nodes", size);
printf("\n\n***ARRAY***\n");
arr = calloc(size, sizeof(int));
AddToArray(root, arr, 0);
qsort(arr,size,sizeof(int),compare);
for (i=0; i<size; i++)
{
printf("arr[%d]: %d\n", i, arr[i]);
}
}
//---------------------------------------------------------------------------
int compare(const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int AddToArray(tree *node, int arr[], int i)
{
if(node == NULL)
return i;
arr[i] = node->data;
i++;
if(node->left != NULL)
AddToArray(node->left, arr, i);
if(node->right != NULL)
AddToArray(node->right, arr, i);
arr[i] = node->data;
i++;
}
tree *CreateNode(int data)
{
tree *node = (tree *)malloc(sizeof(tree));
node -> data = data;
node -> left = node -> right = NULL;
return node;
}
tree *Insert(tree *node, int data)
{
if(node==NULL)
{
tree *temp;
temp = CreateNode(data);
return temp;
}
if(data >(node->data))
{
node->right = Insert(node->right,data);
}
else if(data < (node->data))
{
node->left = Insert(node->left,data);
}
/* Else there is nothing to do as the data is already in the tree. */
return node;
}
void PrintPreorder(tree *node)
{
if(node==NULL)
{
return;
}
printf("%d ",node->data);
PrintPreorder(node->left);
PrintPreorder(node->right);
}
int count(tree *node)
{
int c = 1;
if (node == NULL)
return 0;
else
{
c += count(node->left);
c += count(node->right);
return c;
}
}
两行代码int AddToArray
arr[i] = node->data;
i++;
在每个递归级别出现两次。我的猜测是树中的每个值都被写入数组两次,并且它们相互重叠。但根是要写入两次的最终值,因此它是唯一值得注意的值。
只需删除函数底部的重复调用即可。
将您的 AddToArray
方法更改为:
int AddToArray(tree *node, int arr[], int i)
{
if(node == NULL)
return i;
arr[i] = node->data;
i++;
if(node->left != NULL)
i = AddToArray(node->left, arr, i);
if(node->right != NULL)
i = AddToArray(node->right, arr, i);
return i;
}
发生的事情是你的递归调用正在改变 i
的值(你应该插入以下元素的索引),但你的递归并没有改变 [=12] 的值=] 在实际调用递归的迭代中。用 AddToArray
返回的值更新 i
修复了这个问题。
因为i
没有统一对待
AddToArray
替换为
void AddToArray(tree *node, int arr[], int *i){
if(node == NULL)
return ;
arr[*i] = node->data;
++*i;
AddToArray(node->left, arr, i);
AddToArray(node->right, arr, i);
}
并在 main 调用 i=0; AddToArray(root, arr, &i);
。
int TreeToArray (struct node *tree, int *arr, int i)
{
if (tree == NULL) return i;
if (tree->left != NULL) i = TreeToArray(tree->left, arr, i);
arr[i] = tree->Value;
i++;
if (tree->right != NULL) i = TreeToArray(tree->right, arr, i);
return i;
}