using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sort
{
public class Program
{
public class Node
{
public Node(string strSend)
{
this.Value = strSend;
}
public Node Left { get; set; }
public Node Right { get; set; }
public string Value { get; set; }
}
public static Node BuildTree()
{
Node a = new Node("A");
Node b = new Node("B");
Node c = new Node("C");
a.Left = b;
a.Right = c;
Node d = new Node("D");
Node e = new Node("E");
b.Left = d;
b.Right = e;
Node f = new Node("F");
Node g = new Node("G");
c.Left = f;
c.Right = g;
Node h = new Node("H");
Node i = new Node("I");
d.Left = h;
d.Right = i;
Node j = new Node("J");
Node k = new Node("K");
f.Left = j;
g.Right = k;
return a;
}
static void Main(string[] args)
{
// var root = BuildTree();
// PrintPath(root, null);
// Node f = new Node("F");
// Parent(root, f);
// f = new Node("C");
// Child(root,f);
Parent(root, f);
插入冒泡排序
//BubbleSort();
ConcreteSubject subject = new ConcreteSubject();
subject.Attach(new ConcreteObserver(subject, "Observer A"));
subject.Attach(new ConcreteObserver(subject, "Observer B"));
subject.Attach(new ConcreteObserver(subject, "Observer C"));
subject.Status = "Ready";
subject.Notify();
Console.Read();
Console.ReadLine();
}
public static void BubbleSort()
{
var temp = 0;
var j = 0;
var array = new int[]{ 43,343,42,5,6,76,2};
//插入排序
for (int i = 1; i < array.Length; i++)
{
temp = array[i];
j = i - 1;
while (j >= 0 && array[j]>temp)
{
array[j+1] = array[j];
j--;
}
array[j + 1] = temp;
}
冒泡排序
//for (int i = 0; i < array.Length; i++)
//{
// for (int j = i + 1; j < array.Length; j++)
// {
// if (array[i] > array[j])
// {
// temp = array[j];
// array[j] = array[i];
// array[i] = temp;
// }
// }
//}
foreach (var item in array)
{
Console.WriteLine(item);
}
}
public static void Child(Node root, Node find)
{
if (root != null)
{
if ((root.Left != null && root.Left.Value == find.Value) || (root.Right != null && root.Right.Value == find.Value))
{
string child = "";
if (root.Left != null && root.Left.Value == find.Value)
{
if (root.Left.Left != null) child = root.Left.Left.Value;
if(root.Left.Right != null ) child += "," +root.Left.Right.Value;
}
if (root.Right != null && root.Right.Value == find.Value)
{
if (root.Right.Left != null) child = root.Right.Left.Value;
if (root.Right.Right != null) child += "," + root.Right.Right.Value;
}
Console.WriteLine(find.Value + " Child's " + child);
}
// else if (root.Right != null && root.Right.Value == find.Value)
// Console.WriteLine(find.Value + " Parent's " + root.Value);
else
{
Child(root.Left, find);
Child(root.Right, find);
}
}
}
public static void Parent(Node root, Node find)
{
if (root != null)
{
if(root.Left != null && root.Left.Value == find.Value )
Console.WriteLine(find.Value + " Parent's " + root.Value);
else if (root.Right != null && root.Right.Value == find.Value)
Console.WriteLine(find.Value + " Parent's " + root.Value);
else
{
Parent(root.Left, find);
Parent(root.Right, find);
}
}
}
public static void PrintPath(Node send, Node Parent)
{
Console.WriteLine(send.Value, Parent);
if (send.Left != null)
PrintPath(send.Left, Parent);
if (send.Right != null)
PrintPath(send.Right, Parent);
}
public static Dictionary<string, string> FindParent(Node node, string parent)
{
return null;
}
public abstract class Subject
{
private IList<Observer> observers = new List<Observer>();
public void Attach(Observer obv)
{
observers.Add(obv);
}
public void Detach(Observer obv)
{
observers.Remove(obv);
}
public void Notify()
{
foreach (var item in observers)
{
item.Update();
}
}
}
public class ConcreteSubject : Subject
{
public string Status { get; set; }
}
public abstract class Observer
{
public abstract void Update();
}
public class ConcreteObserver : Observer
{
public string Name { get; set; }
public string ObserveStatus { get; set; }
private ConcreteSubject subject { get; set; }
public ConcreteSubject Subject
{
get { return subject; }
set { subject = value; }
}
public ConcreteObserver(ConcreteSubject send, string name)
{
this.subject = send;
this.Name = name;
}
public override void Update()
{
ObserveStatus = subject.Status;
Console.WriteLine("The observer's state of {0} is {1}", Name, ObserveStatus);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sort
{
public class Program
{
public class Node
{
public Node(string strSend)
{
this.Value = strSend;
}
public Node Left { get; set; }
public Node Right { get; set; }
public string Value { get; set; }
}
public static Node BuildTree()
{
Node a = new Node("A");
Node b = new Node("B");
Node c = new Node("C");
a.Left = b;
a.Right = c;
Node d = new Node("D");
Node e = new Node("E");
b.Left = d;
b.Right = e;
Node f = new Node("F");
Node g = new Node("G");
c.Left = f;
c.Right = g;
Node h = new Node("H");
Node i = new Node("I");
d.Left = h;
d.Right = i;
Node j = new Node("J");
Node k = new Node("K");
f.Left = j;
g.Right = k;
return a;
}
static void Main(string[] args)
{
// var root = BuildTree();
// PrintPath(root, null);
// Node f = new Node("F");
// Parent(root, f);
// f = new Node("C");
// Child(root,f);
Parent(root, f);
插入冒泡排序
//BubbleSort();
ConcreteSubject subject = new ConcreteSubject();
subject.Attach(new ConcreteObserver(subject, "Observer A"));
subject.Attach(new ConcreteObserver(subject, "Observer B"));
subject.Attach(new ConcreteObserver(subject, "Observer C"));
subject.Status = "Ready";
subject.Notify();
Console.Read();
Console.ReadLine();
}
public static void BubbleSort()
{
var temp = 0;
var j = 0;
var array = new int[]{ 43,343,42,5,6,76,2};
//插入排序
for (int i = 1; i < array.Length; i++)
{
temp = array[i];
j = i - 1;
while (j >= 0 && array[j]>temp)
{
array[j+1] = array[j];
j--;
}
array[j + 1] = temp;
}
冒泡排序
//for (int i = 0; i < array.Length; i++)
//{
// for (int j = i + 1; j < array.Length; j++)
// {
// if (array[i] > array[j])
// {
// temp = array[j];
// array[j] = array[i];
// array[i] = temp;
// }
// }
//}
foreach (var item in array)
{
Console.WriteLine(item);
}
}
public static void Child(Node root, Node find)
{
if (root != null)
{
if ((root.Left != null && root.Left.Value == find.Value) || (root.Right != null && root.Right.Value == find.Value))
{
string child = "";
if (root.Left != null && root.Left.Value == find.Value)
{
if (root.Left.Left != null) child = root.Left.Left.Value;
if(root.Left.Right != null ) child += "," +root.Left.Right.Value;
}
if (root.Right != null && root.Right.Value == find.Value)
{
if (root.Right.Left != null) child = root.Right.Left.Value;
if (root.Right.Right != null) child += "," + root.Right.Right.Value;
}
Console.WriteLine(find.Value + " Child's " + child);
}
// else if (root.Right != null && root.Right.Value == find.Value)
// Console.WriteLine(find.Value + " Parent's " + root.Value);
else
{
Child(root.Left, find);
Child(root.Right, find);
}
}
}
public static void Parent(Node root, Node find)
{
if (root != null)
{
if(root.Left != null && root.Left.Value == find.Value )
Console.WriteLine(find.Value + " Parent's " + root.Value);
else if (root.Right != null && root.Right.Value == find.Value)
Console.WriteLine(find.Value + " Parent's " + root.Value);
else
{
Parent(root.Left, find);
Parent(root.Right, find);
}
}
}
public static void PrintPath(Node send, Node Parent)
{
Console.WriteLine(send.Value, Parent);
if (send.Left != null)
PrintPath(send.Left, Parent);
if (send.Right != null)
PrintPath(send.Right, Parent);
}
public static Dictionary<string, string> FindParent(Node node, string parent)
{
return null;
}
public abstract class Subject
{
private IList<Observer> observers = new List<Observer>();
public void Attach(Observer obv)
{
observers.Add(obv);
}
public void Detach(Observer obv)
{
observers.Remove(obv);
}
public void Notify()
{
foreach (var item in observers)
{
item.Update();
}
}
}
public class ConcreteSubject : Subject
{
public string Status { get; set; }
}
public abstract class Observer
{
public abstract void Update();
}
public class ConcreteObserver : Observer
{
public string Name { get; set; }
public string ObserveStatus { get; set; }
private ConcreteSubject subject { get; set; }
public ConcreteSubject Subject
{
get { return subject; }
set { subject = value; }
}
public ConcreteObserver(ConcreteSubject send, string name)
{
this.subject = send;
this.Name = name;
}
public override void Update()
{
ObserveStatus = subject.Status;
Console.WriteLine("The observer's state of {0} is {1}", Name, ObserveStatus);
}
}
}
}