[ create a new paste ] login | about

Link: http://codepad.org/2c5xFbLM    [ raw code | output | fork ]

C++, pasted on Oct 21:
#include<iostream>
#include<math.h>
#include <cstdlib>
using namespace std;

struct node{
	struct node * lc;
	struct node * rc;
	int data;
};

typedef struct node Node;

Node * getNewNode(int data){
	Node * node = NULL;

	node = (Node*)malloc(sizeof(node));
	node -> data = data;
	node -> lc = NULL;
	node -> rc = NULL;

	return node;
}

Node * buildBst(Node * root,int data){
	if(NULL == root){
		return getNewNode(data);
	}
	if(data > root -> data){
		root -> rc = buildBst(root->rc,data);
	}else{
		root -> lc = buildBst(root->lc,data);
	}
	return root;
}

void printInorder(Node * root){
	if(root != NULL){
		printInorder(root -> lc);
		cout << root -> data << " ";
		printInorder(root -> rc);
	}
}

int main(int argc, char* argv[]) {

	int arr [] = {2,3,4,1,5,9,0,3};
	Node * root = NULL;
	for(int i = 0;i < 6; ++i){
		root = buildBst(root,arr[i]);
	}
	printInorder(root);
	cout << endl;
}


Output:
1
1 2 3 4 5 9 


Create a new paste based on this one


Comments: