C# 二叉树的综合操作(一):查找、插入、最大(小)值、先序遍历、后序遍历及中序遍历
来源:互联网 发布:克里米亚战争 知乎 编辑:程序博客网 时间:2024/05/17 09:19
二叉树是学习数据结构与算法的重要内容,现做综合操作二叉树的汇总。由于关于这方面的文章、书籍已是随处可见,故此间的细节不再赘述,附上一段代码,并提供实例,供参考。代码已测试没有问题。
public class Node // 定义二叉树节点类 Node { public int Data; public Node Left; public Node Right; public void Display() { Console.WriteLine(Data); } public Node(int x) { Data = x; } } public class BinaryTree // 定义二叉树 { public Node Current; // Node Parent; // 定义一个 Parent,用于存储当前节点(Current)的父节点,为添加删除节点方法做准备 public Node Root; // 定义根结点 public BinaryTree() // 构造函数,初始化二叉树 { Root = null; } public void InOrder(Node theRoot) // 通过递归,中序遍历 { if (theRoot != null) { InOrder(theRoot.Left); Console.Write(theRoot.Data + "\t"); InOrder(theRoot.Right); } } public void PreOrder(Node theRoot) // 通过递归,先序遍历 { if (theRoot != null) { Console.Write(theRoot.Data + "\t"); PreOrder(theRoot.Left); PreOrder(theRoot.Right); } } public void PostOrder(Node theRoot) // 通过递归,后序遍历 { if (theRoot != null) { PostOrder(theRoot.Left); PostOrder(theRoot.Right); Console.Write(theRoot.Data+"\t"); } } public void Insert(int x) // 插入节点 { Node newNode = new Node(x); Current = Root; if (Root == null) { Root = newNode; } else { while (true) { Parent = Current; if (x < Current.Data) { Current = Current.Left; if (Current == null) { Parent.Left = newNode; break; } } else { Current = Current.Right; if (Current == null) { Parent.Right = newNode; break; } } } } } public int Min() // 查找最小值 { Current = Root; while (Current.Left != null) Current = Current.Left; return Current.Data; } public int Max() // 查找最大值 { Current = Root; while (Current.Right != null) Current = Current.Right; return Current.Data; } public Node Find(int key) // 查找某一个确定的节点的值 { Current = Root; while (Current != null) { if (key == Current.Data) break; // 找到了就结束 while 循环 if (key < Current.Data) { Parent = Current; Current = Current.Left; } else { Parent = Current; Current = Current.Right; } } if (Current == null) return null; else return Current; } } class Program { static void Main(string[] args) { BinaryTree bst = new BinaryTree(); bst.Insert(25); bst.Insert(50); bst.Insert(15); bst.Insert(33); bst.Insert(4); bst.Insert(100); bst.Insert(20); bst.Insert(38); bst.Insert(1); bst.Insert(10); bst.Insert(18); bst.Insert(30); bst.Insert(32); Console.WriteLine("The min is: {0}", bst.Min()); Console.WriteLine("The max is: {0}", bst.Max()); Console.Write("Find \"100\": "); Console.Write(bst.Find(100).Data+"\n"); Console.WriteLine("\n" + "PreOrder: "); bst.PreOrder(bst.Root); Console.WriteLine("\n" +"PostOrder:"); bst.PostOrder(bst.Root); Console.WriteLine("\n"+"InOrder: "); bst.InOrder(bst.Root); Console.Read(); } }运行结果:
The min is: 1
The max is: 100
Find "100": 100
PreOrder:
25 15 4 1 10 20 18 50 33 30 32 38 100
PostOrder:
1 10 4 18 20 15 32 30 38 33 100 50 25
InOrder:
1 4 10 15 18 20 25 30 32 33 38 50 100
2 0
- C# 二叉树的综合操作(一):查找、插入、最大(小)值、先序遍历、后序遍历及中序遍历
- 二叉树的遍历(1)--先序遍历,中序遍历,后序遍历
- 二叉树的遍历代码(先序遍历,中序遍历,后序遍历)
- 二叉树T 的先序遍历、中序遍历、后序遍历(递归实现)
- 二叉树的操作(二叉树的创建、先序遍历--->先根、中序遍历---->先左、后续遍历--->后根)
- 二叉树的建立及遍历(先序遍历、中序遍历、后续遍历、层次遍历)
- 二叉树的遍历(2)--先序遍历,中序遍历,后序遍历(非递归)
- 二叉树的先序遍历,后序遍历,中序遍历,层次遍历
- 二叉查找树的遍历【先序(前序),中序,后序】
- 二叉树重建 - (先序遍历、中序遍历、后序遍历)
- 创建一个二叉树(先序遍历、中序遍历、后序遍历)
- 先序遍历、中序遍历、后序遍历(二叉树)
- 先序遍历中序遍历后序遍历确定一棵二叉树(递归、非递归)
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- 二叉树的先、中、后序遍历及层次遍历的迭代版算法
- 二叉树的先序-中序-后序遍历(一)-递归
- 二叉树递归的创建及三种遍历(先序、中序、后序)
- java native 接口开发流程
- .net句柄无效
- UITextField进入编辑状态文字向下偏移
- 百度代码分享到QQ、微信、人人网等
- EnvironmentError: mysql_config not found
- C# 二叉树的综合操作(一):查找、插入、最大(小)值、先序遍历、后序遍历及中序遍历
- spring 整合 rabbitmq
- Python:文件的读取、创建、追加、删除、清空
- 5、Power Query-抓取网页数据做漂亮的图表
- ehcache配置说明
- 第16章 控制脚本
- 在.NET2.0中使用LINQ
- Junit4学习笔记
- Java中hashCode的作用