Binary search Tree

Source: Internet
Author: User

two fork Find tree (binary search tree) is also known as a binary trees, an ordered binary tree (ordered binary tree), a sort of binary (sorted binary trees), refers to an empty tree or a two-fork tree with the following properties:

    • If the left subtree of any node is not empty, the value of all nodes on the left subtree is less than the value of its root node;
    • If the right subtree of any node is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
    • The left and right subtrees of any node are also two-fork lookup trees, respectively.
    • There are no nodes with key values equal (no duplicate nodes).  

Supported operations:

    • Traverse
    • Inquire
      • Find
      • Maximum keyword element and minimum keyword element
      • Successors and precursors
    • Inserting and deleting

Pseudo code:

x = T.root

Traverse

// Middle Sequence Traversal inorder-tree-Walk (x)    if  x≠nil        inorder-tree-Walk (x.left)        print X.key        inorder-tree-Walk (x.right)

Find

//Recursive versiontree-Search (x,k)ifx = = NIL or k = =X.keyreturnxifK <X.keyreturntree-Search (x.left,k)Else        returntree-Search (x.right,k)//Iteration Versioniterative-tree-Search (x,k) whileX≠nil and K≠x.keyifK <X.key x=X.leftElsex=X.rightreturnX

Maximum keyword element and minimum keyword element

// Minimum keyword element tree-Min (x)    while  x.left≠nil        = x.left    return  x  // max keyword element tree-max (x)    while  x.left≠nil        = x.right     return X

Successors and precursors

//Middle Sequence Traversal successortree-successor (x)ifX.right≠nil//X has right child, then successor is the smallest key element of its right child        returntree-Min (x.right)//x No right child, if X is the left child, then the successor is the parent node, if X is the right child, then the successor is X's left child's lowest ancestor, may not exist (NIL)y =X.P whileY≠nil and x = =y.right x=y y=Y.Preturny//Middle sequence traversal precursortree-predeccessor (x)ifX.left≠nil//X has left child, then successor is the largest key element of its left child        returntree-Max (x.left)//x No left child, if the current node is his father's right child, then the father is his predecessor y =X.P whileY≠nil and x==Y.left x=y y=Y.PreturnY

Insert

//find the insertion position, and then determine if the insertion node is left child, right child, or root.tree-Insert (t,z) y= NIL//Record z parent nodex = T.root//record the insertion position of Z     whileX≠nil y=xifZ.key <X.key x=X.leftElsex=x.right Z.P=yify = =NIL T.root=ZElse ifZ.key <Y.key Y.left=ZElseY.right= Z

Delete

//replacing subtree U with subtree V and establishing a parent-child relationship between V and U.PTransplant (T,U,V)ifU.P = =NIL T.root=vElse ifU = =U.p.left U.p.left=vElseU.p.right=vifV≠nil V.P=U.P//Delete, in three different casestree-Delete (t,z)ifZ.left = NIL//left dial hand tree is emptyTransplant (T,z,z.right)Else ifZ.right = NIL//Right sub-tree is emptyTransplant (T,z,z.left)Else//It 's two different things .y = tree-min (z.right)//successor of Z        ifY.p≠z//y not Z's right child converts it to the right childTransplant (t,y,y,right) Y.right=z.right Y.RIGHT.P=y Transplant (t,z,y) Y.left= Z.left//establishing a parent-child relationship between Y and Z.leftY.LEFT.P = y

time complexity: two fork find tree search,minimum,maximum,predecessor,successor,insert,delete time complexity t (n) =o (h), O (LG N) =

the sequential traversal of binary search tree can get the ordered sequence of a keyword.  

Binary 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.