Binary_sort Two fork Tree

Source: Internet
Author: User

Implementation of binary tree introduction to algorithm English version of the implementation, the Chinese version of the implementation of a slightly different from the English version, I think the Chinese version of the way to change the key better, the English version of the 2-time exchange of pointers

function BinaryTree () {this.root=null;} Binarytree.prototype.inorder_tree_walk=function (x) {if (typeof x== ' undefined ') {x=this.root;} if (!! x) {this.inorder_tree_walk (x.left); Console.log (X.key); This.inorder_tree_walk (x.right);}} Binarytree.prototype.tree_search=function (X,key) {if (typeof x== ' undefined ') {x=this.root;} if (x==null| | Key==x.key) {return x;} if (Key<x.key) {return this.tree_search (X.left,key);} Else{return This.tree_search (X.right,key);}} Binarytree.prototype.minimun=function (x) {while (x.left!=null) {return This.minimun (x.left);} return x;} Binarytree.prototype.maximun=function (x) {while (x.right!=null) {return This.maximun (x.right);} return x;} Binarytree.prototype.successor=function (x) {if (x.right!=null) {return This.minimun (x.right);} var y=x.parent;while (y!=null&&x==y.right) {x=y;y=y.parent;} return y;} Binarytree.prototype.predecessor=function (x) {if (x.left!=null) {return This.maximun (x.left);} var y=x.parent;while (y!=null&&x==y.left) {x=y;y=y.parent;} return y;} /*node {left:null,right:nUll,key} */binarytree.prototype.insert=function (node) {var y=null;var x=this.root;while (x!=null) {y=x;if (Node.key <x.key) {x=x.left;} Else{x=x.right;}} Node.parent=y;if (y==null) {This.root=node;} else if (Node.key<y.key) {Y.left=node;} Else{y.right=node;}} function _transplant (t,u,v) {if (u.parent==null) {//delete node, node left null,node parent null = Node.right to be root ;//delete node, node right null,node parent NULL = Node.left to be root; T.root=v;} else if (u==u.parent.left) {//delete node is their parent left nodeu.parent.left=v;} Else{//delet node is their parent right node;u.parent.right=v;} if (v!=null) {//fix pointer;v.parent=u.parent;}} Binarytree.prototype.delete=function (node) {if (node.left==null) {_transplant (this,node,node.right);} else if (node.right==null) {_transplant (this,node,node.left);} Else{var Y=this.minimun (node.right); if (Y.parent!=node) {//successor left node must be Null;_transplant (this,y,y.right ); y.right=node.right;y.right.parent=y;} _transplant (this,node,y); Y.left=node.Left;y.left.parent=y;}} 

2 special cases of subsequent and prior succession

3 cases of Delete with the last of the two parents most complex, red black tree implementation based on the binary tree, the next wave will continue to review red and Black tree code, custom after the map

After several scenarios, the binary tree is an unbalanced tree that relies on the order of inserts, and the worst-case lookup is n, like a linked list

Binary_sort Two fork 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.