Go -- 实现二叉搜索树
树: https://suanfa.herokuapp.com/3%E6%A0%91/binarytree/
数据结构
首先我们定义需要的数据结构。注意,TreeNode的左右节点都是*TreeNode type的,而树只有一个Root数据域,为*TreeNode type
type TreeNode struct {
  Value int
  Left *TreeNode
  Right *TreeNode
}
type BinarySearchTree struct {
  Root *TreeNode
}
Insert
向二叉搜索树中插入元素,首先要找到插入的位置,然后再插入。这里注意我们的实现方法为给TreeNode和BinarySearchTree这两个type添加方法。需要注意给type添加方法的方式,同时还要注意,如果你要改变方法调用者,则需要使用指针
func (tree BinarySearchTree) Insert (v int) {
  tree.Root.Insert(v)
}
func (node *TreeNode) Insert (v int){
  if v < node.Value {
    if node.Left != nil{
      node.Left.Insert(v)
    }else{
      node.Left = &TreeNode{v, nil, nil}
    }
  }else {
    if node.Right != nil{
      node.Right.Insert(v)
    }else{
      node.Right = &TreeNode{v, nil, nil}
    }
  }
}
遍历
树的遍历有前序,后序,中序等等。这里以中序为例。注意slice与slice指针的不同
func (tree BinarySearchTree) InOrder() []int{
  var res []int
  tree.Root.InOrder(&res)
  return res
}
func (node *TreeNode) InOrder(result *[]int) {
  if node.Left != nil{
    node.Left.InOrder(result)
  }
  *result = append(*result, node.Value)
  if node.Right != nil{
    node.Right.InOrder(result)
  }
}
最大最小值
func (tree BinarySearchTree) FindMin() int {
  node := tree.Root
  for {
    if node.Left != nil {
      node = node.Left
    }else{
      return node.Value
    }
  }
}
func (tree BinarySearchTree) FindMax() int {
  node := tree.Root
  for {
    if node.Right != nil {
      node = node.Right
    }else{
      return node.Value
    }
  }
}
查找
func (tree BinarySearchTree) Contains(v int) bool {
  node := tree.Root
  for {
    if node == nil{
      return false
    }else if (node.Value == v) {
      return true
    }else if (node.Value > v){
      node = node.Left
    }else{
      node = node.Right
    }
  }
}
Go -- 实现二叉搜索树的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
		二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ... 
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
		Serialization is the process of converting a data structure or object into a sequence of bits so tha ... 
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
		Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ... 
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
		Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ... 
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
		Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ... 
- [LeetCode]  Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
		Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ... 
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
		Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ... 
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
		Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ... 
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
		Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ... 
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
		Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ... 
随机推荐
- shell进阶
			shell 中的高级用法 1.if 单重判断 if cmd; then cmd cmd cmd fi 多重判断 单分支 if cmd;then cmd elif cmd fi 双分支 if cmd; ... 
- Linux基础学习-使用DHCP动态管理主机地址
			动态主机配置协议 部署dhcpd服务程序 参数 作用 ddns-update-style none; 设置DNS服务不自动进行动态更新 ignore client-updates; 忽略客户端更新DN ... 
- centos7.2安装redis与配置(史上最全)
			学习了php已经快三年了,一直是在盲目的忙,也没整理下笔记,今天整理一下 分享下安装redis的方法 #首先去redis官网去下载 http://www.redis.cn/download.htm ... 
- python--网络通信协议
			一 . osi七层协议 互联网协议按照功能不同分为osi七层或tcp/ip五层或tcp/ip四层 二 . tcp三次握手和四次挥手 我们知道网络层,可以实现两个主机之间的通信.但是这并不具体,因为,真 ... 
- (转)iOS 常用宏定义
			#ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小------------------- ... 
- Codeforces Round #879 (Div. 2) C. Short Program
			题目链接:http://codeforces.com/contest/879/problem/C C. Short Program time limit per test2 seconds memor ... 
- 基于顺序链表的栈的顺序存储的C风格实现
			头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ... 
- ZOJ 3469 区间DP Food Delivery
			题解 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ... 
- eclipse中tab键设置
			1.点击 window->preference-,依次选择 General->Editors->Text Editors,选中右侧的 insert space for tabs;如下 ... 
- 五、docker配置镜像加速器之阿里云
			1 配置docker加速器 实在忍受不了pull的速度--------- 访问网址: https://dev.aliyun.com/search.html 点击管理中心: 根据操作稳定配置: 
