二叉树的操作

时间:2022-01-31 17:30:55

【学习背景】


      软考之后,我对算法有了初步了解,通过不断学习,更加体会到了算法对程序的重要。数据结构对解决问题产生了很大的帮助。下面内容是我自己用C#写了一个二叉树的操作类,由于时间匆忙,仍有许多缺陷。


【Demo简介】

                                                                                                                                                                              ------Demo下载


    用链表结构定义了一个符合二叉树结构的类,然后编写一个数的操作类,其中包含了插入节点、查找节点、先序遍历、中序遍历、后续遍历。


【代码示例】


 

<span style="font-family:Arial;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tree
{
class Program
{
public static int LevelCount;
static void Main(string[] args)
{
BinaryTreeHelper treeHelper = new BinaryTreeHelper();
#region 描绘一棵二叉树
ChainTree<string> MyTestTree = new ChainTree<string>();
MyTestTree.data = "我是根节点";

ChainTree<string> LevelOneLeft = new ChainTree<string>();
LevelOneLeft.data = "我是左侧一级节点";
MyTestTree.left = LevelOneLeft;
ChainTree<string> LevelOneRight = new ChainTree<string>();
LevelOneRight.data="我是右侧一级节点";
MyTestTree.right = LevelOneRight;

ChainTree<string> LevelTwoLeft = new ChainTree<string>();
LevelTwoLeft.data = "我是左侧二级节点";
LevelOneLeft.left = LevelTwoLeft;
ChainTree<string> LevelTwoRight = new ChainTree<string>();
LevelTwoRight.data = "我是右侧二级节点";
LevelOneLeft.right = LevelTwoRight;
treeHelper.BinTree_DLR(MyTestTree);
#endregion

#region 插入节点测试
ChainTree<string> InsertOne = new ChainTree<string>();
InsertOne.data = "我是插入进来的!";
treeHelper.BinTreeAddNode<string>(MyTestTree, InsertOne, "我是左侧一级节点", BinaryTreeHelper.Direction.left);
treeHelper.BinTree_DLR(MyTestTree);
#endregion
Console.Read();


}
}


public class ChainTree<T>
{
public T data;
public ChainTree<T> left;
public ChainTree<T> right;
}

public class BinaryTreeHelper
{
#region 方法内枚举,规定树的左右两个方向
/// <summary>
/// 方法内枚举,规定树的左右两个方向
/// </summary>
public enum Direction
{
left = 1,
right = 2
}
#endregion

#region 插入节点的操作
/// <summary>
/// 插入节点操作
/// </summary>
/// <typeparam name="T">具体数据类型</typeparam>
/// <param name="tree">插入节点的二叉树</param>
/// <param name="node">插入的节点</param>
/// <param name="data">需要在那个数据下面插入节点</param>
/// <param name="direction">插入节点时的方向</param>
/// <returns></returns>
public ChainTree<T> BinTreeAddNode<T>(ChainTree<T> tree, ChainTree<T> node, T data, Direction direction)
{
ChainTree<T> tempTree;
if (tree == null)
return null;
if (tree.data.Equals(data)) //如果找到了等于自己想要的数据节点了,就插入
{
switch (direction)
{
case Direction.left:
if (tree.left != null)
{
//如果左侧有了节点,那么该左节点成为插入节点的左节点
tempTree = tree.left;
tree.left = node;
node.left = tempTree;
}
else
{
//如果左侧没有节点,那么直接插入
tree.left = node;
}
break;
case Direction.right:
if (tree.right != null)
{
tempTree = tree.right;
tree.right = node;
node.right = tempTree;
}
else
{
tree.left = node;
}
break;
}
}
BinTreeAddNode(tree.left, node, data, direction);
BinTreeAddNode(tree.right, node, data, direction);
return tree;
}
#endregion

#region 在二叉树中查找指定节点
/// <summary>
/// 二叉树中查找指定数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tree">存在的二叉树</param>
/// <param name="data">需要查找的节点</param>
/// <returns></returns>
public ChainTree<T> BinTreeFind<T>(ChainTree<T> tree, T data)
{
if (tree.data.Equals(data))
return tree;
else
{
BinTreeFind(tree.left, data);
BinTreeFind(tree.right, data);
}
return null;
}
#endregion

#region 对二叉树的先序遍历
/// <summary>
/// 先序遍历
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tree">需要遍历的树</param>
public void BinTree_DLR<T>(ChainTree<T> tree)
{
if (tree == null)
return;
//先操作根节点
Console.Write(tree.data + "\n");
//遍历左子树
BinTree_DLR(tree.left);
//遍历右子树
BinTree_DLR(tree.right);

}
#endregion

#region 对二叉树的中序遍历
/// <summary>
/// 对二叉树的中序遍历
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tree">需要遍历的树</param>
public void BinTree_LDR<T>(ChainTree<T> tree)
{
if (tree == null)
return;
//优先遍历左子树
BinTree_LDR(tree.left);
//对根节点进行操作
Console.Write(tree.data + "\t");
//遍历右子树
BinTree_LDR(tree.right);
}
#endregion

#region 二叉树的后序遍历
/// <summary>
/// 二叉树的后序遍历
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tree">需要遍历的树</param>
public void BinTree_LRD<T>(ChainTree<T> tree)
{
if (tree == null)
return;
//优先遍历左子树
BinTree_LRD(tree.left);
//然后遍历右子树
BinTree_LRD(tree.right);
//最后处理根节点
Console.Write(tree.data + "/t");
}
#endregion
}
}</span>


【效果】


二叉树的操作