数据结构---树与二叉树(3)
三、线索二叉树
定义:利用节点子树为空时,指向孩子的链为空值的链来指明节点在某种遍历次序下的前驱和后继结点,就构成线索二叉树。
ThreadTreeNode.cs
using System;
public class ThreadTreeNode
{
public char data;
public ThreadTreeNode left,right;
public int ltag,rtag;
public ThreadTreeNode(char ch)
{
data = ch;
left=right=null;
ltag=rtag=0;
}
public void preorderChild(ThreadTreeNode p)
{
if(p!=null)
{
Console.Write(p.data+" ");
preorderChild(p.left);
preorderChild(p.right);
}
}
}
ThreadTree.cs
using System;
public class ThreadTree
{
public ThreadTreeNode root;
public static ThreadTreeNode front=null;
public static int i=0;
public myStack<char> mq;
public ThreadTree(string str)
{
root = null;
if(str!=null)
{
root=create(str);
}
}
public ThreadTreeNode create(string str)
{
ThreadTreeNode p=null;
if(str[i]!='.')
{
p = new ThreadTreeNode(str[i]);
i++;
p.left=create(str);
p.right=create(str);
}
else
i++;
if(p==null)
Console.WriteLine("返回null");
else
Console.WriteLine(p.data+" ");
return p;
}
public void inThreadTree()
{
inThreadTree(root);
}
public void inThreadTree(ThreadTreeNode p)
{
if(p!=null)
{
inThreadTree(p.left);
if(p.left==null)
{
p.ltag=1;
p.left=front;
}
if(p.right==null)
{
p.rtag = 1;
}
if(front!=null && front.rtag==1)
front.right=p;
if(front!=null)
Console.Write("front= "+ front.data+" ");
else
Console.Write("front=null ");
Console.Write("p="+p.data+" ");
Console.Write("ltag="+p.ltag+" ");
Console.WriteLine("rtag="+p.rtag+" ");
front = p;
inThreadTree(p.right);
}
}
public ThreadTreeNode innext(ThreadTreeNode p)
{
if(p.rtag==1)
p=p.right;
else
{
p=p.right;
while(p.ltag==0)
p=p.left;
}
return p;
}
//中根遍历线索二叉树
public void inorder()
{
ThreadTreeNode p=root;
if(p!=null)
{
while(p.ltag==0)
p=p.left;
do
{
Console.Write(p.data+" ");
p=innext(p);
}while(p!=null);
Console.WriteLine();
}
}
public ThreadTreeNode inlast(ThreadTreeNode p)
{
if(p.ltag==1)
p=p.left;
else
{
p=p.left;
while(p.rtag==0)
p=p.right;
}
return p;
}
public void inorder_last()
{}
public ThreadTreeNode prenext(ThreadTreeNode p)
{
if(p.ltag==0)
{
p=p.left;
}
else
{
if(p.rtag==0)
p=p.right;
else
{
while(p.rtag==1 && p.right!=null)
p=p.right;
p=p.right;
}
}
return p;
}
//先根遍历线索二叉树
public void preorder()
{
ThreadTreeNode p=root;
if(p!=null)
{
do{
Console.Write(p.data+" ");
p=prenext(p);
}while(p!=null);
}
Console.WriteLine();
}
public ThreadTreeNode postlast(ThreadTreeNode p)
{
if(p.rtag==0)
p=p.right;
else
{
while(p.ltag==1 && p.left!=null)
p=p.left;
p=p.left;
}
return p;
}
public void postorder()
{
ThreadTreeNode p=root;
mq = new myStack<char>(20);
if(p!=null)
{
do{
//Console.Write(p.data+" ");
mq.Push(p.data);
p=postlast(p);
}while(p!=null);
}
while(!mq.IsEmpty())
{
Console.Write(mq.Pop()+" ");
}
Console.WriteLine();
}
public void print()
{
root.preorderChild(root);
}
public static void Main()
{
string str = "ABD..EG
CF.H
#";ThreadTree tt = new ThreadTree(str);
//tt.print();
tt.inThreadTree();
tt.inorder();
tt.preorder();
tt.postorder();
}
}
因为后跟次序遍历线索二叉树时需要使用到辅助结构栈,myStack见:
作 者:doku
出 处:http://www.cnblogs.com/kulong995/
关于作者:喜欢编程,喜欢美食,专注于.NET项目开发。
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!


浙公网安备 33010602011771号