二叉查找树的构造与遍历(C++)

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

using namespace std;

typedef struct BSTNode{
    int key;
    struct BSTNode *left,*right;
}BSTNode,*BST;

int Insert(BSTNode *&bst, int data)
{
    if(bst == NULL){
        
        bst = (BSTNode*)malloc(sizeof(BSTNode));
        bst->key = data;
        bst->left = bst->right = NULL;
        return 1;
        
        
    }else if(data == bst->key){
        
        return 0;
        
    }else if(data < bst->key){
        
        Insert(bst->left,data);
        
    }else
        Insert(bst->right,data);
}

BSTNode *CreatBST(int *value, int len)
{
    BSTNode *bst = NULL;
    int i = 0;
    while(i<len)
    {
        if(Insert(bst, value[i])==1)
            i++;
    }
    return bst;
}

void prior(BSTNode *bst)
{
    if(bst == NULL)
        return;
    else{
        cout<<bst->key<<" - ";
        prior(bst->left);
        prior(bst->right);
    }
}
void mid(BSTNode *bst)
{
    if(bst == NULL)
        return;
    else{
        prior(bst->left);
        cout<<bst->key<<" - ";
        prior(bst->right);
    }
}
void after(BSTNode *bst)
{
    if(bst == NULL)
        return;
    else{
        prior(bst->left);
        prior(bst->right);
        cout<<bst->key<<" - ";
    }
}
void show(BSTNode *bst)
{
    cout<<"前序遍历:";
    prior(bst);
    cout<<endl;
    cout<<"中序遍历:";
    mid(bst);
    cout<<endl;
    cout<<"后序遍历:";
    after(bst);
    cout<<endl;
}

int main(int argc,char *argv[])
{
    int a[10] = {1,0,4,5,2,7,8,6,3,9};
    
    BSTNode *bst = CreatBST(a,10);
    show(bst);
}

编译与运行:



序列{1,0,4,5,2,7,8,6,3,9}的二叉查找树如下,并用三种遍历结果验证算法准确性:


再给另一个程序示例:

#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
#include <math.h>
#include <string.h>
#include <iostream>

using namespace std;
/** 
 *leetcode题库:4. 求两个排序数组的中位数
 *
 *  给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。
 *  请找出这两个有序数组的中位数。
 *  要求算法的时间复杂度为 O(log (m+n))。
 *
 *  示例 1: nums1 = [1, 3]   nums2 = [2]   中位数是 2.0
 *  示例 2: nums1 = [1, 2]   nums2 = [3, 4]   中位数是 (2 + 3)/2 = 2.5
 */

/* 整形二叉树节点 */
typedef struct Node {
	int data;
	struct Node *lchild, *rchild;
} Node, *BST;

int insertBSTNode(int data, Node *&bst)
/* 插入节点Node */
{
	if(bst == NULL)
	{
		bst = (Node*)malloc(sizeof(Node));
		bst->data = data;
		bst->lchild = NULL;
		bst->rchild = NULL;
		cout<<"----i"<<endl;
		return 1;
	}
	else 
	{
		if(data == (bst->data))
		{
			cout<<data<<"already exist.\n";
			return 0;
		}
		else if(data < (bst->data))
		{
			cout<<"----l"<<endl;
			insertBSTNode(data, bst->lchild);
		}
		else
		{
			cout<<"----r"<<endl;
			insertBSTNode(data, bst->rchild);
		}
	}
	return 1;
}

int insertArray2BST(Node *&bst, int *v, int num)
/* 向BST中插入数组 */
{
	int i=0;
	while(i<num)
	{
		cout<<v[i]<<endl;
		insertBSTNode(v[i],bst);
		i++;
	}
	return 1;
}
void Traverse(Node *bst, int type)  
/* 先序遍历 */
{  
    if(bst == NULL)  
        return;  
    else{ 
		switch(type)
		{
			case 1:
				cout<<bst->data<<" - "; 
				Traverse(bst->lchild,type);  
				Traverse(bst->rchild,type);  
				break;
			case 2:
				Traverse(bst->lchild,type);  
				cout<<bst->data<<" - "; 
				Traverse(bst->rchild,type);
				break;
			case 3:
				Traverse(bst->lchild,type); 
				Traverse(bst->rchild,type); 
				cout<<bst->data<<" - "; 
				break;
			default:
				cout<<"please type = 1,2,3"<<endl;
				break;
		}
    }  
} 
int main( int argc, char **argv)
{
	int *v1, *v2;
	int n1 = 6, n2 = 2;
	int array1[] = {5,3,7,1,9,4};
	int array2[] = {6,2};
	v1 = array1;
	v2 = array2;
	
	Node *bst = NULL;
	insertArray2BST(bst,v1,n1);
	insertArray2BST(bst,v2,n2);
	Traverse(bst,1);
	
	return 1;
}

结果:

5 - 3 - 1 - 2 - 4 - 7 - 6 - 9 -

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值