二叉搜索树 (BST) 的创建以及遍历

时间:2021-11-02 07:21:52

二叉搜索树(Binary Search Tree) : 属于二叉树,其中每个节点都含有一个可以比较的键(如需要可以在键上关联值), 且每个节点的键都大于其左子树中的任意节点而小于右子树的任意节点的键。

1、BST 的总体结构:

二叉搜索树 (BST) 的创建以及遍历

主要的几种变量以及方法如上图所示,主要有插入、排序、删除以及查找等方法。键采用泛型,继承 IComparable, 便于比较。

其中节点的类如下图:

二叉搜索树 (BST) 的创建以及遍历

BST 类代码如下:

     public class BST<Tkey, Tval> where Tkey : IComparable
{
private Node root; public class Node
{
private Tkey key;
private Tval val;
private int n; public Node(Tkey key, Tval val, int n)
{
this.key = key;
this.val = val;
}
public Tkey Key { get => key; }
public Tval Val { get => val; set => val = value; }
public Node left { set; get; }
public Node right { set; get; }
public int N { set => n = value; get => n; }
} public int Size()
public void Insert(Tkey, Tval)
public void Delete(Node x)
public void InorderTraversal()
}

2、插入新节点

根据键大于左节点, 小于右节点的定义,可用如下代码实现新节点的插入:

 public void Insert(Tkey key, Tval val)
{
// 创建私有方法,便于传入参数 root
root = Insert(root, key, val);
} private Node Insert(Node x, Tkey key, Tval val)
{
// 若节点为空(无根节点),则创建新的节点
if (x == null)
{
return new Node(key, val, );
} // 比较键的大小,小于返回 -1 , 大于返回 1, 等于返回 0
int t = key.CompareTo(x.Key); if (t > ) { x.right = Insert(x.right, key, val); }
else if (t < ) { x.left = Insert(x.left, key, val); }
else { x.Val = val; } x.N = Size(x.left) + Size(x.right) + ; return x;
}

3、计算以该节点为根的节点总节点数

采用递归的方法,从根节点到循环到叶子节点

     public int Size(Node x)
{
if (x == null) { return ; }
return Size(x.left) + Size(x.right) + ;
}

4、遍历

遍历分为广度遍历与深度遍历,如下图所示:

二叉搜索树 (BST) 的创建以及遍历

深度优先遍历的几种方式原理相似, 只是输出的节点键的位置不同而已。

中序遍历递归:

     public void InorderTraversal_recursive(Node x)
{
if (x == null) { return; } InorderTraversal(x.left);
Console.Write(x.Key + " ");
InorderTraversal(x.right);
}

中序遍历非递归:

     public void  InorderTraversal_stack()   // 利用堆栈先进后出的特性, 先将全部左节点压入,然后输出左节点,每输出一个左节点后切换到右节点, 继续输出
{
Stack<Node> nodeStack = new Stack<Node>();
Node currentNode = root; while (nodeStack != null || currentNode != null) // 此处判断 curretNode 非空,是因为首次循环 nodeStack 为空, 避免了在循环外添加根节点。
{
while (currentNode != null) // 将全部左节点压入堆栈
{
nodeStack.Push(currentNode);
currentNode = currentNode.left;
}
if (nodeStack.count != 0)
{
currentNode = nodeStack.Pop();
Console.Write(currentNode.key + " ");
currentNode = currentNode.right; // 切换到右节点
}
}
}

层序遍历:

     // 利用队列先进先出的特性, 层层输出
public void LevelTraversal()
{
Queue<Node> nodeQueue = new Queue<Node>();
if (root != null) { nodeQueue.Enqueue(root); } while (nodeQueue.Count() != )
{
Node currentNode = nodeQueue.Dequeue();
Console.Write(currentNode.Key + " ");
// 将去除节点的子节点添加到队列的尾部
if (currentNode.left != null) { nodeQueue.Enqueue(currentNode.left); }
if (currentNode.right != null) { nodeQueue.Enqueue(currentNode.right); }
}
}

5. 证明二叉树为搜索树

根据定义,搜索树是二叉树的基础上添加的一个条件: 节点左子树全部节点小于节点, 节点右子树大于节点。中序遍历,全部节点按序遍历,由此我们只需要证明后一个节点大于前一个节点。

     public bool isBST()
{
Stack<Node> nodeStack = new Stack<Node>();
Node currentNode = root;
Node preNode = null; if (nodeStack.Count() != || currentNode != null)
{
while (currentNode != null)
{
nodeStack.Push(currentNode);
currentNode = currentNode.left;
} if (nodeStack.Count() != )
{
currentNode = nodeStack.Pop();
// 此处需要判断 preNode 等于空的情况(当进行首个节点判断时,preNOde 为空, 跳过判断)
if (preNode != null && currentNode.Key.CompareTo(preNode.Key) <= ) { return false; } preNode = currentNode;
currentNode = currentNode.right;
}
}
return true;
}