一.树节点的结构及创建,如下:
typedef struct TreeNode* link;
struct TreeNode {
unsigned char item;
link parent;
link left;
link right;
};
static link createNode(unsigned char item){
link p = (link)malloc(sizeof(*p));
p->item = item;
p->left = NULL;
p->right = NULL;
p->parent = NULL;
return p;
}
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8230489
二.树的创建,如下:
link SearchTree::Insert(link t,int key,int type){
if(type == RECURSIVE){
return recursiveInsert(t,key);
}
return iterativeInsert(t,key);
}
说明:这里可以基于两种方式创建树,即递归或迭代,递归的实现在之前一篇文字中已经实现,这里给出非递归的实现,如下:
link SearchTree::iterativeInsert(link t,int key){
link z = createNode(key);
link y = NULL;
link x = t;//t means the root of a tree
while(x != NULL){
y = x;//keep y being the parent of x,the while loop will continue util x is NULL;
if(z->item < x->item){//and this will be the insert place
x = x->left;
}else{
x = x->right;
}
}
z->parent = y;
if(y == NULL){//means the tree is empty,then the new insert node is supposed to be the root
t = z;
}else if(z->item < y->item){//else,we need to judge which child of y that z is supposed to be
y->left = z;
}else{
y->right = z;
}
return t;//return the root node
}
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8230489
三.树节点的查找,如下:
link SearchTree::Search(link t,int key,int type){
if(type == RECURSIVE){
return recursiveSearch(t,key);
}
return iterativeSearch(t,key);
}
说明:这里可以基于两种方式查找树的节点,即递归或迭代,递归的实现在之前一篇文字中已经实现,这里给出非递归的实现,如下:
link SearchTree::iterativeSearch(link t,int key){
while( t != NULL && key != t->item){
if(key < t->item){
t = t->left;
}else{
t = t->right;
}
}
return t;
}
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8230489
四.树节点的删除,如下:
link SearchTree::Delete(link t,int key,int type){//return the delete node
if(type == RECURSIVE){
return recursiveDelete(t,key);
}
return iterativeDelete(t,key);
}
说明:这里可以基于两种方式删除树的节点,即递归或迭代,递归的实现在之前一篇文字中已经实现,这里给出非递归的实现,如下:
link SearchTree::iterativeDelete(link t,int key){
link y = NULL;
link x = NULL;
link z = Search(t,key,SearchTree::ITERATIVE);
if(z->left == NULL || z->right == NULL){//means z has no more than one child
y = z;
}else{
y = treeSuccessor(z);//means z has two children,so y is z`s successor
}
if(y->left != NULL){//x is supposed to be the not-null child of y
x = y->left;
}else{
x = y->right;
}
if(x != NULL){
x->parent = y->parent;
}
if(y->parent == NULL){//only the root`s parent is NULL
t = x;
}else if(y == y->parent->left){
y->parent->left = x;
}else{
y->parent->right = x;
}
if(y != z){//means y is the node we will delete,no matter whether it is z`s successor or not
z->item = y->item;//means if not,then assign z with y`s value
}
return y;
}
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8230489
五.树的遍历,有三种方式,即前序,中序,后序,实现如下:
void SearchTree::preOrderWalk(link t){
if(t != NULL){
printf("%d ",t->item);
preOrderWalk(t->left);
preOrderWalk(t->right);
}
}
void SearchTree::midOrderWalk(link t){
if(t != NULL){
midOrderWalk(t->left);
printf("%d ",t->item);
midOrderWalk(t->right);
}
}
void SearchTree::posOrderWalk(link t){
if(t != NULL){
posOrderWalk(t->left);
posOrderWalk(t->right);
printf("%d ",t->item);
}
}
六.树的使用实例,即main.cpp函数的实现,如下:
int main(int argc, char** argv){
int i = 0;
int key = 0;
link root = NULL;
link searchResult = NULL;
SearchTree tree;
srand(time(NULL));
for(i=0;i<N;++i){
key = rand() % RANGE;
root = tree.Insert(root,key,SearchTree::ITERATIVE);
}
tree.setRoot(root);
printf("\t\\tree");
tree.printTree(root);
printf("\n\n");
printf("preOrderWalk:");
tree.preOrderWalk(root);
printf("\nmidOrderWalk:");
tree.midOrderWalk(root);
printf("\nposOrderWalk:");
tree.posOrderWalk(root);
searchResult = tree.Search(root,key,SearchTree::ITERATIVE);
if(searchResult != NULL){
printf("\nsearch key % d result is %d:",key,searchResult->item);
link successor = tree.treeSuccessor(searchResult);
link decessor = tree.treePredecessor(searchResult);
if(successor != NULL){
printf("\nsearchKey %d`s successor is %d.",key,successor->item);
}else{
printf("\nsearchKey %d`s successor is NULL.",key);
}
if(decessor != NULL){
printf("\nsearchKey %d`s decessor is %d.",key,decessor->item);
}else{
printf("\nsearchKey %d`s decessor is NULL.",key);
}
}else{
printf("\nsearch key % d result is NULL.",key);
}
printf("\ntree Minimum is %d.",tree.treeMinimum(root)->item);
printf("\ntree Maximum is %d.",tree.treeMaximum(root)->item);
if(searchResult != NULL){
link deleteResult = tree.Delete(root,searchResult->item,SearchTree::ITERATIVE);
if (deleteResult != NULL) {
printf("\ndeleteResult is %d.",deleteResult->item);
}
printf("\npreOrderWalk:");
tree.preOrderWalk(root);
printf("\nmidOrderWalk:");
tree.midOrderWalk(root);
printf("\nposOrderWalk:");
tree.posOrderWalk(root);
}
return 0;
}
说明:代码简洁明了,实现起来很简单,就不需再赘述了,源码就不上传了
转载请注明出处:山水间博客:http://blog.csdn.net/linyanwen99/article/details/8230489