打印二叉查找树所有满足k1<=key(x)<=k2的元素X

数据结构与算法分析——c语言描述 练习4.32 答案


程序会写复杂度还是不会算。。。


#include"fatal.h"
#include<stdlib.h>
#include"tree.h"
typedef int ElementType;

struct TreeNode;
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;

struct TreeNode {
	ElementType element;
	SearchTree left;
	SearchTree right;
};

void printElemBetwweenK1K2(SearchTree t, int k1, int k2) {
	if (t) {
		if (t->element >= k1)
			printElemBetwweenK1K2(t->left, k1, k2);
		if (t->element >= k1&&t->element <= k2)
			printf("%d ", t->element);
		if (t->element <= k2)
			printElemBetwweenK1K2(t->right, k1, k2);
		
	}
}

int main() {
	SearchTree t=NULL;

	for (int i = 0; i < 444; i++)
		t = insert(rand(), t);
	printElemBetwweenK1K2(t,400, 1500);
}




你可能感兴趣的:(打印二叉查找树所有满足k1<=key(x)<=k2的元素X)