Binary Search Tree (C #)

Source: Internet
Author: User
Binary Search Tree (C #)

Code implementation (the product of your own learning process, only for your own play)

 

/// <Summary> <br/> // Binary Search Tree <br/> /// </Summary> <br/> public class binarysearchtree <br/> {< br/> # region constructor </P> <p> Public binarysearchtree (binarysearchtreenode root) <br/>{< br/> This. root = root; <br/>}</P> <p> Public binarysearchtree (long [] nodevalues) <br/>{< br/> If (nodevalues = NULL | nodevalues. length = 0) return; </P> <p> This. root = new binarysearchtreenode () <br/>{< br/> value = Nodevalues [0] <br/>}; </P> <p> for (INT I = 1; I <nodevalues. length; I ++) <br/>{< br/> addnode (nodevalues [I]); <br/>}</P> <p> # endregion </P> <p> # region attribute region </P> <p> Public binarysearchtreenode Root <br/>{< br/> get; <br/> set; <br/>}</P> <p> # endregion </P> <p> # region function area </P> <p> // <summary> <br />/// obtain the node with the maximum value <br/> /// </Summary> <br/> /// <returns> nodes with the maximum value </returns> <br/> Public Bin Arysearchtreenode getmaxnode () <br/>{< br/> return getmaxnode (Root ); <br/>}</P> <p> // <summary> <br/> // obtain the node with the maximum value <br/> /// </Summary> <br/> // <returns> nodes with the maximum value </returns> <br/> Public binarysearchtreenode getmaxnode (binarysearchtreenode root) <br/>{< br/> binarysearchtreenode maxnode = root; <br/> binarysearchtreenode rightnode = root. right; <br/> while (rightnode! = NULL) <br/>{< br/> maxnode = rightnode; <br/> rightnode = rightnode. right; <br/>}</P> <p> return maxnode; <br/>}</P> <p> // <summary> <br/> // obtain the maximum value of the Binary Search Tree. <br/> /// </ summary> <br/> // <returns> maximum value of the binary query tree </returns> <br/> Public long getmax () <br/>{< br/> binarysearchtreenode node = getmaxnode (Root); <br/> If (node = NULL) throw new exception ("maximum value does not exist "); </P> <p> return node. value; <br/>}</P> <p> /// <Summary> <br/> // obtain the node with the minimum value <br/> /// </Summary> <br/> // <returns> nodes with minimum values </returns> <br/> Public binarysearchtreenode getminnode () <br/>{< br/> return getminnode (Root ); <br/>}</P> <p> // <summary> <br/> // obtain the node with the minimum value <br/> /// </Summary> <br/> // <returns> nodes with minimum values </returns> <br/> Public binarysearchtreenode getminnode (binarysearchtreenode root) <br/>{< br/> binarysearchtreenode Mi Nnode = root; <br/> binarysearchtreenode leftnode = root. Left; <br/> while (leftnode! = NULL) <br/>{< br/> minnode = leftnode; <br/> leftnode = minnode. left; <br/>}</P> <p> return minnode; <br/>}</P> <p> // <summary> <br/> // obtain the minimum value of the Binary Search Tree. <br/> /// </ summary> <br/> // <returns> minimum value of the binary query tree </returns> <br/> Public long getmin () <br/>{< br/> binarysearchtreenode node = getminnode (); <br/> If (node = NULL) throw new exception ("the minimum value does not exist "); </P> <p> return node. value; <br/>}</P> <p> // <Su Mmary> <br/> // obtain the precursor node of the specified node <br/> /// </Summary> <br/> /// <Param name = "Node"> to obtain the node of the front-end node </param> <br/> // <returns> specify the node's front-end node </returns> <br/> Public binarysearchtreenode getpredecessor (binarysearchtreenode Node) <br/>{< br/> If (node = NULL) return NULL; </P> <p> // The left subtree is not empty, the frontend of the node is the maximum node of the Left subtree <br/> If (node. left! = NULL) <br/>{< br/> return getmaxnode (node. left); <br/>}< br/> else <br/> {<br/> // If the left subtree is empty, search up, until a node is the left-son node of its parent node (in turn, the current node requires the successor node of the node. As you can imagine, the right subtree has the smallest node) <br/> binarysearchtreenode parent = node. parent; <br/> while (parent! = NULL & parent. left = node) <br/>{< br/> node = parent; <br/> parent = parent. parent; <br/>}</P> <p> return parent; <br/>}</P> <p> // <summary> <br/> // obtain the successor node of the specified node <br/> /// </Summary> <br/> /// <Param name = "Node"> obtain the node of the successor node </param> <br/> // <returns> specified node </returns> <br/> Public binarysearchtreenode getsuccessor (binarysearchtreenode node) <br/>{< br/> If (node = NULL) return NULL; </P> <p> // if the right subtree is not empty, the node is followed by the minimum node of the right subtree. <br/> If (node. Right! = NULL) <br/>{< br/> return getminnode (node. right); <br/>}< br/> else <br/> {<br/> // if the right subtree is empty, search up, until a node is the right-son node of its parent node (in turn, the current node is the frontend node of the node, which can be imagined as the largest node in the left subtree) <br/> binarysearchtreenode parent = node. parent; <br/> while (parent! = NULL & parent. right = node) <br/>{< br/> node = parent; <br/> parent = parent. parent; <br/>}</P> <p> return parent; <br/>}</P> <p> // <summary> <br/> // search nodes with specified values <br/> /// </Summary> <br/> /// <Param name = "value"> specified value </param> <br/> /// <returns> value node </returns> <br/> Public binarysearchtreenode search (long value) <br/>{< br/> return search (root, value); <br/>}</P> <p> // <summar Y> <br/> /// search nodes with specified values <br/> /// </Summary> <br/> /// <Param name = "value"> specified value </param> <br/> // <returns> nodes with specified value </returns> <br/> private binarysearchtreenode search (binarysearchtreenode parent, long value) <br/>{< br/> If (parent = NULL) return NULL; <br/> If (parent. value = value) return parent; </P> <p> binarysearchtreenode searchednode = NULL; <br/> If (value <parent. value) <br/> {<br/> // Search left <br/> searchednode = search (parent. left, value); <br/>}< br/> If (value> parent. value) <br/>{< br/> // search right <br/> searchednode = search (parent. right, value); <br/>}</P> <p> return searchednode; <br/>}</P> <p> // <summary> <br/> // Add a node to the binary search tree (and maintain the feature of the Binary Search Tree)) <br/> /// </Summary> <br/> /// <Param name = "Node"> node to be added </param> <br/> Public void addnode (long node) <br/>{< br/> If (root. value> = Node) <br/>{< br/> // Add to left subtree <br/> If (root. left = NULL) <br/>{< br/> // Add <br/> root. left = new binarysearchtreenode () <br/>{< br/> parent = root, <br/> left = NULL, <br/> right = NULL, <br/> value = node <br/>}; <br/> return; <br/>}< br/> addnode (node, Root. left); <br/>}< br/> If (root. value <node) <br/>{< br/> // Add to the right subtree <br/> If (root. right = NULL) <br/>{< br/> // Add <br/> root. ri Ght = new binarysearchtreenode () <br/>{< br/> parent = root, <br/> left = NULL, <br/> right = NULL, <br/> value = node <br/>}; <br/> return; <br/>}< br/> addnode (node, Root. right ); <br/>}</P> <p> // <summary> <br/> // Add a node to the specified subtree <br />/// </Summary> <br/> /// <Param name = "Node"> Add a node </param> <br/> // <Param name = "parent"> subtree </param> <br/> private void addnode (long node, binarysearcht Reenode parent) <br/>{< br/> If (parent. value >=node) <br/>{< br/> // Add to left subtree <br/> If (parent. left = NULL) <br/>{< br/> // Add <br/> parent. left = new binarysearchtreenode () <br/>{< br/> parent = parent, <br/> left = NULL, <br/> right = NULL, <br/> value = node <br/>}; <br/> return; <br/>}< br/> addnode (node, parent. left); <br/>}< br/> If (parent. value <node) <br/>{< br/> // Add to the right subtree <br/> If (parent. right = NULL) <br/>{< br/> // Add <br/> parent. right = new binarysearchtreenode () <br/>{< br/> parent = parent, <br/> left = NULL, <br/> right = NULL, <br/> value = node <br/>}; <br/> return; <br/>}< br/> addnode (node, parent. right ); <br/>}</P> <p> // <summary> <br/> // Delete the specified node (and keep the Binary Search Tree features) <br/> /// </Summary> <br/> /// <Param name = "Node"> node to be deleted </param> <br/> Public void Deletenode (binarysearchtreenode node) <br/>{< br/> If (node = NULL) <br/> throw new argumentnullexception ("Node parameter is blank "); </P> <p> If (node. left = NULL & node. right = NULL) <br/>{< br/> // if the current node is a leaf node, delete <br/> binarysearchtreenode parent = node. parent; <br/> If (parent. value> = node. value) <br/>{< br/> parent. left = NULL; <br/>}< br/> else <br/> {<br/> parent. right = NULL; <br/>}</P> <P> If (node. Left! = NULL & node. Right = NULL) | (node. Left = NULL & node. Right! = NULL) <br/>{< br/> // if there is only one subnode, The subnode is directly appended. <br/> binarysearchtreenode parent = node. parent; <br/> If (parent. value> = node. value) <br/>{< br/> parent. left = node. left = NULL? Node. right: node. left; <br/> parent. left. parent = parent; <br/>}< br/> else <br/>{< br/> parent. right = node. left = NULL? Node. right: node. left; <br/> parent. right. parent = parent; <br/>}</P> <p> If (node. left! = NULL & node. Right! = NULL) <br/>{< br/> // back up the current node <br/> binarysearchtreenode copynode = node. clone () as binarysearchtreenode; </P> <p> // delete a successor node <br/> binarysearchtreenode postnode = getsuccessor (node); <br/> deletenode (postnode ); </P> <p> // Replace the current node with the successor node <br/> postnode. left = node. left; <br/> postnode. right = node. right; <br/> postnode. parent = node. parent; <br/> postnode. tag = node. tag; <br/> postnode. value = node. value; <br/>}</P> <p> # endregion <br/>}</P> <p> // <summary> <br/> /// binary query Tree node <br/> /// </Summary> <br/> public class binarysearchtreenode: icloneable <br/>{< br/> # region constructor </P> <p> Public binarysearchtreenode () {}</P> <p> Public binarysearchtreenode (binarysearchtreenode parent, binarysearchtreenode left, binarysearchtreenode right, long value, object tag) <br/>{< br/> This. parent = parent; <br/> This. left = left; <br/> This. right = right; <br/> This. value = value; <br/> This. tag = tag; <br/>}</P> <p> # endregion </P> <p> # region attribute node </P> <p> // <summary> <br />/// parent node <br/> /// </Summary> <br/> Public binarysearchtreenode parent <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // left son <br/> /// </Summary> <br /> Public binarysearchtreenode left <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // right son <br/> /// </Summary> <br /> Public binarysearchtreenode right <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // node value <br/> /// </Summary> <br /> Public long value <br/>{< br/> get; <br/> set; <br/>}</P> <p> // <summary> <br/> // store other values <br/> /// </Summary> <br/> Public OBJECT tag <br/>{< br/> get; <br/> set; <br/>}</P> <p> # endregion </P> <p> # region icloneable member </P> <p> Public object clone () <br/>{< br/> return New binarysearchtreenode () <br/>{< br/> left = This. left, <br/> right = This. right, <br/> parent = This. parent, <br/> tag = This. tag, <br/> value = This. value <br/>}; <br/>}</P> <p> # endregion <br/>}

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.