Introduction to algorithms Part three--basic data structure--two-fork search tree

Source: Internet
Author: User

First, what is a two-fork search tree

Binary search tree is organized according to the binary tree structure, so it can be represented by a binary linked list structure. The keyword stored in a binary search tree satisfies the feature that sets X as a node in a two-fork lookup tree. If Y is a node in the left sub-tree of x, then key[y]≤key[x]. If Y is a node in the right subtree of x, then Key[x]≤key[y]. according to the characteristics of binary search tree, it is found that the key words in the tree can be small to large by using the middle root to traverse a binary search tree.

Binary tree lookup, max/small, precursor, and subsequent pseudocode: The complexity is all H

//Search Recursive versionTree_search (x,k)ifX=null or k=Key[x] Thenreturnxif(k<key[x]) ThenreturnTree_search (left[x],k)Else ThenreturnTree_search (right[x],k)//Search Iteration VersionIterative_tree_search (x,k) whileX!=null and k!=Key[x] Do ifk<Key[x] then x=Left[x]ElseThen x=Right[x]returnx//Minimum Fingertree_minmum (x) whileLEFT[X]! =NULL Dox=Left[x]returnx//Maximum Valuetree_maxmum (x) whileRIGHT[X]! =NULL Dox=Right[x]returnx//successortree_processor (x)//right child non-empty, returns the minimum value of the right subtree    ifRIGHT[X]! =NULL ThenreturnTree_minmum (right (x))//The right child is empty, look up the successory=Parent[x] whiley!= NULL and x = =Right[y] Dox =y y=Parent[y]returnY

The insertion and deletion complexity of binary tree is H

Insertions and deletions cause changes in the dynamic set represented by the binary lookup, and the difficulty is to maintain the nature of the binary lookup tree during insertions and deletions. The insertion process is fairly straightforward, and deleting nodes is more complex.

(1) Insert

The location of the insertion node corresponds to the location of the node in the lookup process when the lookup is unsuccessful, so you need to find the location with the insertion node from the root node and insert it after locating the location. Insert the node process as shown:

The pseudo-code for the insertion process is given in the book:

Tree_insert (t,z) y=NULL; X=Root[t] whileX! =NULL Doy =xifKEY[Z] <Key[x] then x=Left[x]Elsex=Right[x] Parent[z]=yify=NULL then root[t]=ZElse ifKey[z]>Key[y] then Keft[y]=ZElseRight[y] =z

(2) Delete

Delete the given node z from the two-fork lookup tree in three scenarios:

<1> node Z has no left and right subtree, modify its parent p[z] to NULL. The removal process is as follows:

<2> if node Z has only one subtree (left subtree or right subtree), delete Z by establishing a chain at its child nodes with the parent node. The removal process is as follows:

<3> if Z has two children, delete the successor y of Z (Y has no left child) and replace the contents of Z with the contents of Y.

The pseudo-code for the removal process is given in the book:

Tree_delete (t,z)ifLeft[z] ==null or right[z] = =NULL then y=ZElsey=Tree_successor (z)ifLeft[y]! =NULL then x=Left[y]Elsex=Right[y]ifx!=NULL then parent[x]=Parent[y]ifP[y] = =NULL then root[t]=xElse ify =Left[[prarnt[y] [then Left[parent[y]]=xElseRight[parent[y]] =xify!=Z then key[z]=key[y] Copy y's data into Z     returnY

Introduction to algorithms Part three--basic 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.