namespace DSList
{
public class BinaryTree<T> where T:IComparable<T>
{
//Field
private TreeNode<T> head;
//Property
public TreeNode<T> Head
{
get
{
return head;
}
set
{
head = value;
}
}
//Constructors
public BinaryTree(T val, TreeNode<T> lc, TreeNode<T> rc)
{
TreeNode<T> q = new TreeNode<T>(val, lc, rc);
head = q;
}
public BinaryTree(T val)
{
TreeNode<T> q = new TreeNode<T>(val);
head = q;
}
public BinaryTree()
{
head = null;
}
//Base methods
public bool IsEmpty()
{
return head == null;
}
public TreeNode<T> Root()
{
return head;
}
public TreeNode<T> GetLChild(TreeNode<T> p)
{
return p.LChild;
}
public TreeNode<T> GetRChild(TreeNode<T> p)
{
return p.RChild;
}
public void InsertL(T val,TreeNode<T> p)
{
TreeNode<T> q = new TreeNode<T>(val);
q.LChild = p.LChild;
p.LChild = q;
}
public void InsertR(T val,TreeNode<T> p)
{
TreeNode<T> q = new TreeNode<T>(val);
q.RChild = p.RChild;
p.RChild = q;
}
public TreeNode<T> DeleteL(TreeNode<T> p)
{
if (p == null || p.LChild == null)
{
return null;
}
TreeNode<T> tmp = new TreeNode<T>();
tmp = p.LChild;
p.LChild = null;
return tmp;
}
public TreeNode<T> DeleteR(TreeNode<T> p)
{
if (p == null || p.RChild == null)
{
return null;
}
TreeNode<T> tmp = new TreeNode<T>();
tmp = p.RChild;
p.RChild = null;
return tmp;
}
public bool IsLeaf(TreeNode<T> p)
{
return p != null && p.LChild == null && p.RChild == null;
}
//DLR 前序遍历:Preorder traverse
public void PreOrder(TreeNode<T> root)
{
if (root == null)
{
return;
}
Console.Write(root.Data);
PreOrder(root.LChild);
PreOrder(root.RChild);
}
//LDR 中序遍历
public void InOrder(TreeNode<T> root)
{
if (root == null)
{
return;
}
InOrder(root.LChild);
Console.Write(root.Data);
InOrder(root.RChild);
}
//LRD 后序遍历
public void PostOrder(TreeNode<T> root)
{
if (root == null)
{
return;
}
PostOrder(root.LChild);
PostOrder(root.RChild);
Console.Write(root.Data);
}
//广度优先遍历(层序遍历)
public void LevelOrder(TreeNode<T> root)
{
if (root == null)
{
return;
}
CSeqQueue<TreeNode<T>> sq = new CSeqQueue<TreeNode<T>>();
sq.In(root);
while (!sq.IsEmpty())
{
TreeNode<T> tmp = sq.Out();
Console.Write(tmp.Data);
if (tmp.LChild != null)
{
sq.In(tmp.LChild);
}
if (tmp.RChild != null)
{
sq.In(tmp.RChild);
}
}
}
public TreeNode<T> Search(TreeNode<T> root, T val)
{
if (root == null)
{
return null;
}
CSeqQueue<TreeNode<T>> sq = new CSeqQueue<TreeNode<T>>();
sq.In(root);
while (!sq.IsEmpty())
{
TreeNode<T> tmp = sq.Out();
if (tmp.Data.Equals(val))
{
return tmp;
}
if (tmp.LChild != null)
{
sq.In(tmp.LChild);
}
if (tmp.RChild != null)
{
sq.In(tmp.RChild);
}
}
return null;
}
public int CountLeaf(TreeNode<T> root)
{
TreeNode<T> p = root;
if (p == null)
{
return 0;
}
else if (p.LChild == null && p.RChild == null)
{
return 1;
}
else
{
return CountLeaf(p.LChild) + CountLeaf(p.RChild);
}
}
public int GetDepth(TreeNode<T> root)
{
int ld;
int rd;
if (root == null)
{
return 0;
}
else if (root.LChild == null && root.RChild == null)
{
return 1;
}
else
{
ld = GetDepth(root.LChild);
rd = GetDepth(root.RChild);
return (ld > rd ? ld : rd) + 1;
}
}
}
}