Data Structure Tutorial 31st Class Dynamic Lookup table

Source: Internet
Author: User
Tags definition empty insert sort

Teaching Objective: grasping the realization method of binary sort tree

Teaching emphases: The realization of two fork sorting tree

Teaching Difficulty: The method of constructing two-fork sort tree

Teaching Content:

Definition of a dynamic lookup table

Dynamic lookup tables are characterized by:

The table structure itself is dynamically generated in the lookup process, that is, for a given value key, if there is a record in the table whose keyword equals key, then the search returns successfully, otherwise the Insert keyword equals Key's record. Politics is the definition of a dynamic lookup table:

ADT dymanicsearchtable{

A data Object D:D is a collection of data elements with the same attributes. Each data element contains the same type, which uniquely identifies the data element.

Data relation R: A data element is a collection of the same genus.

Basic Operations P:

Initdstable (&DT);

Destroydstable (&DT);

Searchdstable (Dt,key);

Insertdstable (&dt,e);

Deletedstable (&dt,key);

Traversedstable (Dt,visit ());

}adt dynamicsearchtable

Two or two fork sorting tree and its lookup process

A binary sort tree or an empty tree, or a two-pronged tree with the following properties:

1. If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node;

2. If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;

3, its left and right subtree of the tree are two fork sorting tree.

If you take the two-fork list as the storage structure for the binary sort tree, the lookup process is as follows:

Bitree Searchbst (Bitree t,keytype key) {

if (! T) | | EQ (Key,t->data.key)) return (T);

else if LT (Key,t->data.key) return (Searchbst (T->lchild,key));

else return (SEARCHBST (T->rchild.key));

}//searchbst

Insert and delete of three or two fork sort tree

Binary sort tree is a dynamic tree table, which is characterized by the structure of the tree is usually not a capital generation, the face is in the search process, when the tree does not exist when the keyword is equal to the value of the node and then inserted. The newly inserted node must be a newly added leaf node and is the left or right child node that finds the last node accessed on the path when it is unsuccessful.

Status Searchbst (bitree t,keytype key,bitree f,bitree &p) {

if (! T) {P=f;return FALSE;}

else if EQ (key,t->data.key) {P=t;return TRUE;}

else if LT (Key,t->data.key) Searchbst (t->lchild,key,t,p);

else Searchbst (t->rchild,key,t,p);

}//searchbst

Insert algorithm:

Status Insertbst (Bitree &t,elemtype e) {

if (! Searchbst (t,e.key,null,p) {

S= (bitree) malloc (sizeof (Bitnode));

s->data=e;s->lchild=s->rchild=null;

if (!p) t=s;

else if (LT (E.key,p->data.key) p->lchild=s;

else p->rchild=s;

return TRUE;

}

else return FALSE;

}//insertbst

Algorithm for deleting a node in a binary sort tree:

Status Deletebst (Bitree &t,keytype key) {

if (! T) return FALSE;

else{

If EQ (Key,t->data.key) Delete (T);

else if LT (Key,t->data.key) Deletebst (T->lchild,key);

else Deletebst (T->rchild,key);

return TRUE;

}

}

void Delete (Bitree &p) {

if (!p->rchild) {

Q=p; p=p->lchild; Free (q);

}

else if (!p->lchild) {

q=p;p=p->rchild; Free (q);

}

else{

Method One: As illustrated

q=p;s=p->lchild;

while (S->rchild) {q=s;s=s->rchild}//to left, then to the right to the end

p->data=s->data; s point to the "precursor" of the deleted node.

if (q!=p) q->rchild=s->lchild; Re-connect the right subtree of *q

else q->lchild=s->lchild;//*q Zuozi (method one end)

Or Available method two:

Q=s= (*p)->l;

while (S->r) s=s->r;

S->r= (*p)->r;

Free (*p);

(*p) =q;

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.