二叉树:是每个结点最多有两个子树的树结构,通常子树被称作“左子树”和“右子树”,二叉树常被用于实现二叉查找树和二叉堆。
完全二叉树:若二叉树的高度为h,除了第h层外,其它各层(1~h-1)的结点数都达到了最大个数,第h层有叶子节点,并且叶子结点都是从左至右依次排布,这就是完全二叉树
满二叉树:除最后一层无任何子节点外,每一层上的所有结点都有两个子结点的二叉树。
平衡二叉树:有名AVL树,它是一颗二叉排序树,具有以下性质它是一颗空树或者左右子树的高度差绝对值不超过1,并且左右子树都是一颗平衡树。
二叉排序树:又被称为二叉查找树,或二叉搜索树
特点:若左子树不空,则左子树所有节点的值均小于它的根节点的值
若右子树不空,则右子树所有节点的值均大于它的根节点的值
左右子树也分别为二叉排序树
没有键值相等的节点
二叉查找树结构体定义
typedef int KeyType;
struct BstNode
{
BstNode *leftchild;
BstNode *parent;
BstNode *rightchild;
KeyType key;
};
typedef struct
{
BstNode * head;
int cursize;
}BSTree;
BstNode * Buynode(BstNode *pa = NULL)
{
BstNode *s = (BstNode*)malloc(sizeof(BstNode));
if(NULL == s) exit(1);
memset(s,0,sizeof(BstNode));
s->parent = pa;
return s;
}
void Freenode(BstNode *p)
{
free(p);
}
void InitBSTree(BSTree &bst)
{
bst.head = Buynode(); // head // root;
bst.cursize = 0;
}
void DestroyBSTree(BSTree &bst)
{
}
BstNode * FindValue(BSTree &bst,KeyType kx)
{
BstNode *p = bst.head->parent;// root;
while(p != NULL && p->key != kx)
{
p = kx < p->key? p->leftchild:p->rightchild;
}
return p;
}
BstNode * Search(BstNode *ptr,KeyType kx)
{
if(ptr == NULL || ptr->key == kx)
return ptr;
else if(kx < ptr->key)
return Search(ptr->leftchild,kx);
else
return Search(ptr->rightchild,kx);
}
BstNode *SearchValue(BSTree &bst,KeyType kx)
{
return Search(bst.head->parent,kx);
}
BstNode *First(BstNode *ptr)
{
while(ptr != NULL && ptr->leftchild != NULL)
{
ptr = ptr->leftchild;
}
return ptr;
}
BstNode *Next(BSTree &myt,BstNode *ptr)
{
if(ptr == NULL || ptr == myt.head) return NULL;
if(ptr->rightchild != NULL)
{
return First(ptr->rightchild);
}
else
{
BstNode *pa = ptr->parent;
while(pa != myt.head && pa->leftchild != ptr)
{
ptr = pa;
pa = pa->parent;
}
if(pa == myt.head)
{
pa = NULL;
}
return pa;
}
}
void NiceInOrder(BSTree &bst)
{
for(BstNode *p = First(bst.head->parent);
p != NULL ; p = Next(bst,p))
{
cout<<p->key<<" ";
}
cout<<endl;
}
bool InsertBST(BSTree &myt,KeyType kx)
{
BstNode *pa = myt.head; // head;
BstNode *p = myt.head->parent; // root;
while(p != NULL && p->key != kx)
{
pa = p;
p = kx < p->key? p->leftchild:p->rightchild;
}
if(p != NULL && p->key == kx) return false;
p = Buynode(pa);
p->key = kx;
if(pa == myt.head)
{
myt.head->parent = p;
myt.head->leftchild = p;
myt.head->rightchild = p;
}
else
{
if(p->key < pa->key)
{
pa->leftchild = p;
if(p->key < myt.head->leftchild->key)
{
myt.head->leftchild = p;
}
}
else
{
pa->rightchild = p;
if(p->key > myt.head->rightchild->key)
{
myt.head->rightchild = p;
}
}
}
myt.cursize+=1;
return true;
}
bool RemoveBST(BSTree &myt,KeyType kx)
{
BstNode *p = FindValue(myt,kx);
if(NULL == p) return false;
if(p->leftchild != NULL && p->rightchild != NULL)
{
BstNode *s = Next(myt,p);
p->key = s->key;
p = s;
}
BstNode *pa = p->parent;
BstNode *child = p->leftchild != NULL? p->leftchild:p->rightchild;
if(child != NULL) child->parent = pa;
if(pa == myt.head)
{
myt.head->parent = child;
}
else
{
if(p == pa->leftchild)
{
pa->leftchild = child;
}
else
{
pa->rightchild = child;
}
}
Freenode(p);
myt.cursize-=1;
return true;
}
int main()
{
int ar[]={53,17,78,9,45,65,87,23,81,94,88,17,23};
int n = sizeof(ar)/sizeof(ar[0]);
int kx;
BSTree myt;
InitBSTree(myt);
for(int i = 0;i<n;++i)
{
cout<<InsertBST(myt,ar[i])<<" ";
}
cout<<endl;
NiceInOrder(myt);
while(cin>>kx,kx != -1)
{
RemoveBST(myt,kx);
NiceInOrder(myt);
}
DestroyBSTree(myt);
return 0;
}