Binary search tree implementation in go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Using go to achieve the next two-fork search tree, or spent a lot of time, in the implementation of the use of a one-way list, this is to realize the two-way list in the realization of the advantages


Package Datastructureimport ("Container/list", "FMT") type bstree struct {root *node}type Node struct {left *noderight *node Value int}//newbstree Create tree func newbstree () *bstree {return &bstree{}}func (t *bstree) Insert (value int) {var parent *n Odez: = &node{value:value}x: = t.rootfor x! = Nil {parent = Xif Z.value < x.value {x = x.left} else {x = x.right}} if parent = = Nil {//the tree is empty t.root = Z} else if z.value < parent.value {parent.left = Z} else {parent.right = Z}}func (T * Bstree) Search (x int) *node {node: = t.rootfor Node! = Nil {if Node.value = = x {return Node} else if x < Node.value {No de = node.left} else {node = Node.right}}return nil}func (t *bstree) Delete (x int) bool {var parent *nodenode: = T.ROOTISF IND: = falsefor Node! = Nil {if Node.value = = x {isfind = truebreak} else if x < node.value {parent = Nodenode = Node.l EFT} else {parent = Nodenode = Node.right}}if Isfind = = False {return false}//case one: node is leaf node if node.left = Nil && n Ode.right = = Nil {if parent = nil {t.root = nil} else {if Parent.left = = Node {parent.left = nil} else {parent.right = Nil}}return true}//Situation II: Left child The side is empty or the right child is empty if Node.left = = Nil | | Node.right = = Nil {if parent = = Nil {if Node.left = nil {t.root = node.right} else {t.root = Node.left}} else {if parent. left = = Node {if Node.left = nil {parent.left = node.right} else {parent.left = Node.left}} else {if Node.left = nil {PA    Rent.right = node.right} else {parent.right = Node.left}}} return True}//condition three: Two children are not empty re: = Node.left Re_parent: = node for re.right! = Nil {//Find the parent node of the precursor node and the precursor node re_parent = re re = re.right} n    Ode.value = re.value if node = = re_parent {node.left = re.left} else {re_parent.right = Re.left } return true}//printtree1 recursive struct func (t *node) PrintTree1 () {if t.left! = nil {t.left.printtree1 ()}fmt. Print (T.value, "") if t.right! = nil {t.right.printtree1 ()}}//printtree2 non-recursive struct func (t *node) PrintTree2 () {stack: = list. New () stack. Pushback (t) for {node: = stack. Back () if node = = Nil {return}stack. Remove (node) v, _: = node. Value. (*node) fmt. Println (v.value) if v.left! = nil {stack. Pushback (v.left)}if v.right! = nil {stack. Pushback (V.right)}}}


Related Article

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.