Introduction to Algorithms learning Notes--12th Chapter two fork Find tree

Source: Internet
Author: User

Binary Search Tree Properties

Set X is a node in the two-fork lookup tree, and if Y is a node in the left subtree of x, then k[y]<=key[x]; if Y is a node in the right subtree k[y]>=k[x]

1 // Middle sequence traversal algorithm, output binary find all elements in tree T 2 inorder-tree-WALK (x)3if x!=nil4then     inorder-tree- WALK (left[x]) 5             print key[x]6             Inorder-tree-walk (Right[x])

Find

1 //Recursive version2tree-SEARCH (x,k)3 ifX=nil or k=Key[x]4Thenreturnx5 ifk<Key[x]6Thenreturntree-SEARCH (left[x],k)7     Else returntree-SEARCH (right[x],k)8 9 //non-recursive versionTeniterative-tree-SEARCH (x,k) One  whileX!=nil and k!=Key[x] A      Do ifk<Key[x] - Then X←left[x] -         ElseX←right[x] the returnX

Maximum elements, minimum elements, next and Prev

1 treeminimum (x)2  whileleft[x]!=Nil3      DoX←left[x]4 returnx5 6tree-MAXIMUM (x)7  whileright[x]!=Nil8      DoX←right[x]9 returnxTen  Onetree-successor (x) A ifright[x]!=Nil -Thenreturntree-Nimimum (right[x]) - Y←p[x] the  whileY!=nil and x=Right[y] -      DoX←y - Y←p[y] - returnY

Insert

1tree-INSERT (t,z)2 Y←nil3 X←root[t]4  whilex!=Nil5      Doy←x6         ifkey[z]<Key[x]7 Then X←left[x]8             ElseX←right[x]9 P[z]←yTen ify=Nil One Then root[t]←z A     Else ifKey[z]←key[y] - Then left[y]←z -         ElseRight[y]←z

Delete

1tree-DELETE (t,z)2 ifLeft[z]=nil or right[z]=Nil3 Then y←z4     Elsey←tree-successor (z)5 ifleft[y]!=Nil6 Then X←left[y]7     ElseX←right[y]8 ifx!=Nil9 Then P[x]←p[y]Ten ifp[y]=Nil One Then root[t]←x A     Else ify=Left[p[y]] - Then left[p[y]]←x -         Elseright[p[y]]←x the ify!=x - Then Key[z]←key[y] -Copy y's satellite data into Z - returnY

Introduction to Algorithms learning Notes--12th Chapter two fork Find 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.