Golang以OO的方式实现二叉查找树

来源:互联网 发布:火车票制作生成器软件 编辑:程序博客网 时间:2024/05/16 19:54

二叉查找树是一种满足如下性质的二叉树:

(1)  某个节点的左子树中的所有节点的值都比这个节点的值小

(2)  某个节点的右子树中的所有节点的值都比这个节点的值大


下面有Go实现的非常详尽的代码,采用了Go风格的OO进行了封装。代码中主函数的例子的参照图如下:



这是我的二叉查找树的使用手册:

[cpp] view plain copy
  1. type BiSearchTree struct   
  2.     func (bst *BiSearchTree) Add(data float64)                        //插入节点  
  3.     func (bst *BiSearchTree) Delete(data float64)                     //删除节点  
  4.     func (bst BiSearchTree) GetRoot() *TreeNode                       //获取根节点  
  5.     func (bst BiSearchTree) IsEmpty() bool                            //检查树是否为空  
  6.     func (bst BiSearchTree) InOrderTravel()                           //中序遍历(也就是从小到大输出)  
  7.     func (bst BiSearchTree) Search(data float64) *TreeNode            //查找节点  
  8.     func (bst BiSearchTree) GetDeepth() int                           //获取树的深度  
  9.     func (bst BiSearchTree) GetMin() float64                          //获取值最小的节点  
  10.     func (bst BiSearchTree) GetMax() float64                          //获取值最大的节点  
  11.     func (bst BiSearchTree) GetPredecessor(data float64) *TreeNode    //获取直接前驱  
  12.     func (bst BiSearchTree) GetSuccessor(data float64) *TreeNode      //获取直接后继  
  13.     func (bst *BiSearchTree) Clear()                                  //清空树  

实现代码:

[java] view plain copy
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5. )  
  6.   
  7. type TreeNode struct {  
  8.     data   float64  
  9.     lchild *TreeNode  
  10.     rchild *TreeNode  
  11.     parent *TreeNode  
  12. }  
  13.   
  14. type BiSearchTree struct {  
  15.     root   *TreeNode  
  16.     cur    *TreeNode  
  17.     create *TreeNode  
  18. }  
  19.   
  20. func (bst *BiSearchTree) Add(data float64) {  
  21.     bst.create = new(TreeNode)  
  22.     bst.create.data = data  
  23.   
  24.     if !bst.IsEmpty() {  
  25.         bst.cur = bst.root  
  26.         for {  
  27.             if data < bst.cur.data {  
  28.                 //如果要插入的值比当前节点的值小,则当前节点指向当前节点的左孩子,如果  
  29.                 //左孩子为空,就在这个左孩子上插入新值  
  30.                 if bst.cur.lchild == nil {  
  31.                     bst.cur.lchild = bst.create  
  32.                     bst.create.parent = bst.cur  
  33.                     break  
  34.                 } else {  
  35.                     bst.cur = bst.cur.lchild  
  36.                 }  
  37.   
  38.             } else if data > bst.cur.data {  
  39.                 //如果要插入的值比当前节点的值大,则当前节点指向当前节点的右孩子,如果  
  40.                 //右孩子为空,就在这个右孩子上插入新值  
  41.                 if bst.cur.rchild == nil {  
  42.                     bst.cur.rchild = bst.create  
  43.                     bst.create.parent = bst.cur  
  44.                     break  
  45.                 } else {  
  46.                     bst.cur = bst.cur.rchild  
  47.                 }  
  48.   
  49.             } else {  
  50.                 //如果要插入的值在树中已经存在,则退出  
  51.                 return  
  52.             }  
  53.         }  
  54.   
  55.     } else {  
  56.         bst.root = bst.create  
  57.         bst.root.parent = nil  
  58.     }  
  59. }  
  60.   
  61. func (bst *BiSearchTree) Delete(data float64) {  
  62.     var (  
  63.         deleteNode func(node *TreeNode)  
  64.         node       *TreeNode = bst.Search(data)  
  65.     )  
  66.   
  67.     deleteNode = func(node *TreeNode) {  
  68.         if node == nil {  
  69.             return  
  70.         }  
  71.   
  72.         if node.lchild == nil && node.rchild == nil {  
  73.             //如果要删除的节点没有孩子,直接删掉它就可以(毫无挂念~.~!)  
  74.             if node == bst.root {  
  75.                 bst.root = nil  
  76.             } else {  
  77.                 if node.parent.lchild == node {  
  78.                     node.parent.lchild = nil  
  79.                 } else {  
  80.                     node.parent.rchild = nil  
  81.                 }  
  82.             }  
  83.   
  84.         } else if node.lchild != nil && node.rchild == nil {  
  85.             //如果要删除的节点只有左孩子或右孩子,让这个节点的父节点指向它的指针指向它的  
  86.             //孩子即可  
  87.             if node == bst.root {  
  88.                 node.lchild.parent = nil  
  89.                 bst.root = node.lchild  
  90.             } else {  
  91.                 node.lchild.parent = node.parent  
  92.                 if node.parent.lchild == node {  
  93.                     node.parent.lchild = node.lchild  
  94.                 } else {  
  95.                     node.parent.rchild = node.lchild  
  96.                 }  
  97.             }  
  98.   
  99.         } else if node.lchild == nil && node.rchild != nil {  
  100.             if node == bst.root {  
  101.                 node.rchild.parent = nil  
  102.                 bst.root = node.rchild  
  103.             } else {  
  104.                 node.rchild.parent = node.parent  
  105.                 if node.parent.lchild == node {  
  106.                     node.parent.lchild = node.rchild  
  107.                 } else {  
  108.                     node.parent.rchild = node.rchild  
  109.                 }  
  110.             }  
  111.   
  112.         } else {  
  113.             //如果要删除的节点既有左孩子又有右孩子,就把这个节点的直接后继的值赋给这个节  
  114.             //点,然后删除直接后继节点即可  
  115.             successor := bst.GetSuccessor(node.data)  
  116.             node.data = successor.data  
  117.             deleteNode(successor)  
  118.         }  
  119.     }  
  120.   
  121.     deleteNode(node)  
  122. }  
  123.   
  124. func (bst BiSearchTree) GetRoot() *TreeNode {  
  125.     if bst.root != nil {  
  126.         return bst.root  
  127.     }  
  128.     return nil  
  129. }  
  130.   
  131. func (bst BiSearchTree) IsEmpty() bool {  
  132.     if bst.root == nil {  
  133.         return true  
  134.     }  
  135.     return false  
  136. }  
  137.   
  138. func (bst BiSearchTree) InOrderTravel() {  
  139.     var inOrderTravel func(node *TreeNode)  
  140.   
  141.     inOrderTravel = func(node *TreeNode) {  
  142.         if node != nil {  
  143.             inOrderTravel(node.lchild)  
  144.             fmt.Printf("%g ", node.data)  
  145.             inOrderTravel(node.rchild)  
  146.         }  
  147.     }  
  148.   
  149.     inOrderTravel(bst.root)  
  150. }  
  151.   
  152. func (bst BiSearchTree) Search(data float64) *TreeNode {  
  153.     //和Add操作类似,只要按照比当前节点小就往左孩子上拐,比当前节点大就往右孩子上拐的思路  
  154.     //一路找下去,知道找到要查找的值返回即可  
  155.     bst.cur = bst.root  
  156.     for {  
  157.         if bst.cur == nil {  
  158.             return nil  
  159.         }  
  160.   
  161.         if data < bst.cur.data {  
  162.             bst.cur = bst.cur.lchild  
  163.         } else if data > bst.cur.data {  
  164.             bst.cur = bst.cur.rchild  
  165.         } else {  
  166.             return bst.cur  
  167.         }  
  168.     }  
  169. }  
  170.   
  171. func (bst BiSearchTree) GetDeepth() int {  
  172.     var getDeepth func(node *TreeNode) int  
  173.   
  174.     getDeepth = func(node *TreeNode) int {  
  175.         if node == nil {  
  176.             return 0  
  177.         }  
  178.         if node.lchild == nil && node.rchild == nil {  
  179.             return 1  
  180.         }  
  181.         var (  
  182.             ldeepth int = getDeepth(node.lchild)  
  183.             rdeepth int = getDeepth(node.rchild)  
  184.         )  
  185.         if ldeepth > rdeepth {  
  186.             return ldeepth + 1  
  187.         } else {  
  188.             return rdeepth + 1  
  189.         }  
  190.     }  
  191.   
  192.     return getDeepth(bst.root)  
  193. }  
  194.   
  195. func (bst BiSearchTree) GetMin() float64 {  
  196.     //根据二叉查找树的性质,树中最左边的节点就是值最小的节点  
  197.     if bst.root == nil {  
  198.         return -1  
  199.     }  
  200.     bst.cur = bst.root  
  201.     for {  
  202.         if bst.cur.lchild != nil {  
  203.             bst.cur = bst.cur.lchild  
  204.         } else {  
  205.             return bst.cur.data  
  206.         }  
  207.     }  
  208. }  
  209.   
  210. func (bst BiSearchTree) GetMax() float64 {  
  211.     //根据二叉查找树的性质,树中最右边的节点就是值最大的节点  
  212.     if bst.root == nil {  
  213.         return -1  
  214.     }  
  215.     bst.cur = bst.root  
  216.     for {  
  217.         if bst.cur.rchild != nil {  
  218.             bst.cur = bst.cur.rchild  
  219.         } else {  
  220.             return bst.cur.data  
  221.         }  
  222.     }  
  223. }  
  224.   
  225. func (bst BiSearchTree) GetPredecessor(data float64) *TreeNode {  
  226.     getMax := func(node *TreeNode) *TreeNode {  
  227.         if node == nil {  
  228.             return nil  
  229.         }  
  230.         for {  
  231.             if node.rchild != nil {  
  232.                 node = node.rchild  
  233.             } else {  
  234.                 return node  
  235.             }  
  236.         }  
  237.     }  
  238.   
  239.     node := bst.Search(data)  
  240.     if node != nil {  
  241.         if node.lchild != nil {  
  242.             //如果这个节点有左孩子,那么它的直接前驱就是它左子树的最右边的节点,因为比这  
  243.             //个节点值小的节点都在左子树,而这些节点中值最大的就是这个最右边的节点  
  244.             return getMax(node.lchild)  
  245.         } else {  
  246.             //如果这个节点没有左孩子,那么就沿着它的父节点找,知道某个父节点的父节点的右  
  247.             //孩子就是这个父节点,那么这个父节点的父节点就是直接前驱  
  248.             for {  
  249.                 if node == nil || node.parent == nil {  
  250.                     break  
  251.                 }  
  252.                 if node == node.parent.rchild {  
  253.                     return node.parent  
  254.                 }  
  255.                 node = node.parent  
  256.             }  
  257.         }  
  258.     }  
  259.   
  260.     return nil  
  261. }  
  262.   
  263. func (bst BiSearchTree) GetSuccessor(data float64) *TreeNode {  
  264.     getMin := func(node *TreeNode) *TreeNode {  
  265.         if node == nil {  
  266.             return nil  
  267.         }  
  268.         for {  
  269.             if node.lchild != nil {  
  270.                 node = node.lchild  
  271.             } else {  
  272.                 return node  
  273.             }  
  274.         }  
  275.     }  
  276.   
  277.     //参照寻找直接前驱的函数对比着看  
  278.     node := bst.Search(data)  
  279.     if node != nil {  
  280.         if node.rchild != nil {  
  281.             return getMin(node.rchild)  
  282.         } else {  
  283.             for {  
  284.                 if node == nil || node.parent == nil {  
  285.                     break  
  286.                 }  
  287.                 if node == node.parent.lchild {  
  288.                     return node.parent  
  289.                 }  
  290.                 node = node.parent  
  291.             }  
  292.         }  
  293.     }  
  294.   
  295.     return nil  
  296. }  
  297.   
  298. func (bst *BiSearchTree) Clear() {  
  299.     bst.root = nil  
  300.     bst.cur = nil  
  301.     bst.create = nil  
  302. }  
  303.   
  304. func main() {  
  305.     var bst BiSearchTree  
  306.     bst.Add(15)  
  307.     bst.Add(6)  
  308.     bst.Add(18)  
  309.     bst.Add(3)  
  310.     bst.Add(7)  
  311.     bst.Add(17)  
  312.     bst.Add(20)  
  313.     bst.Add(2)  
  314.     bst.Add(4)  
  315.     bst.Add(13)  
  316.     bst.Add(9)  
  317.     bst.Add(14)  
  318.   
  319.     fmt.Printf("The nodes of the BiSearchTree is: ")  
  320.     bst.InOrderTravel()  
  321.     fmt.Printf("\n")  
  322.   
  323.     fmt.Printf("The deepth of the tree is: %d\n", bst.GetDeepth())  
  324.     fmt.Printf("The min is: %g\n", bst.GetMin())  
  325.     fmt.Printf("The max is: %g\n", bst.GetMax())  
  326.   
  327.     if bst.Search(17) != nil {  
  328.         fmt.Printf("The 17 exists.\n")  
  329.     }  
  330.   
  331.     fmt.Printf("root node's predecessor is: %g\n", bst.GetPredecessor(bst.GetRoot().data).data)  
  332.     fmt.Printf("root node's successor is: %g\n", bst.GetSuccessor(bst.GetRoot().data).data)  
  333.   
  334.     bst.Delete(13)  
  335.     fmt.Printf("Nodes after delete the 13: ")  
  336.     bst.InOrderTravel()  
  337.     fmt.Printf("\n")  
  338. }  


输出结果:





如果转载请注明出处:http://blog.csdn.net/gophers/article/details/23552925


原创粉丝点击