人们时常选择二叉树超过诸如数组和链表真阳的较为基础的数据结构,因为人们可以快速的查找二叉树(相对于链表),还可以迅速的二叉树中插入和拆除数据(相对于数组)。
人们把每个节点最拥有不超过两个子节点的树定义为二叉树。
二叉查找树是一种吧较小数据存储在左节点二较大数据存储在右节点的二叉树。
Public class Node
{
Public int Data;
Public Node left;
Public Node right;
Public void DisplayNode()
{
Console.writeline(Data);
}
}
Public class BinarySearchTree()
{
Public Node root;
Pbulic BinarySearchTree()
{
root=null;
}
Public void Insert(int i)
{
Node newNode=new Node();
newNode.Data=i;
If(root==null)
{
Root=newNode;
}
Else
{
Node CurrentNode=root;
Node ParentNode=null;
While(true)
{
ParentNode=CurrentNode;
If(i<CurrentNode.Data)
{
CurrentNode=CurrentNode.Left;
If(CurrentNode==null)
{
CurrentNode.Left=newNode;
Break;
}
}
Else
{
CurrentNode=CurrentNode.Right;
If(CurrentNode==null)
{
CurrentNode.Right=newNode;
Break;
}
}
}
}
Public void Inorder(node theRoot) // 中序遍历
{
If(!(theRoot==null))
{
Inorder(theRoot.left);
theRoot.DisplayNode();
Inorder(theRoot.right);
}
}
Public void PreOrder(node theRoot) //先序遍历
{
If(!(theRoot==null))
{
theRoot.DisplayNode();
PreOrder(theRoot.Left);
PreOrder(theRoot.Right);
}
}
Public void PostOrder(node theRoot) //后续遍历
{
If(theRoot==null)
{
PostOrder(theRoot.Left);
PostOrder(theRoot.Right);
theRoot.DisplayNode();
}
}
Public void LevelOrdr(node theRoot) // 广度优先遍历 借助队列
{
Queue que=new Queue();
que.Enqueue(theRoot);
While(que.Count )
{
Node node=(Node).que.Dequeue();
Console.Wite(node);
If(node.Left!=null)
{
que.Enqueue(node.Left);
}
If(node.Right!=null)
{
que.Enqueue(node.Right);
}
}
}
Public int findMain()
{
Node CurrentNode=root;
While(CurrentNode.left!=null)
{
CurrentNode=CurrentNode.Left;
}
Return CurrentNode.Data;
}
Public int findMax()
{
Node CurrentNode=root;
While(CurrentNode.right!=null)
{
CurrentNode=CurrentNode.right
}
Return CurrentNode.Data;
}
Public Node Find(int key)
{
Node current=root;
While(curent.data!=key)
{
If(key<current.data)
Curent=current.Left;
Else
Current=current.Right;
If(current==null)
Return null;
}
Return current;
}
}
public bool Delete(int key)
{
Node current = root;
Node parent = root;
bool isLeftChild = true;
while (current.Data != key)
{
parent = current;
if (key < current.Data)
{
isLeftChild = true;
current = current.Right;
}
else
{
isLeftChild = false;
current = current.Right;
}
if (current == null)
return false;
}
if ((current.Left == null) & (current.Right == null))
if (current == root)
root = null;
else if (isLeftChild)
parent.Left = null;
else if (current.Right == null)
if (current == root)
root = current.Left;
else if (isLeftChild)
parent.Left = current.Left;
else
parent.Right = current.Right;
else if (current.Left == null)
if (current == root)
root = current.Right;
else if (isLeftChild)
parent.Left = parent.Right;
else
parent.Right = current.Right;
else
{
Node successor = GetSuccessor(current);
if (current == root)
root = successor;
else if (isLeftChild)
parent.Left = successor;
else
parent.Right = successor;
successor.Left = current.Left;
}
return true;
}
}
二叉树遍历(Traversal)是按照某种顺序对树中的每个节点访问,且只访问一次的过程。二叉树遍历是本质上是将非线性结构线性化。
二叉树深度优先遍历
D 二叉树的某一节点
L ,R 分别为节点D的左右树。
先序 DLR DRL
中序 LDR RDL
后序 LRD LRD
深度优先搜索子孙节点再搜索节点的兄弟节点。广度遍历先搜索所有兄弟节点,再搜索子孙节点。
二叉查找树三件最容易的事情 : 查找特定值,找到最下值 ,找最大值。