// 4.3 Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.
void CreateBST(Tree* &tree, std::vector<int> &arr, int start, int end) {
	if(start > end)
		return;
	int mid = (start + end) / 2;
	Node* node = new Node; // save all the tree nodes in heap
	node->SetKey(arr[mid]);
	tree->TreeInsert(tree, node);
	CreateBST(tree, arr, start, mid - 1);
	CreateBST(tree, arr, mid + 1, end);
	return;
}
Exemple #2
0
void main()
{
	BSTree T;
	char ch1,ch2;
	KeyType Key;
	printf("建立一棵二叉排序树的二叉链表存储\n");
	T=CreateBST();
	ch1='y';
	while (ch1=='y' || ch1=='Y')
	{
		printf("请选择下列*作:\n");
		printf("1------------------更新二叉排序树存储\n");
		printf("2------------------二叉排序树上的查找\n");
		printf("3------------------二叉排序树上的插入\n");
		printf("4------------------二叉排序树上的删除\n");
		printf("5------------------二叉排序树中序输出\n");
		printf("6------------------退出\n");
		scanf("\n%c",&ch2);
		switch (ch2)
		{
		case '1':
			T=CreateBST();
			break;
		case '2':
			printf("\n请输入要查找的数据:");
			scanf("\n%d",&Key);
			SearchBST(T,Key);
			printf("查找*作完毕。\n");
			break;
		case '3':
			printf("\n请输入要插入的数据:");
			scanf("\n%d",&Key);
			InsertBST(&T,Key);
			printf("插入*作完毕。\n");
			break;
		case '4':
			printf("\n请输入要删除的数据:");
			scanf("\n%d",&Key);
			DelBSTNode(&T,Key);
			printf("删除*作完毕。\n");
			break;
		case '5':
			InorderBST(T);
			printf("\n二叉排序树输出完毕。\n");
			break;
		case '6':
			ch1='n';
			break;
		default:
			ch1='n';
		}
	}
}
Exemple #3
0
TNode* CreateBST(LNode** head,int n){

if(n<=0){
return NULL;
}

TNode* left=CreateBST(head,n/2);

TNode* root=(TNode*)malloc(sizeof(TNode));
root->data=(*head)->data;
root->left=left;
(*head)=(*head)->next;
root->right=CreateBST(head,n-(n/2)-1);
return root;

}
Exemple #4
0
int main()
{
	BSTree T;
	int k;
	BSTree result;
	printf("建立二叉排序树,请输入序列, 以-1结束:\n");
    CreateBST(&T);
	printf("先序遍历输出序列为:");
	PreOrder(T);
/*
	printf("\n请输入要查找的元素:");
	fflush(stdin);
	scanf("%d",&k);
	result = SearchBST(T,k);
	if (result != NULL)
		printf("要查找的元素为%d\n",result->key);
	else
		printf("未找到!\n");
	result = DelBST(T,k);
	printf("删除元素%d后的排序树先序遍历为\n",k);
*/
	PreOrder(T);
	MakeDot(T);
	return 0;
}
Exemple #5
0
// inserts an item at the wanted location
TreeNode *BSTInsert(TreeNode * tree, Item * newItem) {
	// empty tree case, make a new tree with this item as the root
	if ( tree == NULL ) return( CreateBST(newItem, NULL, NULL) );

	// duplicates go to the right
	if ( newItem->key  < tree->item->key )
		tree->pLeft = BSTInsert(tree->pLeft, newItem);
	else
		tree->pRight = BSTInsert(tree->pRight, newItem);
	return tree;
}
int main() {
	int a[10], key;
	BTNode *bt,*node;
	ScanArray(a, 10);
	//PrintArray(a, 10);
	CreateBST(&bt, a, 10);
	scanf("%d", &key);
	node = BSTsearch_r(bt, key);
	if (NULL != node)
		printf("Succcess %d\n", node->key);
	else 
		printf("Failed\n");
	return 0;
}
int main()
{
	Tree tree;
	Tree* t = &tree;
	std::vector<int> arr;
	for(int i = 0; i < 10; i++) {
		arr.push_back(i);
	}

	CreateBST(t, arr, 0, 9); // problem 4.3
	Node* successor = tree.FindSuccessor(tree.GetRoot()); // problem 4.6

	DeleteBST(tree.GetRoot()); // free the tree nodes in heap
	
	return 0;
}
Exemple #8
0
void main()
{
	BSTNode *bt,*p,*f;
	int n=12,x=46;
	KeyType a[]={25,18,46,2,53,39,32,4,74,67,60,11};
	bt=CreateBST(a,n);
	printf("BST:");DispBST(bt);printf("\n");
	printf("删除%d结点\n",x);
	if (SearchBST(bt,x)!=NULL)
	{
		DeleteBST(bt,x);
		printf("BST:");DispBST(bt);printf("\n");
	}
	x=18;
	p=SearchBST1(bt,x,NULL,f);
	if (f!=NULL)
		printf("%d的双亲是%d\n",x,f->key);


}
int main()
{
	int key, i;
	BSTree bst, *pos;
	CreateBST(&bst, source, ARRAYLEN);
	printf("遍历二叉排序树结果:");
	BST_LDR(&bst);


	scanf_s("%d", &key);
	pos = SearchBST(&bst, key);
	if (pos)
	{
		printf("search successfull!");
	}
	else
	{
		printf("search failed!");
	}
	getchar();
	getchar();
	return 0;
}