您的位置:首页 > 其它

search - binary search/sort tree

2015-04-15 03:08 281 查看
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

//What is a Binary Sort Tree?
//1. A BST is either an empty tree or a binary tree with the following characteristics
//2. all elements on left child tree is smaller than root
//3. all elements on right child tree is larger than root
//4. both left and right child are BST in its own right

//Define a node structure in BST
typedef struct _BSTNode {
int data;
struct _BSTNode* lchild, *rchild;
}BSTNode, *BSTree;

// searching key in BST tree
// tree: the target BST tree to be searched
// parent: the parent of tree, this is useful only when tree == NULL
// key: key to search
// found: if found key, gives the node else the last node in the search path
bool SearchBST( BSTree tree, BSTree parent, int key, BSTree* found )
{
if( tree == NULL )
{
//tree is NULL, simply set found = parent
*found = parent;
return false;
}
else if( key == tree->data )
{
//if tree hosts key, set found = tree
*found = tree;
return true;
}
else if( key < tree->data )
{
//if key is smaller, search the left BST
return SearchBST( tree->lchild, tree, key, found );
}
else
{
//if key is larger, search the right BST
return SearchBST( tree->rchild, tree, key, found );
}
}

//insert key into BST
//tree: a pointer to the tree
bool InsertBST( BSTree* tree, int key)
{
BSTree found, temp;
if( !SearchBST( *tree, NULL, key, &found))
{
//if we have not found key, create a new node
temp = (BSTree)malloc( sizeof(BSTNode));
temp->data = key;
temp->lchild = temp->rchild = NULL;
if( found == NULL )
{
//first scenario, found returns NULL, indicating tree is empty
*tree = temp;
}
else if( key < found->data )
{
found->lchild = temp;
}
else
{
found->rchild = temp;
}
return true;
}
else
{
//if key is found, do nothing and simply return false
return false;
}
}

bool DeleteNode( BSTree* node )
{
BSTree q;
if( (*node)->rchild == NULL )
{
//if right tree is empty, simply re-wiring to *node to point to left child
//note dereference node (*node) gives the address of node, so what we need
//to do here is populate the value of *node to the left node
q = *node; //q caches which node is originally pointed
*node = (*node)->lchild; //re-wiring
free(q); //q is now wild pointer, delete it
}
else if( (*node)->lchild == NULL )
{
//if left tree is empty
q = *node;
*node = (*node)->rchild;
free(q);
}
else
{
//things going to complicated, now both child exists
//we found the largest node on the left tree
//the node must not have a right child otherwise it
//won't be the largest
BSTree s;
s = (*node)->lchild;
q = *node;
while( s->rchild )
{
q = s;
s = s->rchild;
}
//s is always the largest node on left tree
//q is always the parent of s
//now we not re-wiring node, but just copy the data field
//from s to node, this avoids polluting lchild/rchild pointers
(*node)->data = s->data;
//next step, we delete s
//but before that we need re-wiring s->lchild to q
//need consider connect (q->lchild or q->rchild) to s->lchild
if( q != *node )
{
//the while(s->rchild) loops at least once
//so s is rchild of q
q->rchild = s->lchild;
}
else
{
//while loop not run, s=(*node)->lchild and q=*node
q->lchild = s->lchild;
}
free(s);
}
return true;
}

//delete key from BST
bool DeleteBST( BSTree* tree, int key )
{
if( !*tree )
{
//this indicates key is not found, simply return
return false;
}
else
{
if( key == (*tree)->data )
{
//if *tree is the node host key, delete it
return DeleteNode(tree);
}
else if( key < (*tree)->data )
{
//we need to search on the left tree
return DeleteBST( &(*tree)->lchild, key);
}
else
{
return DeleteBST( &(*tree)->rchild, key);
}
}
}

int main()
{
//building up the BST
BSTree t = NULL;
int intarr[] = {1,100,67,48,98,55,12,79,9,36};
int length = sizeof(intarr)/sizeof(int);
int index = 0;
for( ; index < length; index++ )
{
printf(" %d",  intarr[index] );
InsertBST( &t, intarr[index]);
}
//Search for key 67
int key = 67;
printf("\nBST search %d...\n", key);
BSTree found = NULL;
int child = 1;
if( SearchBST( t, NULL, key, &found) )
{
printf("Found %d, check value %d\n", key, found->data);
printf("Check left child ");
if( found->lchild )
{
child = found->lchild->data;
printf("%d\n", child);
}
else
{
printf("NULL\n");
}
printf("Check right child ");
if( found->rchild )
{
child = found->rchild->data;
printf("%d\n", child);
}
else
{
printf("NULL\n");
}
}
else
{
printf("%d not found\n", key);
}
//delete child
if( DeleteBST(&t, child) )
{
printf("Deleted %d from tree\n", child);
printf("BST search %d again...\n", key);
if( SearchBST( t, NULL, key, &found) )
{
printf("Found %d, check value %d\n", key, found->data);
printf("Check left child ");
if( found->lchild )
{
child = found->lchild->data;
printf("%d\n", child);
}
else
{
printf("NULL\n");
}
printf("Check right child ");
if( found->rchild )
{
child = found->rchild->data;
printf("%d\n", child);
}
else
{
printf("NULL\n");
}
}
else
{
printf("%d not found\n", key);
}
}
else
{
printf("Delete %d failed\n", key);
}
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: