数据结构与算法(C#实现)系列---演示篇(三)

                   数据结构与算法(C#实现)系列---演示篇()

                            Heavenkiller(原创)

 

         public static void ShowSortedList_Polynomial()

         {

              //100+10*x+x^2  + 1+10*x+100x^2

              SortedList tmpListA=new SortedList();

              SortedList tmpListB=new SortedList();

              SortedList tmpListC=new SortedList();//used to store the result

              SortedList tmpKeyList=new SortedList();//used to store all keys of two polynomials

 

 

              //init polynomial A and show it

              tmpListA.Add(0,100);

              tmpListA.Add(1,10);

              tmpListA.Add(2,1);

              ShowSortedList_ShowPolynomial("tmpListA",tmpListA.GetEnumerator());

 

              //init polynomial B and show it

              tmpListB.Add(0,1);

              tmpListB.Add(1,10);

              tmpListB.Add(2,100);

              ShowSortedList_ShowPolynomial("tmpListB",tmpListB.GetEnumerator());

 

              //init the key list which contains all keys of A and B but everyone once

              IDictionaryEnumerator tmpIDic=tmpListA.GetEnumerator();

              while(tmpIDic.MoveNext()!=false)

              {

                   if(!tmpKeyList.ContainsKey(tmpIDic.Key))

                   {

                       tmpKeyList.Add(tmpIDic.Key,null);

                   }

              }

 

              tmpIDic=tmpListB.GetEnumerator();

              while(tmpIDic.MoveNext()!=false)

              {

                   if(!tmpKeyList.ContainsKey(tmpIDic.Key))

                   {

                       tmpKeyList.Add(tmpIDic.Key,null);

                   }

              }

 

              //Add A and B and show the result

              tmpIDic=tmpKeyList.GetEnumerator();

              while(tmpIDic.MoveNext()!=false)

              {

                   object objA=null,objB=null,objC=null;

                   objC=tmpIDic.Key;

                   if(tmpListA.ContainsKey(objC))

                       objA=tmpListA[objC];

                   if(tmpListA.ContainsKey(objC))

                       objB=tmpListB[objC];

                   //objC=objA+objB;

                   //tmpKeyList[objC]=(int)objA+(int)objC;

                   tmpListC.Add(objC,(int)objA+(int)objB);

 

              }

              ShowSortedList_ShowPolynomial("the addition result of A and B",tmpListC.GetEnumerator());

 

        

             

         }

         public static void ShowSortedList_ShowPolynomial(string tip,IDictionaryEnumerator iDic)

         {

              string strExpress=null;

              iDic.Reset();

              while(iDic.MoveNext()!=false)

              {

                   strExpress+=iDic.Value.ToString()+"*X^"+iDic.Key.ToString()+"+";

              }

              Console.WriteLine(tip+":"+strExpress);

 

         }

 

    

        

    

}

using System; using QueueDs; namespace BinaryTreeDs { public class LinkBiTree<T> { private Node<T> head; //头引用 //头引用属性 public Node<T> Head { get { return head; } set { head = value; } } //构造函数 public LinkBiTree() { head = null; } //构造函数 public LinkBiTree(T val) { Node<T> p = new Node<T>(val); head = p; } //构造函数 public LinkBiTree(T val, Node<T> lp, Node<T> rp) { Node<T> p = new Node<T>(val, lp, rp); head = p; } //判断是否是空二叉树 public bool IsEmpty() { if (head == null) { return true; } else { return false; } } //获取根结点 public Node<T> Root() { return head; } //获取结点的左孩子结点 public Node<T> GetLChild(Node<T> p) { return p.LChild; } //获取结点的右孩子结点 public Node<T> GetRChild(Node<T> p) { return p.RChild; } //将结点p的左子树插入值为val的新结点, //原来的左子树成为新结点的左子树 public void InsertL(T val, Node<T> p) { Node<T> tmp = new Node<T>(val); tmp.LChild = p.LChild; p.LChild = tmp; } //将结点p的右子树插入值为val的新结点, //原来的右子树成为新结点的右子树 public void InsertR(T val, Node<T> p) { Node<T> tmp = new Node<T>(val); tmp.RChild = p.RChild; p.RChild = tmp; } //若p非空,删除p的左子树 public Node<T> DeleteL(Node<T> p) { if ((p == null) || (p.LChild == null)) { return null; } Node<T> tmp = p.LChild; p.LChild = null; return tmp; } //若p非空,删除p的右子树 public Node<T> DeleteR(Node<T> p) { if ((p == null) || (p.RChild == null)) { return null; } Node<T> tmp = p.RChild; p.RChild = null; return tmp; } //编写算法,在二叉树中查找值为value的结点 public Node<T> Search(Node<T> root, T value) { Node<T> p = root; if (p == null) { return null; } if (!p.Data.Equals(value)) { return p; } if (p.LChild != null) { return Search(p.LChild, value); } if (p.RChild != null) { return Search(p.RChild, value); } return null; } //判断是否是叶子结点 public bool IsLeaf(Node<T> p) { if ((p != null) && (p.LChild == null) && (p.RChild == null)) { return true; } else { return false; } } //中序遍历 public void inorder(Node<T> ptr) { if (IsEmpty()) { Console.WriteLine("Tree is empty"); return; } if (ptr != null) { inorder(ptr.LChild); Console.Write(ptr.Data + " "); inorder(ptr.RChild); } } //前序遍历 public void preorder(Node<T> ptr) { if (IsEmpty()) { Console.WriteLine("Tree is empty"); return; } if (ptr != null) { Console.Write(ptr.Data + " "); preorder(ptr.LChild); preorder(ptr.RChild); } } //后序列遍历 public void postorder(Node<T> ptr) { if (IsEmpty( )) { Console.WriteLine("Tree is empty"); return; } if (ptr != null) { postorder(ptr.LChild); postorder(ptr.RChild); Console.Write(ptr.Data + " "); } } public void LevelOrder(Node<T> root) { //根结点为空 if (root == null) { return; } //设置一个队列保存层序遍历的结点 CSeqQueue<Node<T>> sq = new CSeqQueue<Node<T>>(50); //根结点入队 sq.EnQueue(root); //队列非空,结点没有处理完 while (!sq.IsEmpty()) { //结点出队 Node<T> tmp = sq.DeQueue(); //处理当前结点 Console.WriteLine("{o}", tmp); //将当前结点的左孩子结点入队 if (tmp.LChild != null) { sq.EnQueue(tmp.LChild); } if (tmp.RChild != null) { //将当前结点的右孩子结点入队 sq.EnQueue(tmp.RChild); } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值