class Tree
{
private TreeNode root = null;
//键盘创建树
public void CreateTreeByKeyboard()
{
root = CrKeyboard();
}
//简略,没有考虑异常情况,先序,需要一个扩展二叉树的输入
private TreeNode CrKeyboard()
{
string numStr = Console.ReadLine();
if(numStr == "x")
{
return null;
}
else
{
TreeNode node = new TreeNode();
node.val = Int32.Parse(numStr);
node.left = CrKeyboard();
node.right = CrKeyboard();
return node;
}
}
//需要一个完全2叉树,数组创建树
public void CreateTreeByArray()
{
char[] nums = { '1', '2', '3', '4', '5', 'x', 'x', 'x', 'x', 'x', '6' };
TreeNode[] nodes = new TreeNode[nums.Length+1];
for (int i = 1; i < nodes.Length; i++)
{
if(nums[i-1] == 'x')
{
nodes[i] = null;
}
else
{
TreeNode node = new TreeNode();
node.val = Int32.Parse(nums[i-1] + "");
nodes[i] = node;
}
}
for (int i = 1; i < nodes.Length; i++)
{
if(nodes[i] != null)
{
if (2*i<= nodes.Length)
{
nodes[i].left = nodes[2*i];
}
if (2 * i + 1 <= nodes.Length)
{
nodes[i].right = nodes[2 * i + 1];
}
}
}
root = nodes[1];
}
//需要一个完全2叉树,数组创建树
public void CreateTreeByArray2()
{
char[] nums = { 's','1', '2', '3', '4', '5', 'x', 'x', 'x', 'x', 'x', '6' };
root = Create(nums,1,nums.Length);
}
private TreeNode Create(char[] nums,int i ,int n)
{
TreeNode node;
if (i > n)
return null;
else
{
if (nums[i] == 'x')
{
return null;
}
node = new TreeNode();
node.val = int.Parse(nums[i] + "");
node.left = Create(nums,2*i,n);
node.right = Create(nums, 2*i + 1, n);
return node;
}
}
//交换所有左右孩子
public void SwapAll()
{
Queue<TreeNode> nodes = new Queue<TreeNode>();
TreeNode current;
if (root != null)
{
nodes.Enqueue(root);
}
while(nodes.Count!=0)
{
current = nodes.Dequeue();
swapChildren(current, current.left,current.right);
if(current.left != null)
nodes.Enqueue(current.left);
if(current.right != null)
nodes.Enqueue(current.right);
}
}
private void swapChildren(TreeNode parent,TreeNode left,TreeNode right)
{
parent.left = right;
parent.right = left;
}
//删除一颗子树
public void DeleteSubTree(int value)
{
if (root == null)
return;
if (root.val == value)
{
root = null;
return;
}
TreeNode node = root;
TreeNode father = null;
Stack<TreeNode> nodes = new Stack<TreeNode>();
while(node!= null||nodes.Count!=0)
{
while(node!=null)
{
if(node.val == value)
{
if(father.left == node)
{
father.left = null;
}
else
{
father.right = null;
}
}
father = node;
nodes.Push(node);
node = node.left;
}
father = nodes.Pop();
node = father.right;
}
}
//递归求树的高度
public int Depth()
{
return GetDepth(root);
}
private int GetDepth(TreeNode node)
{
if (node == null)
return 0;
else
return 1 + Math.Max( GetDepth(node.left),GetDepth(node.right) );
}
//非递归求树的高度
public int DepthByStack()
{
Stack<TreeNode> nodes= new Stack<TreeNode>();
Stack<int> depths = new Stack<int>();
TreeNode node = root;
int curdepth;
int maxdepth = 0;
if(root != null)
{
curdepth = 1;
while(node!= null|| nodes.Count!=0)
{
while(node!= null)
{
nodes.Push(node);
depths.Push(curdepth);
node = node.left;
curdepth++;
}
node = nodes.Pop();
curdepth = depths.Pop();
if (node.left == null && node.right == null)
if (curdepth > maxdepth)
maxdepth = curdepth;
node = node.right;
curdepth++;
}
}
return maxdepth;
}
//查找某一个节点所处的层次
public int NodeLayer(int value)
{
Stack<TreeNode> nodes = new Stack<TreeNode>();
Stack<int> accessable = new Stack<int>();
TreeNode node = root;
int accessFlag;
while (nodes.Count != 0 || node != null)
{
while (node != null)
{
nodes.Push(node);
accessable.Push(0);
node = node.left;
}
node = nodes.Pop();
accessFlag = accessable.Pop();
if (accessFlag == 0)
{
nodes.Push(node);
accessable.Push(1);
node = node.right;
}
else
{
if (node.val == value)
return nodes.Count + 1;
node = null;
}
}
return -1;
}
//先序递归遍历
public void TraversePreOrder()
{
TraversePre(root);
}
private void TraversePre(TreeNode node)
{
if(node== null)
return;
else
{
Console.WriteLine(node.val);
TraversePre(node.left);
TraversePre(node.right);
}
}
//先序非递归遍历
public void TraversePreByStack()
{
Stack<TreeNode> nodes = new Stack<TreeNode>();
TreeNode node = root;
while(nodes.Count!=0||node != null)
{
while (node!=null)
{
Console.WriteLine(node.val);
nodes.Push(node);
node = node.left;
}
node = nodes.Pop();
node = node.right;
}
}
//中序非递归遍历
public void TraverseInByStack()
{
Stack<TreeNode> nodes = new Stack<TreeNode>();
TreeNode node = root;
while (nodes.Count != 0 || node != null)
{
while (node != null)
{
nodes.Push(node);
node = node.left;
}
node = nodes.Pop();
Console.WriteLine(node.val);
node = node.right;
}
}
//后序非递归遍历
public void TraversePostByStack()
{
Stack<TreeNode> nodes = new Stack<TreeNode>();
Stack<int> accessable = new Stack<int>();
TreeNode node = root;
int accessFlag;
while (nodes.Count != 0 || node != null)
{
while (node != null)
{
nodes.Push(node);
accessable.Push(0);
node = node.left;
}
node = nodes.Pop();
accessFlag = accessable.Pop();
if(accessFlag==0)
{
nodes.Push(node);
accessable.Push(1);
node = node.right;
}
else
{
Console.WriteLine(node.val);
node = null;
}
}
}
//层次遍历
public void TraverseByLayer()
{
Queue<TreeNode> nodes = new Queue<TreeNode>();
TreeNode current;
if(root != null)
{
nodes.Enqueue(root);
}
while (nodes.Count != 0)
{
current = nodes.Dequeue();
Console.WriteLine(current.val);
if(current.left != null)
nodes.Enqueue(current.left);
if(current.right != null)
nodes.Enqueue(current.right);
}
}
}
class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
}
树的各种操作
最新推荐文章于 2022-08-28 12:29:52 发布