using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayFind
{
/*****************************
剑指offer 名企面试官精讲典型编程题
*/
/*********:面试题3**********/
class Matrix
{
private int[,] m_data;
private int row=4, coloum=4;
public Matrix()
{
m_data = new int[4,4]{{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
}
public void Show()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
Console.Write( m_data[i, j]+" ");
Console.WriteLine();
}
}
public bool Find(int number)
{
bool found=false;
if (this == null || row <= 0 || coloum <= 0)
return false;
row =0;
coloum = coloum - 1;
while (row < 4 && coloum >= 0)
{
if (m_data[row, coloum] == number)
{
found = true;
break;
}
else if (m_data[row, coloum] > number)
coloum--;
else
row++;
}
Console.WriteLine(found);
return found;
}
}
/*********:面试题4**********/
class StringReplace
{
private char[] _str;
private int _len=100;
public StringReplace(int len)
{
_len = len;
_str = new char[_len];
}
public StringReplace(char[] str)
{
_str = new char[_len];
//str.CopyTo(_str, 0);
for (int i = 0; i < str.Length; i++)
_str[i] = str[i];
}
public void Show()
{
Console.WriteLine(_str);
}
public void Replace()
{
if (_str == null && _len <= 0)
return;
int originalLength = 0;
int numberofBlank = 0;
int i = 0;
while (_str[i] !='\0' )
{
++originalLength;
if (_str[i] == ' ')
++numberofBlank;
++i;
}
int newLength = originalLength + numberofBlank * 2;
if (newLength > _len)
return;
int indexOfold = originalLength;
int indexofnew = newLength;
while (indexOfold >= 0 && indexofnew > indexOfold)
{
if (_str[indexOfold] == ' ')
{
_str[indexofnew--] = '0';
_str[indexofnew--] = '2';
_str[indexofnew--] = '%';
}
else
{
_str[indexofnew--] = _str[indexOfold];
}
--indexOfold;
}
}
}
class Tree<T> where T : IComparable<T>
{
private T data;
private Tree<T> left;
private Tree<T> right;
public Tree(T nodeValue)
{
this.data = nodeValue;
this.left = null;
this.right = null;
}
public Tree()
{
// TODO: Complete member initialization
}
public T NodeData
{
get { return this.data; }
set { this.data = value; }
}
public Tree<T> LeftTree
{
get { return this.left; }
set { this.left = value; }
}
public Tree<T> RightTree
{
get { return this.right; }
set { this.right = value; }
}
public void Insert(T newItem)
{
T currentNodeValue = this.NodeData;
if (currentNodeValue.CompareTo(newItem) > 0)
{
if (this.LeftTree == null)
{
this.LeftTree = new Tree<T>(newItem);
}
else
{
this.LeftTree.Insert(newItem);
}
}
else
{
if (this.RightTree == null)
{
this.RightTree = new Tree<T>(newItem);
}
else
{
this.RightTree.Insert(newItem);
}
}
}
public void PreOrderTree(Tree<T> root)
{
if (root != null)
{
Console.Write(root.NodeData+"--");
PreOrderTree(root.LeftTree);
PreOrderTree(root.RightTree);
}
}
public void InOrderTree(Tree<T> root)
{
if (root != null)
{
InOrderTree(root.LeftTree);
Console.Write(root.NodeData + "--");
InOrderTree(root.RightTree);
}
}
public void PostOrderTree(Tree<T> root)
{
if (root != null)
{
PostOrderTree(root.LeftTree);
PostOrderTree(root.RightTree);
Console.Write(root.NodeData + "--");
}
}
public int Depth(Tree<T> root)
{
if (root == null)
return 0;
return Math.Max( Depth(root.LeftTree),Depth(root.RightTree))+1;
}
#region 二叉树中和为某一值的路径
public void FindPath(Tree<int> root,int expectedSum)
{
//if(num.GetType()!="int") {}
if (root == null)
return;
List<Tree<int>> path = new List<Tree<int>>();
int currentSum = 0;
FindPath(root, expectedSum, ref path, ref currentSum);
}
public void FindPath(Tree<int> root, int expectedSum, ref List<Tree<int>> path, ref int currentSum)
{
currentSum += root.data;
path.Add(root);
Boolean isLeaf = root.LeftTree == null && root.RightTree == null;
if (isLeaf && currentSum == expectedSum)
{
Console.WriteLine("A path is found:");
foreach (Tree<int> pNode in path)
{
Console.Write(pNode.data + " ");
}
Console.WriteLine();
}
if(root.LeftTree!=null)
{
FindPath(root.LeftTree, expectedSum, ref path, ref currentSum);
}
if (root.RightTree != null)
{
FindPath(root.RightTree, expectedSum, ref path, ref currentSum);
}
currentSum -= root.data;
path.RemoveAt(path.Count()-1);
}
#endregion
#region 判断一棵树是否为平衡二叉树
public bool IsBanlance(Tree<T> root)
{
if (root == null)
return true;
bool isBanlance= Math.Abs( Depth(root.LeftTree) - Depth(root.RightTree))>1?false:true;
if (isBanlance == false)
return false;
else
return IsBanlance(root.LeftTree) && IsBanlance(root.RightTree);
}
#endregion
#region 打印二叉树的结构,
public void TreePrint(Tree<T> root,Tree<T> flag)
{
String str = new String('*', 4);
Queue<Tree<T>> queue = new Queue<Tree<T>>();
if (root == null)
return;
queue.Enqueue(root);
int i = 0, j = 0;
int depth = Depth(root);
while (queue.Count>0)
{
Tree<T> temp = queue.Dequeue();
if (temp.data.CompareTo(flag.data) == 0)
{
for (int p = 0; p < Convert.ToInt32(Math.Pow(2, depth)); p++)
Console.Write(str);
}
else
{
for (int p = 0; p < Convert.ToInt32(Math.Pow(2, depth) / 2); p++)
Console.Write(str);
Console.Write(temp.data);
for (int p = 0; p < Convert.ToInt32(Math.Pow(2, depth) / 2); p++)
Console.Write(str);
}
if (depth>0)
{
if (temp.LeftTree != null)
queue.Enqueue(temp.LeftTree);
else
queue.Enqueue(flag);
if (temp.RightTree != null)
queue.Enqueue(temp.RightTree);
else
queue.Enqueue(flag);
}
if (i == j)
{
i = 0;
j = queue.Count()-1;
depth--;
Console.WriteLine();
}
else
i++;
}
}
#endregion
public Tree<T> Construct(List<T> preoder,List<T> inorder )
{
if (preoder.Count == 0 || inorder.Count == 0 || (preoder.Count != inorder.Count))
return null;
Tree<T> root=null;
root =ConstructCore(preoder, inorder);
return root;
}
#region 树的重建
Tree<T> ConstructCore(List<T> preoder, List<T> inorder)
{
// root.data = preoder[0];
//root.LeftTree = root.RightTree = null;
if (preoder.Count == 0||inorder.Count==0)
return null;
int i = 0;
while (i < preoder.Count && preoder[0].CompareTo( inorder[i])!=0)
i++;
Tree<T> root = new Tree<T>(preoder[0]);
if (i == 0)
{
root.LeftTree = null;
root.RightTree = ConstructCore(preoder.GetRange(i + 1, preoder.Count - i - 1), inorder.GetRange(i + 1, inorder.Count - i - 1));
}
else if (i == preoder.Count - 1)
{
root.RightTree = null;
root.LeftTree = ConstructCore(preoder.GetRange(1, i), inorder.GetRange(0, i));
}
else
{
root.LeftTree = ConstructCore(preoder.GetRange(1, i), inorder.GetRange(0, i));
root.RightTree = ConstructCore(preoder.GetRange(i + 1, preoder.Count - i - 1), inorder.GetRange(i + 1, inorder.Count - i - 1));
}
return root;
}
#endregion
#region 树的子结构
public bool HasSubTree(Tree<T> root1, Tree<T> root2)
{
bool restult = false;
if (root1 != null && root2 != null)
{
if (root1.NodeData.CompareTo(root2.NodeData) == 0)
restult = DoesTree1HasTree2(root1, root2);
if (!restult)
restult = HasSubTree(root1.LeftTree, root2);
if (!restult)
restult = HasSubTree(root1.RightTree, root2);
}
return restult;
}
public bool DoesTree1HasTree2(Tree<T> root1, Tree<T> root2)
{
if (root2 == null)
return true;
if (root1 == null)
return false;
if (root1.NodeData.CompareTo(root2.NodeData) != 0)
return false;
return DoesTree1HasTree2(root1.LeftTree, root2.LeftTree) && DoesTree1HasTree2(root1.RightTree, root2.RightTree);
}
#endregion
/// <summary>
/// 判断一个序列是否为二叉搜索树的后序序列
/// </summary>
/// <param name="sequence"></param>
/// <returns></returns>
public Boolean VerifySequenceOfBST(List<T> sequence)
{
if (sequence.Count == 0)
return false;
T root = sequence.Last();
int i = 0;
for (; i < sequence.Count-1; i++)
{
if (sequence[i].CompareTo( root)>0)
break;
}
int j = i;
for (; j < sequence.Count - 1; j++)
if (sequence[j].CompareTo(root) < 0)
return false;
bool left = true;
bool right = true;
if(i>0)
left = VerifySequenceOfBST(sequence.GetRange(0,i));
if (i < sequence.Count - 1)
right = VerifySequenceOfBST(sequence.GetRange(i+1,sequence.Count-1));
return left && right;
}
public Tree<T> ConvertTreeToDList()
{
Tree<T> pLastNodeInList = null;
ConvertNode(this, ref pLastNodeInList);
Tree<T> pHeadOfList = pLastNodeInList;
while (pHeadOfList != null && pHeadOfList.left != null)
pHeadOfList = pHeadOfList.left;
Console.WriteLine("\n*****转换后*****");
pLastNodeInList= pHeadOfList;
while (pLastNodeInList != null)
{
Console.Write(pLastNodeInList.data + "--");
pLastNodeInList = pLastNodeInList.right;
}
Console.WriteLine("\n*****end*****");
return pHeadOfList;
}
public void ConvertNode(Tree<T> pNode,ref Tree<T> pLastNodeInlist)
{
if (pNode == null)
return;
Tree<T> pCurrent = pNode;
if (pNode.LeftTree != null)
ConvertNode(pNode.LeftTree,ref pLastNodeInlist);
pCurrent.LeftTree = pLastNodeInlist;
if (pLastNodeInlist != null)
pLastNodeInlist.RightTree = pCurrent;
pLastNodeInlist = pCurrent;
if(pCurrent.RightTree!=null)
ConvertNode(pNode.RightTree, ref pLastNodeInlist);
}
}
}
class Program
{
internal static void Modify( String text)
{
text="world";
}
static void Main(string[] args)
{
Matrix matrix = new Matrix();
matrix.Show();
matrix.Find(7);
String str = "Hello";
str.ToUpper();
Modify(str);
char[] str1 ="a b c d e\0".ToCharArray();
StringReplace sr=new StringReplace(str1);
sr.Replace();
sr.Show();
Tree<int> tree = new Tree<int>(10);
tree.Insert(15);
tree.Insert(6);
tree.Insert(7);
tree.Insert(8);
tree.Insert(5);
//tree.Insert(12);
//tree.Insert(14);
//tree.Insert(13);
Console.Write("\n");
tree.PreOrderTree(tree);
Console.Write("\n");
tree.InOrderTree(tree);
Console.WriteLine(tree.Depth(tree));
Console.WriteLine(tree.IsBanlance(tree));
//int num =int.Parse( Console.ReadLine());
//tree.FindPath(tree, num);
Tree<int> flag = new Tree<int>(0);
tree.TreePrint(tree,flag);
}
}
}
边复习边写(树的操作)
最新推荐文章于 2024-06-09 16:38:14 发布