(Online two fork sorting tree deleted data is very few, this article is very good.) Turn from: http://bbs.csdn.net/topics/110010437)
Two fork Sort tree Delete :
For a typical two-fork tree, it makes no sense to delete a node in the tree, because it will make the subtree of the root of the deleted node into a forest, destroying the structure of the whole tree.
However, for a binary sort tree, deleting a node in a tree is equivalent to deleting a record in an ordered sequence, as long as the character of the binary sort tree is not changed after a node is deleted.
The algorithm for deleting a node on a binary sort tree is as follows:
Btree * DELETEBST (btree *b, Elemtype x)
{
if (b)
{
if (B->data = = x)
B = Delnode (b);
else if (B->data > x)
b->lchild = Deletebst (B->lchild, x);
else
b->rchild = Deletebst (B->rchild, x);
}
return b;
}
There are two ways to delete a procedure.
The first process is as follows:
1. If P has a left dial hand tree, find the rightmost leaf node r of the left subtree, replace p with the leaf node R, and turn R's left child
As R's father's right child.
2. If p does not have a left subtree, replace it directly with P's right child.
The second process is as follows:
1. If P has a left dial hand tree, replace it with P's left child; find the rightmost leaf node r of the left subtree, and use the right subtree of P as R
Right sub-tree.
2. If p does not have a left subtree, replace it directly with P's right child.
Both methods have advantages and disadvantages, the first operation is a little bit simpler, but the balance is not as good as the second, because it will node P's right sub-tree
All moved to the left. The following will be written in two different ways of code.
The first type:
Btree * Delnode (btree *p)
{
if (p->lchild)
{
Btree *r = p->lchild; R points to its left subtree;
while (r->rchild! = NULL)//Search the rightmost leaf node of the left subtree R
{
r = r->rchild;
}
R->rchild = p->rchild;
Btree *q = p->lchild; Q points to its left subtree;
Free (p);
return q;
}
else
{
Btree *q = p->rchild; Q points to its right sub-tree;
Free (p);
return q;
}
}
The second type:
Btree * Delnode (btree *p)
{
if (p->lchild)
{
Btree *r = p->lchild; R points to its left subtree;
Btree *prer = p->lchild; Prer points to its left subtree;
while (r->rchild! = NULL)//Search the rightmost leaf node of the left subtree R
{
prer = R;
r = r->rchild;
}
if (prer! = r)//if R is not P's left child, put R's left child as R's father's right child
{
prer->rchild = r->lchild;
R->lchild = p->lchild; The left subtree of the deleted node p as the left subtree of R
}
R->rchild = p->rchild;//The right subtree of the deleted node p as the right subtree of R
(p);
return r;
}
else
{
Btree *q = p->rchild; Q points to its right sub-tree;
Free (p);
return q;
}
}
But the above method, move R to move, very easy error, in fact, here we delete only P element value, not its address, so there is absolutely no need to move the pointer. After careful observation, we find that the address we deleted is actually the right-most leaf node R address of P's left subtree, so we just need to fill out the R data into P and then delete the R.
The algorithm is as follows:
Btree * Delnode (btree *p)
{
if (p->lchild)
{
Btree *r = p->lchild; R points to its left subtree;
Btree *prer = p->lchild; Prer points to its left subtree;
while (r->rchild! = NULL)//Search the rightmost leaf node of the left subtree R
{
prer = R;
r = r->rchild;
}
P->data = r->data;
if (prer! = r)//if R is not P's left child, put R's left child as R's father's right child
Prer->rchild = r->lchild;
else
p->lchild = r->lchild;//Otherwise the left subtree of the node p points to the left subtree of R free
(r);
return p;
}
else
{
Btree *q = p->rchild; Q points to its right sub-tree;
Free (p);
return q;
}
}
http://www.programfan.com/
——————————————————————————————————————————————————————————————————
1. The concept of a two-fork sorting tree:
Binary sort tree is a dynamic tree table.
Binary sort tree definition: Two fork sort tree or an empty tree,
Or a two-prong tree with the following properties:
⑴ if its left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of the root node;
⑵ if its right subtree is not empty, the values of all nodes on the right subtree are greater than the values of the root nodes;
⑶ the left and right sub-tree itself is a binary sorting tree. The nature of binary sort tree: by traversing the binary sort tree in the middle order, the resulting middle sequence traversal sequence is an ascending ordered sequence
2. Inserting a two-fork sort tree:
Insert a new node in the binary sort tree, to ensure that the inserted two-fork tree still conforms to the definition of the binary sort tree.
Insert process: If the binary sorting tree is empty, the insertion node *s as the root node into the empty tree;
When non-empty, the S->key keyword is compared with the tree root keyword, t->key.
If S->key = T->key, it is not necessary to insert, if s->key< T->key, it is inserted into the left subtree of the root,
If S->key> T->key, it is inserted into the right subtree of the root. And the insert process in the subtree is the same as the insert process in the tree,
So proceed until the node *s as a new leaf is inserted into the two-fork sorting tree, or until the tree has found a node with the same keyword.
3. Two fork sort tree generation:
Starting with an empty two-fork sort tree, a binary sort tree is generated after a series of find-and-insert operations.
Description
① each new node that is inserted is a new leaf node on the two-fork sorting tree.
② by a sequence of different sequences of keywords, you get different binary sort trees.
③ constructs a binary sort tree for an arbitrary keyword sequence, which essentially sorts the keywords.
4. The program implementation of the two-fork sorting tree Lookup:
#include <malloc.h>
#include <iostream.h>
#include <stdio.h>
typedef struct bitnode{
int data;
int flag;
struct Bitnode *lchild,*rchild;
}btnode,btree;
Lookup non-recursive algorithm for binary sorting tree
Find the element in the binary sort tree T that the keyword is key, and if found returns True,
Otherwise, false is returned.
BOOL Searchbst (BTree *t,int key) {
BTree *p=t;
while (p) {
if (P->data==key)
return true;
p= (key<p->data)? p->lchild:p->rchild;
}
return false;
}
Search recursive algorithm for binary sorting tree
Find the element in the binary sort tree T that the keyword is key, and if found returns True,
Otherwise, false is returned.
bool SearchBST2 (BTree *t,int key) {
BTree *p=t;
if (!p)
return false;
Else
if (P->data==key)
return true;
Else
if (Key>p->data)
Return SearchBST2 (P->rchild,key);
Else
Return SearchBST2 (P->lchild,key);
}
Set up a two-fork sorting tree
When there is no element in the binary sort tree T that has the keyword key, insert key and return the root of the tree,
Otherwise, it is not inserted and returns the root of the tree.
btree* Insertbst (BTree *t,int key) {
BTree *f=t,*p=t;
while (p) {
if (P->data==key) return T;
f=p;//use F to make a note of the last accessed node on the lookup path
p= (key<p->data)? p->lchild:p->rchild;
}
p= (btnode*) malloc (sizeof (Btnode));
p->data=key;
p->lchild=p->rchild=null;
if (t==null)
t=p;
Else
if (Key<f->data)
f->lchild=p;
Else
f->rchild=p;
return T;
}
Recursive middle order traversal
void Inorderdisplay (BTree *t) {
if (T) {
Inorderdisplay (T->lchild);
cout<<t->data;
Inorderdisplay (T->rchild);
}
}
Test
int main () {
int i;
int data;
BTree *tree=null;
for (i=0;i<4;i++) {
cout<< "Input data" <<endl;
cin>>data;
Tree=insertbst (Tree,data);
}
Inorderdisplay (tree);
BOOL Find=searchbst2 (tree,24);
cout<<find<<endl;
return 0;
}
5. Deletion of the two-fork sorting tree:
Assuming that the deleted node is *p, whose parents are *f, without losing generality, *p is the left child of *f, the following three kinds of situations are discussed:
⑴ If the node *p is a leaf node, simply modify the pointer of the parent node *f.
⑵ If the node *p only left dial hand tree pl or only the right sub-tree PR, then as long as the PL or PR becomes its parent node of the left sub-tree can be.
⑶ if the *p of the left and right sub-tree are not empty, first find the *p of the middle order before the *s (note *s is the left subtree of the right node, the right link is empty), and then there are two ways:
① the left subtree of the *p directly to the left chain of the *p's parent node *f, and the *p right subtree chain to the *p's right strand in the pre-order node *s.
② the *p *s instead of *p (that is, copying the *s data into the *p), the *s left subtree is chained to the left (or right) chain of *s's parent node *q. (This method is used in the demo algorithm below)
6. Delete Algorithm Demo:
Delete a node in a binary sort tree
Delete the keyword key node in the binary sort tree T
void Delbst (BTree *t,int key) {
BTree *p=t,*f,*q,*s,*root=t;
while (p) {
if (P->data==key) break; Find the key keyword for the node
f=p;//Note the parent node of the keyword key node
p= (key<p->data) p->lchild:p->rchild;//found in the left and right subtrees of *p respectively
}
if (!p) return;//two forks the node with no key in the sort tree
if (p->lchild==null&&p->rchild==null) {//p has no left and right subtree
if (p==t) t=null;//The root node is deleted
Else
if (p==f->lchild)
f->lchild=null;
Else
f->rchild=null;
Free (p);
}
else if (p->lchild==null&&p->rchild!=null)//p no left subtree has right subtree
{
if (f->lchild==p)
f->lchild=p->rchild; Link the right subtree of p to the left chain of its parent node
Else
f->rchild=p->rchild; Link the right subtree of p to the right strand of its parent node
Free (p);
}
else if (p->rchild==null&&p->lchild!=null)//p has left dial hand tree without right subtree
{
if (f->lchild==p)
f->lchild=p->lchild; Link the left subtree of P to the left chain of its parent node
Else
f->rchild=p->lchild; Link the left subtree of P to the right chain of its parent node
Free (p);
}
else if (p->lchild!=null&&p->rchild!=null)//p both Zuozi and right subtree
{
Q=p;
s=p->lchild;//Turn Left
while (S->rchild) {//Then right to end
Q=s;
S=S->RCHILD;//S pointing to the "precursor" of the truncated point (the precursor of the middle order)
}
p->data=s->data;//to P (that is, the data of S is copied to p).
if (q!=p) q->rchild=s->lchild;//re-connect the right subtree of q
Else q->lchild=s->lchild;//the left subtree of Q again.
Free (s);
}
}
7. Find the two-fork sort tree:
The process of finding in a binary sort tree is similar to a binary lookup, and it is also a process of narrowing down the search scope. If the search succeeds, it is a path from the root node to the unknown origin node, and if the lookup fails, it is the path of a root node to a leaf node. Therefore, the number of times the lookup process is compared to the keyword does not exceed the depth of the tree.
Because the two-fork sort tree with n nodes is not unique, the morphology and depth may be different. Therefore, the average search length of the two-fork sorting tree with n nodes is related to the tree morphology.
The best case scenario is that the two-fork sort tree and the two-fork decision tree have the same shape.
Worst case scenario: a two-fork sort tree is a single tree, when the average lookup length is the same as the order lookup.
Worst Case Example
In terms of average performance, there is little difference between finding and binary search on binary sort tree, and it is convenient to insert and delete nodes on binary sort tree without moving nodes.