Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将二叉树转换为二叉搜索树_C_Data Structures_Binary Tree_Binary Search Tree - Fatal编程技术网

C 将二叉树转换为二叉搜索树

C 将二叉树转换为二叉搜索树,c,data-structures,binary-tree,binary-search-tree,C,Data Structures,Binary Tree,Binary Search Tree,在这里,我通过将二叉树的元素复制到数组,将二叉树转换为二叉搜索树。 然后,我对数组进行排序(气泡排序),然后使用顺序遍历遍历树,并将数组的元素复制到树中。 一切正常,但函数void inr(Node*root)没有像在二叉搜索树中那样复制元素,我应该按递增顺序(按顺序遍历)获取元素,但我没有得到正确的输出 #include<stdio.h> #include<stdlib.h> int x=0; int l=0; typedef struct node

在这里,我通过将二叉树的元素复制到数组,将二叉树转换为二叉搜索树。 然后,我对数组进行排序(气泡排序),然后使用顺序遍历遍历树,并将数组的元素复制到树中。 一切正常,但函数void inr(Node*root)没有像在二叉搜索树中那样复制元素,我应该按递增顺序(按顺序遍历)获取元素,但我没有得到正确的输出

#include<stdio.h>
#include<stdlib.h>
   int x=0;
   int l=0;
   typedef struct node
   {
      int data;
      struct node *rlink;
      struct node *llink;
   } Node;
   Node *getnode()
   {
      Node *temp=(Node *)malloc(sizeof(Node));
      return temp;
   }
   int a[10];
   int i=0;
   Node *create(Node *root,int key)
   {
      int c;
      if(root==NULL)
      {
         root=getnode();
         root->rlink=root->llink=NULL;
         root->data=key;
      }
      else
      {
         printf("Enter where you want to enter 1.RIGHT 2.LEFT");
         scanf("%d",&c);
         switch(c)
         {
            case 1:root->rlink=create(root->rlink,key);
                   break;
            case 2:root->llink=create(root->llink,key);
                   break;
            default:printf("Wrong choice");
            break;
         }
     }
     return root;
}

void inorder(Node *root) //to copy the contents  of the tree in the array
{
   if(root!=NULL)
   {
      inorder(root->rlink);
      if(root->data>0) a[i++]=root->data;
      inorder(root->llink);
   }
}

void utu(Node *h) //For inorder traversal
{
   if(h!=NULL)
   {
      utu(h->rlink);
      printf("%d",h->data);
      utu(h->llink);
   }
}

void inr(Node *root) //to copy the elements of sorted array in the tree to make it a BST
{
   if(root==NULL) return;
   else if(root!=NULL)
   {
      inr(root->llink);
      root->data=a[l++];
      inr(root->rlink);
   }
}

void main()
{
   int j=0;
   int temp=0;
   Node *h=NULL;
   h=create(h,5);
   h=create(h,6);
   h=create(h,7);
   h=create(h,8);
   inorder(h);
   count(h);
   printf("%d \n",x);
   printf("\n \n \n");
   for(j=0;j<x;j++)
   {
      printf("%d \n",a[j]);
   }
   for(int i=0;i<x;i++) //bubble sorting of the array obtained by copying the nodes data in the array
   {
      for(j=i+1;j<x;j++)
      {
         if(a[i]>a[j])
         {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
         }
      }
  }
  utu(h);
  //for(j=0;j<x;j++)
  //{
  //    printf("\n %d \n",a[j]);
  //}
  printf("\n \n");
  //j=maxd(h);
  //printf("%d",j);
  inr(h);
  utu(h);
}
#包括
#包括
int x=0;
int l=0;
类型定义结构节点
{
int数据;
结构节点*rlink;
结构节点*llink;
}节点;
Node*getnode()
{
Node*temp=(Node*)malloc(sizeof(Node));
返回温度;
}
INTA[10];
int i=0;
节点*创建(节点*根,int键)
{
INTC;
if(root==NULL)
{
root=getnode();
root->rlink=root->llink=NULL;
根->数据=键;
}
其他的
{
printf(“输入要输入的位置1.RIGHT 2.LEFT”);
scanf(“%d”、&c);
开关(c)
{
案例1:root->rlink=create(root->rlink,key);
打破
案例2:root->llink=create(root->llink,key);
打破
默认值:printf(“错误选择”);
打破
}
}
返回根;
}
void inoorder(Node*root)//复制数组中树的内容
{
if(root!=NULL)
{
顺序(根->链接);
如果(root->data>0)a[i++]=root->data;
顺序(根->llink);
}
}
void utu(Node*h)//用于顺序遍历
{
如果(h!=NULL)
{
utu(h->rlink);
printf(“%d”,h->data);
utu(h->llink);
}
}
void inr(Node*root)//复制树中已排序数组的元素,使其成为BST
{
if(root==NULL)返回;
else if(root!=NULL)
{
inr(根->llink);
根->数据=a[l++];
inr(根->rlink);
}
}
void main()
{
int j=0;
内部温度=0;
Node*h=NULL;
h=创建(h,5);
h=创建(h,6);
h=创建(h,7);
h=创建(h,8);
顺序(h);
计数(h);
printf(“%d\n”,x);
printf(“\n\n\n”);

对于(j=0;jp请告诉我们您看到的输出以及您希望看到的。
utu
先右后左,但
inr
先左后右。@BadZen我想要5678,我得到的输出是8765。听起来好像您按错误的顺序排序,或者按错误的顺序插入。请告诉我们您看到的输出是什么,以及您期望看到的。
utu
先右后左,但
inr
先左后右。@BadZen我想要5678,我得到8765,因为输出听起来好像您排序顺序错误,或者插入顺序错误。