Data structure-two-fork search tree

Source: Internet
Author: User

The nature of making a two-fork tree a binary lookup tree is that for each node x in the tree, all the key values in its left subtree are less than the keyword value of x, and all the key values in its right subtree are greater than the key value of X. This means that all elements of the tree are sorted in a uniform way.

Declaration of a binary lookup tree

Binary search tree is a special two-fork tree, the structure of the nodes in the binary search tree is the same as the structure of the two-fork tree nodes, and the key is the operation that can be performed on the binary lookup tree and the algorithm to manipulate it. The binary lookup tree is declared as follows:

#ifndef _tree_h

struct TreeNode;

typedef struct TREENODE *position;

typedef struct TREENODE *searchtree;

Searchtree makeempty (Searchtree T);

Position Find (ElementType x,searchtree T);

Position findmin (Searchtree T);

Position Findmax (Searchtree T);

Searchtree Insert (ElementType x,searchtree T);

Searchtree Delete (ElementType x,searchtree T);

ElementType Retrieve (Position p);

#endif

Binary Lookup tree node structure:

struct TreeNode

{

ElementType Element;

Searchtree left;

Searchtree right;

};

Makeempty operation

Makeempty is mainly used to build an empty tree. The routines are as follows:

Searchtree makeempty (Searchtree T)

{

if (t!=null)//recursive termination condition

{

Makeempty (T->left); Makeempty (T->right);

Free (T);//The operation actually performed, freeing space

}

return NULL;

}

The recursive algorithm must have a recursive termination condition, where the terminating condition is that the root node of the tree (subtree) is null.

Find operation

The find operation is a pointer to the node in the tree that holds the key element to retrieve, the return value is a pointer to the node type, and returns null if not found. The important feature of binary lookup trees is that element permutations are sequential, so finding elements can be "direction" based on this feature (looking for a branch), rather than retrieving all elements, so that the time complexity of the lookup operation is up to two of the depth of the lookup tree, and the average depth of the binary lookup tree is O ( LOGN), the time complexity of the lookup operation of the binary lookup tree is O (logn), and the O (N) relative to the linear data structure is greatly improved.

It is very simple to use recursive algorithm to find the recursive property of binary search tree. The core idea is to select the direction of the lookup (left and right subtrees) or find the element you are looking for by comparing the element you are looking for with the element at the (subtree) root node (greater than or equal to). Handle the case where the root node is empty at the same time. The code is as follows:

Position Find (ElementType x,searchtree T)

{

if (t==null)

return NULL;

if (x<t->element)

Return Find (X,t->left);

else if (x>t->element)

Return Find (x,t->right);

Else

return T;

}

Findmin and Findmax operations

Findmin and Findmax operation is relatively simple, find the minimum value only need to find the leftmost node, the maximum value only need to find the rightmost node. The algorithm only needs to return to the previous node when the left (right) subtree is empty, as well as to be aware of the case where the initial null is the first. The most intuitive way to operate on a tree structure is to use recursion, but you can also use the idea of iteration. Here Findmin uses recursion, Findmax uses iterations, and the code is as follows:

Position findmin (Searchtree T)

{

if (t==null)

return NULL;

Else

if (t->left==null)

return T;

Else

Return Findmin (T->left);

}

Positon Findmax (Searchtree T)

{

if (t!=null)

while (T->right!=null)

t=t->right;

return T;

}

Insert operation

The insert operation routine is also relatively simple, similar to the find operation, the key is to find where to insert the element should be in place, the location will need to create a node, and the newly created node is "connected" with the original tree (the pointer of the newly created node is placed to the left or right child node of its parent node pointer position). Using recursive thinking to handle the insertion operation, the key is to find the recursive termination condition, where the recursive termination condition is to insert the tree into an empty tree (the case of the root node is null). The specific code is as follows:

Searchtree Insert (ElementType x,searchtree T)

{

if (t==null)//recursive termination condition

{

T=malloc (sizeof (struct Treenode));

If (T==null)

ERROR;

Else

{

t->element=x;

t->left=t->right=null;

}

}

Else

{

if (x<t->element)

T->left=insert (X,t->left);

If (x>t->element)

T->right=insert (X,t->right);

When the element to be inserted already exists, the operation needs to look at the tree itself as defined

return T;

}

}

When the element you want to insert already exists, you can save the frequency of the element in a new field in the node. Or the key value is just part of a larger data structure, and you can use a linked list or another tree to hold the same key that is worth the data. The specific needs to be decided.

Delete operation

A delete operation is the most complex operation in all operations, because deleting a node in a tree is more than just freeing up a piece of memory space. After you remove a node from the tree, you also need to maintain the attributes of the binary lookup tree (the elements in the left subtree of the root node are smaller than the elements in the root node, and the elements in the right subtree are larger than the elements in the root node). The simple case is that the node to be deleted has only one child node or no child nodes. When there are no child nodes, delete the node directly (freeing the space occupied by the node). You also need to set one of the child nodes of its parent node to NULL. (in the recursive processing tree operation, only need to consider the current node as the root of the subtree, its parent node processing is after the recursive function returned after processing, that is, as long as the current node to consider how to handle the call to its child nodes after the return value of the function, then the recursive mechanism runs the current root node of the parent node processing, This is the idea of recursive thinking of processing. When there is only one child node, delete the current node, and the child nodes directly as child nodes of the parent node of the node to be deleted. Complex is when there are two sub-nodes, then the left dial hand tree needs to remain unchanged, and delete the node to be deleted, while the smallest node in the right subtree to replace the deleted node, the right subtree of the smallest node of the left subtree is set to the left subtree of the deleted node, Set one of the child node pointers of the parent node of the deleted node to itself at the same time. Also, you need to remove yourself from the right subtree (the original smallest node). At this point, because the minimum node has at most only the right subtree, it can be processed using the case where the node is the most child node. In fact, because the parent node of the current processing node is difficult to obtain because of the recursive thought, it is relatively simple to delete the node with two child nodes, just to change the data to the data of the smallest node in its right subtree. The specific code is as follows:

Searchtree Delete (Element x,searchtree T)

{

Position Tempcell;

If (T==null)

Error ("Element not Found");

Else

If (x<t->element)

T->left=delete (X,t->left);

Else

If (x>t->element)

T->right=delete (X,t->right);

Else//found the element to delete

If (t->left&&t->right)//left and right subtrees are not empty

{

Tempcell=findmin (T->right);

t->element=tempcell->element;

T->right=dlete (T->element,t->right);

}

else//Only Zuozi or right subtree, or No child nodes

{

tempcell=t;

if (t->left==null)

t=t->right;

If (T->right=null)

t=t->left;

Free (Tempcell);

}

return T;

}

Data structure-two-fork search tree

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.