Data Structure and Algorithm (C # Implementation) Series --- Binary Tree

zhaozj2021-02-16  47

Data Structure and Algorithm (C # Implementation) Series --- Binary Tree

Using system;

Using system.collections;

Namespace Datastructure

{

///

/// binarytree's summary description.

///

Public Class BinaryTree: Narytree

{

// Construct a binary empty tree

Public binarytree (): Base (2)

{

//

// TODO: Add constructor logic here

//

}

Public binarytree (Object _obj): Base (2, _obj)

{}

/ / -------------------------------------------------------------------------------------------- ------------------

Protected Override Object getemptyInstance (uint _degree)

{Return New BinaryTree (_degree);

/ / -------------------------------------------------------------------------------------------- ------------------

// Rewrite deep traversal

Public Override Void DepthfirstTraversal (iPrePostvisitor_VIS)

{

IF (! iSempty ())

{

_vis.previsit (this.key);

THIS [0] .depthfirsttraversal (_VIS);

_vis.visit (this.key);

THIS [1] .depthfirsttraversal (_VIS);

_vis.postvisit (this.key);

}

}

// bifurcated comparison

// First compare keywords, if equally, compare the left subtree, if you wait, compare the right tree ---- so recursive

#Region iComparable member

Public override int compareto (Object Obj)

{

// Todo: Add binarytree.compareto implementation

// Because COMARE () has been disconnected, there is no transformation error

BinaryTree Tmptree = (binarytree) OBJ;

IF (this.isempty ())

Return TMPTREE.ISEMPTY ()? 0: -1;

IF (tmptree.isempty ())

Return 1;

Int result = comparer.default.compare (this, tmptree);

IF (result == 0)

Result = this [0] .compareto (TMPTREE [0]);

IF (result == 0)

Result = this [1] .compareto (TMPTREE [1]);

Return Result;

}

#ndregion

}

}

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

New Post(0)