求助
  • 板块灌水区
  • 楼主Chr0n1CleC
  • 当前回复10
  • 已保存回复10
  • 发布时间2022/7/11 08:38
  • 上次更新2023/10/27 21:09:23
查看原帖
求助
701221
Chr0n1CleC楼主2022/7/11 08:38

关于这个BST,怎么都没有输出?(指针版,我感觉数组版反而更难)

#include<stdio.h>
#include<stdlib.h>

struct tree
{
	int x;
	tree* lchild;
	tree* rchild;
};

inline tree* build(int x)
{
	tree* ret = (tree*)malloc(sizeof(tree*));
	ret -> x = x, ret -> lchild = NULL, ret -> rchild = NULL;
	return ret;
}

tree* T;

inline void add(tree* t, int num)
{
	tree* t1 = t;
	while (t1)
		if (t1 -> x == num)
			return;
		else if (num < t1 -> x)
			t1 = t1 -> lchild;
		else
			t1 = t1 -> rchild;
	t1 = build(num);
}

void print(tree* t)
{
	if (t)
	{
		print(t -> lchild);
		printf("%d ", t -> x);
		print(t -> rchild);
	}
}

int main()
{
	int t;
	scanf("%d", &t);
	int num;
	while (t --)
		scanf("%d", &num), add(T, num);
	print(T);
	
	return 0;
}
2022/7/11 08:38
加载中...