重新整理数据结构与算法(c#)—— 线索化二叉树[二十]
前言
为什么会有线索化二叉树呢?
是这样子的,二叉树呢,比如有n个节点,那么就有n+1个空指针域。
这个是怎么来的呢?比如我们假如一个节点都有左子树和右子树,那么就有2n个节点。
但是我们发现连接我们节点的一共有n-1个(要把n个节点串起来),那么就等于2n-(n-1)=n+1。
那么如何利用这些空间?
假如中序遍历如下:
{8,3,10,1,6,14}
那么如果3的右节点为空,就指向它的后驱节点,也就是10。
如果3的左节点为空,那么就指向它的前驱节点,也就是8。
以此类推,8的前驱节点为空,如果左节点为空,那么就是空。
职业盗图:
正文
创建节点模型:
public class ThreadedHeroNode
{
private int no;
private string name;
private ThreadedHeroNode left;
private ThreadedHeroNode right;
//0 表示左子树,1表示后继节点
private int leftType;
//0 表示右子树,1表示后继节点
private int rightType;
public int getLeftType()
{
return leftType;
}
public void setLeftType(int leftType)
{
this.leftType = leftType;
}
public int getRightType()
{
return rightType;
}
public void setRightType(int rightType)
{
this.rightType = rightType;
}
public ThreadedHeroNode(int no, string name)
{
this.no = no;
this.name = name;
}
public int getNo()
{
return no;
}
public void setNo(int no)
{
this.no = no;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public ThreadedHeroNode getLeft()
{
return left;
}
public void setLeft(ThreadedHeroNode left)
{
this.left = left;
}
public ThreadedHeroNode getRight()
{
return right;
}
public void setRight(ThreadedHeroNode right)
{
this.right = right;
}
public override string ToString()
{
return "姓名:" + name + "编号:" + no;
}
public void record()
{
Console.WriteLine("查找步骤为:名字" + this.name + " 编号:" + this.no);
}
}
创建树模型:
public class ThreadedBinaryTree
{
private ThreadedHeroNode root;
private ThreadedHeroNode pre;
public void setRoot(ThreadedHeroNode root)
{
this.root = root;
}
//中序遍历线索化二叉树
public void threadedList()
{
ThreadedHeroNode node = root;
while (node!=null)
{
//判断是否是
while (node.getLeftType() == 0)
{
node = node.getLeft();
}
Console.WriteLine(node);
while (node.getRightType() == 1)
{
node = node.getRight();
Console.WriteLine(node);
}
node=node.getRight();
}
}
public void threadedNodes()
{
threadedNodes(root);
}
public void threadedNodes(ThreadedHeroNode node)
{
if (node == null)
{
return;
}
//线索化左子树
threadedNodes(node.getLeft());
if (node.getLeft() == null)
{
node.setLeft(pre);
node.setLeftType(1);
}
if (pre != null&& pre.getRight() == null)
{
pre.setRight(node);
pre.setRightType(1);
}
pre = node;
//线索化右子树
threadedNodes(node.getRight());
}
}
测试:
static void Main(string[] args)
{
//测试一把中序线索二叉树的功能
ThreadedHeroNode root = new ThreadedHeroNode(1, "tom");
ThreadedHeroNode node2 = new ThreadedHeroNode(3, "jack");
ThreadedHeroNode node3 = new ThreadedHeroNode(6, "smith");
ThreadedHeroNode node4 = new ThreadedHeroNode(8, "mary");
ThreadedHeroNode node5 = new ThreadedHeroNode(10, "king");
ThreadedHeroNode node6 = new ThreadedHeroNode(14, "dim");
//二叉树,后面我们要递归创建, 现在简单处理使用手动创建
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node3.setLeft(node6);
//测试中序线索化
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
threadedBinaryTree.setRoot(root);
threadedBinaryTree.threadedNodes();
//当线索化二叉树后,能在使用原来的遍历方法
Console.WriteLine("使用线索化的方式遍历 线索化二叉树");
threadedBinaryTree.threadedList(); // 8, 3, 10, 1, 14, 6
Console.ReadKey();
}
测试结果: