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-fork tree with a property such as the following:
⑴ 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 value of all nodes on the right subtree is greater than the value of the root node;
⑶ 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 Keywords->key node is compared with the root keywordt->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. The insertion process in the subtree and the insertion process in the tree are also
This goes on until the node *s as a new leaf is inserted into the two-fork sorting tree, or until the discovery tree has the same keyword node.
3. Two fork sort tree generation:
Starting with an empty two-fork sorting tree, a binary sort tree is generated after a series of lookup inserts.
Description
① each new node that is inserted is a new leaf node on the two-fork sorting tree.
② by different sequential keyword sequences, different binary sorting trees will be obtained.
③ constructs a binary sort tree for a random keyword sequence, and actually sorts the keyword on the qualitative.
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 keyword is key in the binary sort tree T, 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 keyword is key in the binary sort tree T, 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 an element of keyword is not present in the binary sort tree T, the key is inserted, and the root of the tree is returned,
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 visited 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:
If 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, it is only necessary to change its parent node *f pointer.
⑵ If the node *p only have left dial hand tree pl or only the right sub-tree PR, then only the PL or PR become the parent node of the left subtree 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.
② replaces *p (that is, copying *s data into *p) with the *p of the middle sequence *s, and links the *s left subtree to the left (or right) chain of *s's parent node *q. (This method is used in the following demonstration algorithms)
6. Delete Algorithm Demo:
Delete a node in a binary sort tree
Delete the node keyword as key 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 node where keyword is key
f=p;//Note the parent node of the Keywordkey 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 keyword as key in the sorting 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;//p (i.e. copy s data to P) with P's middle order forward node S.
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 in the lookup process and 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 form the same.
The worst case scenario is that the two-fork sort tree is a single tree, and the average lookup length and order lookup are the same.
Worst Case Demo Sample
In terms of average performance, there is little difference between finding and binary search on binary sort tree, and the insertion and deletion nodes on binary sort tree are very convenient, so there is no need to move nodes much.
Binary sort Tree