Binary Tree

truct Node

typedef struct Node{
	int key;
	struct Node *left, *right;
}Node;

truct Tree

typedef struct Tree{
	Node *root;
}Tree;
Binary Tree

function initial

void initial(Tree *t){
	t->root = NULL;
}
Binary Tree

function getNode

Node* getNode(int k){
	Node * p = (Node*)malloc(sizeof(Node));
	p->key = k;
	p->left = NULL;
	p->right = NULL;
	return p;
}
Binary Tree

function insert

bool insert(Tree *t, int k){
	Node **p = &t->root;
	while(*p != NULL){
		if ((*p)->key == k){
			return false;
		}else if( (*p) -> key > k){
			p = &(*p) -> left;
		}else{
			p = &(*p) -> right;
		}
	}
	*p = getNode(k);
	return true;
}
Binary Tree

function add

Node* add(Node *root, int k){
	if (root == NULL){
		return getNode(k);
	}
	if (root -> key > k){
		root -> left = add(root->left, k);
	}else{
		root -> right = add(root -> right, k);
	}
	return root;
}
Binary Tree

function nodeLeftRight

void nodeLeftRight(const Node *root){
	if (root != NULL){
		printf("%d ", root -> key);
		nodeLeftRight(root -> left);
		nodeLeftRight(root -> right);
	}
}

Binary Tree

function leftNodeRight

void leftNodeRight(const Node *root){
	if (root != NULL){
		leftNodeRight(root -> left);
		printf("%d ", root -> key);
		leftNodeRight(root -> right);
	}
}

Binary Tree

function leftRightNode

void leftRightNode(const Node *root){
	if (root != NULL){
		leftRightNode(root -> left);
		leftRightNode(root -> right);
		printf("%d ", root -> key);
	}
}

Binary Tree

function rightNodeLeft

void rightNodeLeft(const Node *root){
	if (root != NULL){
		rightNodeLeft(root->right);
		printf("%d ", root->key);
		rightNodeLeft(root->left);
	}
}
Binary Tree

function search

Node* search(Tree *t, int k){
	Node* p = t->root;
	while( p != NULL ){
		if (p -> key == k){
			return p;
		}else if ( p -> key == k){
			p = p -> left;
		}else{
			p = p -> right;
		}
	}
	return NULL;
}