C # written

xiaoxiao2021-03-06  49

C # is indeed a good object-oriented language, I see "Data Structure (Second Edition)" The book should be a version described with C #. Here is a tree I wrote with C #. First use the interface to make an abstract definition, so that only the interface is operated while implementing traversal, insertion, etc. In the program, I try to use the characteristics of C #, such as interface, attribute, and rising, although it looks more often, when the code is getting more and more, you will see the advantages, because reasonable structure Let you think clearly. I can only count a beginning, because if you want to write all the types of trees and the algorithms above them, I see that there is no 1 ~ 2k line program is absolutely not, but as long as there is time. I will definitely write, and I hope everyone will write, and improve this code base.

Using system; use system.collections; namespace tree {/// LEFT left subtree, Right Right}; /// link points to children, Thread points to enum tag {link, thread}; // binary node abstract definition interface ibinnode {bool isleaf (); object element {get; set;} ibinnode left {get; set;} ibinnode right {get; set;}} /// traversal, clues, etc. Interface interface ITravelBinTree {void PreOrderTravel (); void InOrderTravel (); void RevOrderTravel (); void Print (IBinNode t);} interface IInsertBinTree {void Insert (IBinNode node, Position pos);} /// Normal actualize of bintree class BinNodePtr : IBinNode {protected object element; protected IBinNode lchild; protected IBinNode rchild; public BinNodePtr (object e, IBinNode left, IBinNode right) {element = e; lchild = left; rchild = right;} public BinNodePtr (object e) {element = e; lchild = rchild = null;} public BinNodePtr () {element = null; lchild = rchild = null;} public bool isLeaf () {if (lchild == null && rchild == null) return true; return false;} pub lic object Element {get {return element;} set {element = value;}} public IBinNode Left {get {return lchild;} set {lchild = value;}} public IBinNode Right {get {return rchild;} set {rchild = value;}}} class BinNodeLine: BinNodePtr, IBinNode {private Tag ltag, rtag; public BinNodeLine (object e, IBinNode left, IBinNode right): base (e, left, right) {ltag = rtag = Tag.LINK;} public Binnodeline (Object E): Base (e) {ltag =} public tag ltag {get {return ltag;} set {ltag = value;}} public tag = value;}} public tag = value;}} RTAG = VALUE;

}}} Class TravelBinTree: ITravelBinTree, IInsertBinTree {const int INIT_TREE_SIZE = 20; private IBinNode tree; private BinNodeLine head; head pointer // private IBinNode prenode after threading; // points to the predecessor node most recently visited public TravelBinTree () { tree = new BinNodePtr ();} public TravelBinTree (IBinNode INode) {tree = INode;} /// first traversal tree, realized public void PreOrderTravel () {IBinNode temptree non recursive algorithm; Stack stk = new Stack (INIT_TREE_SIZE) IF (Tree == NULL) Throw ("Accessing tree is empty")); Temptree = Tree; Stk.push (Tree); while (stk.count! = 0) {while (TempTree! = NULL ) {Print (Temptree); Temptree = Temptree.Left;} stk.pop (); // Empty pointer retracting IF (stk.count! = 0) {TempTree = (ibinnode) stk.Pop (); stk.Push (temptree.Right); temptree = temptree.Right;}}} public void InOrderTravel () {InOrderTravel (tree);} private void InOrderTravel (IBinNode t) {if (t == null ) Return; inordertravel (T.LEFT); Print (T); inorde rTravel (t.Right);} public void RevOrderTravel () {RevOrderTravel (tree);} private void RevOrderTravel (IBinNode t) {if (t == null) return; RevOrderTravel (t.Left); RevOrderTravel (t.Right) PRINT (T);} public void print (Ibinnode T) {Console.write (T.Element ",");} public void insert (IbinNode Node, Position POS) {if (Node == Null) Throw (New INVALIDOPERATIONEXCEPTION ("You cannot insert a node into the tree"))); switch (pOS) {copy position.Left: Tree.Left = node; break; case position.right: Tree.right = node; break;}} // Prunitary traversal sequence traversal tree public void treebuilder () {stack stk = new stack (init_tree_size);

转载请注明原文地址:https://www.9cbs.com/read-55052.html

New Post(0)