C#进行二叉树排序的代码是什么?

作者&投稿:机侍 (若有异议请与网页底部的电邮联系)
C语言 排序二叉树~

#include #include struct node{int data;struct node *lc,*rc;}*head,*s;//-----必须传递二维指针才能改变实参指针变量的值!!void find(struct node **p,struct node *x){if((*p)==NULL) *p=x;else if(x->datadata)find( &((*p)->lc),x);else find( &((*p)->rc),x);}void print(struct node *p){if(p!=NULL){print(p->lc);printf("%d ",p->data);print(p->rc);}}int main (){FILE *fin=fopen("排序二叉树.txt","r");int a;while(!feof(fin)){fscanf(fin,"%d",&a);s=(struct node *)malloc(sizeof(struct node));//--s->data=a;s->lc=s->rc=NULL ;//--find( &head,s);//--}print(head);fclose(fin);system("pause");return 0;}

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "malloc.h"

typedef struct _student {
char number[20];
char name[20];
int score;
}Student;

typedef struct _node {
Student student;
_node *lchild, *rchild;
}Node, *PNode, BSTree, *PBSTree;

void BSTreeInsert(PBSTree *root, Student s) {
if(*root == NULL) {
*root = (PBSTree) malloc (sizeof(BSTree));
strcpy((*root)->student.number, s.number);
strcpy((*root)->student.name, s.name);
(*root)->student.score = s.score;
(*root)->lchild = NULL;
(*root)->rchild = NULL;
return;
}
else if(s.score student.score)
BSTreeInsert(&((*root)->lchild), s);
else
BSTreeInsert(&((*root)->rchild), s);
}

void InOrder(PBSTree root) {
if(!root) return;
InOrder(root->lchild);
printf("%s%s%d
", root->student.number, root->student.name, root->student.score);
InOrder(root->rchild);
}

void main( ) {
int i, j, n, _class;
PBSTree bstree[20];
Student s;
for(i = 0; i < 20; i++) bstree[i] = NULL;
printf("请输入班级数!
");
scanf("%d", &_class);
for(j = 0; j < _class; j++) {
printf("请输入%d班的学生人数!
", j+1);
scanf("%d", &n);
printf("请输入学生信息:学号 姓名 成绩
");
for(i = 0; i < n; i++) {
scanf("%s %s %d", s.number, s.name, &s.score);
BSTreeInsert(&bstree[i], s);
}

printf("
从低分到高分排序后的结果为:
");
InOrder(bstree[j]);
}
getch( );
}

public interface ISorttedBinaryTree
{
void InsertElement(IComparable val) ;//如果树中有一个节点的值等于val的值,则val将被忽略
void RemoveElement(IComparable val) ;
bool ContainsElement(IComparable var) ;
ArrayList GetAllNodesAscend(bool ascend) ;//ArrayList 中是Node
//遍历二叉树
ArrayList Traverse(TraverseMode mode) ; //ArrayList 中是Node

IComparable MaxElement {get ;}
IComparable MinElement {get ;}
Node Root {get ;}
int Depth {get ;}
int Count {get ;}
}

//VS2003实现,不支持泛型
public class Node
{
public IComparable val ;
public Node leftChild ;
public Node rightChild ;
}

//遍历模式
public enum TraverseMode
{
PreOrder ,MidOrder ,PostOrder
}

再看整个二叉树的实现:

public class SorttedBinaryTree :ISorttedBinaryTree
{
private Node root = null ;
public SorttedBinaryTree()
{
}

#region ISorttedBinaryTree 接口实现
#region InsertElement
//如果树中有一个节点的值等于val的值,则val将被忽略
public void InsertElement(IComparable val)
{
Node node = new Node() ;
node.val = val ;

if(this.root == null)
{
this.root = node ;
return ;
}

this.DoInsert(node ,this.root) ;
}
#endregion

#region RemoveElement
public void RemoveElement(IComparable val)
{
IAndFather iAndFather = this.SearchElement(val) ;
if(iAndFather == null)
{
return ;
}

this.RemoveRoot(iAndFather) ;

}
#endregion

#region ContainsElement
public bool ContainsElement(IComparable var)
{
IAndFather iAndFather = this.SearchElement(var) ;
return (iAndFather == null) ;
}
#endregion

#region property
public int Depth
{
get
{
return this.GetDepth() ;
}
}

public IComparable MaxElement
{
get
{
if(this.root == null)
{
return null ;
}

Node node = this.root ;

while(node.rightChild != null)
{
node = node.rightChild ;
}

return node.val ;
}
}

public Node Root
{
get
{
return this.root ;
}
}

public IComparable MinElement
{
get
{
if(this.root == null)
{
return null ;
}

Node node = this.root ;

while(node.leftChild != null)
{
node = node.leftChild ;
}

return node.val ;
}
}

public int Count
{
get
{
if(this.root == null)
{
return 0 ;
}

int count = 1 ;
this.CountAllNodes(this.root ,ref count) ;

return count ;
}
}

#endregion

#region GetAllNodesAscend
//非深拷贝 ,外部不得改变得到的各个元素的值
public ArrayList GetAllNodesAscend(bool ascend)
{
ArrayList list = new ArrayList() ;
this.DoGetAllNodes(this.root ,ascend ,ref list ,TraverseMode.MidOrder) ;

return list ;
}

private void DoGetAllNodes(Node childTreeRoot ,bool ascend ,ref ArrayList list ,TraverseMode mode)
{
if(childTreeRoot == null)
{
return ;
}

//中序遍历
if(mode == TraverseMode.MidOrder)
{
if(ascend)
{
this.DoGetAllNodes(childTreeRoot.leftChild ,ascend ,ref list ,mode) ;
list.Add(childTreeRoot) ;
this.DoGetAllNodes(childTreeRoot.rightChild ,ascend ,ref list ,mode) ;
}
else
{
this.DoGetAllNodes(childTreeRoot.rightChild ,ascend ,ref list ,mode) ;
list.Add(childTreeRoot) ;
this.DoGetAllNodes(childTreeRoot.leftChild ,ascend ,ref list ,mode) ;
}
}
else if(mode == TraverseMode.PreOrder)
{
list.Add(childTreeRoot) ;
this.DoGetAllNodes(childTreeRoot.leftChild ,ascend ,ref list ,mode) ;
this.DoGetAllNodes(childTreeRoot.rightChild ,ascend ,ref list ,mode) ;
}
else
{
this.DoGetAllNodes(childTreeRoot.leftChild ,ascend ,ref list ,mode) ;
this.DoGetAllNodes(childTreeRoot.rightChild ,ascend ,ref list ,mode) ;
list.Add(childTreeRoot) ;
}
}
#endregion

#region Traverse
public ArrayList Traverse(TraverseMode mode)
{
ArrayList list = new ArrayList() ;
switch(mode)
{
case TraverseMode.MidOrder :
{
this.DoGetAllNodes(this.root ,true ,ref list ,TraverseMode.MidOrder) ;
return list ;
}
case TraverseMode.PreOrder :
{
this.DoGetAllNodes(this.root ,true ,ref list ,TraverseMode.PreOrder) ;
return list ;
}
case TraverseMode.PostOrder :
{
this.DoGetAllNodes(this.root ,true ,ref list ,TraverseMode.PostOrder) ;
return list ;
}
default:
{
throw new Exception("Error TraverseMode !") ;
}
}
}
#endregion
#endregion

#region privateMethod

#region DoInsert
private void DoInsert(Node node ,Node childTreeRoot)
{
if(childTreeRoot.val.CompareTo(node.val) == 0)
{
return ;
}

if(childTreeRoot.val.CompareTo(node.val) > 0)
{
if(childTreeRoot.leftChild == null)
{
childTreeRoot.leftChild = node ;
return ;
}

this.DoInsert(node ,childTreeRoot.leftChild) ;
return ;
}

if(childTreeRoot.val.CompareTo(node.val) <0)
{
if(childTreeRoot.rightChild == null)
{
childTreeRoot.rightChild = node ;
return ;
}

this.DoInsert(node ,childTreeRoot.rightChild) ;
return ;
}
}
#endregion

#region SearchElement
private IAndFather SearchElement(IComparable val)
{
if(this.root == null)
{
return null ;
}

IAndFather iAndFather = new IAndFather() ;
if(val.CompareTo(this.root.val) == 0)
{
iAndFather.son = this.root ;
return iAndFather ;
}

iAndFather.father = this.root ;
this.DoSearch(val ,this.root ,iAndFather) ;
if(iAndFather.son != null)
{
return iAndFather ;
}

return null ;
}
#endregion

#region DoSearch
private void DoSearch(IComparable val ,Node childTreeRoot ,IAndFather iAndFather)
{
if(childTreeRoot == null)
{
return ;
}

if(val == childTreeRoot.val)
{
iAndFather.son = childTreeRoot ;
return ;
}

iAndFather.father = childTreeRoot ;
if(val.CompareTo(childTreeRoot.val) >0)
{
iAndFather.isLeftChild = false ;
this.DoSearch(val ,childTreeRoot.rightChild ,iAndFather) ;
}
else
{
iAndFather.isLeftChild = true ;
this.DoSearch(val ,childTreeRoot.leftChild ,iAndFather) ;
}
}
#endregion

#region RemoveRoot
private void RemoveRoot(IAndFather childTreeRootAndFather)
{
if(childTreeRootAndFather.son == null)
{
return ;
}

bool isRootDeleted = (childTreeRootAndFather.father == null) ;

//如果删除的为叶子节点
if((childTreeRootAndFather.son.leftChild == null) && (childTreeRootAndFather.son.rightChild ==null))
{
//删除根节点
if(isRootDeleted)
{
this.root = null ;
return ;
}

if(childTreeRootAndFather.isLeftChild)
{
childTreeRootAndFather.father.leftChild = null ;
}
else
{
childTreeRootAndFather.father.rightChild = null ;
}
return ;
}

//要删除的节点有一个子树
if((childTreeRootAndFather.son.leftChild == null) || (childTreeRootAndFather.son.rightChild ==null))
{
//删除根节点
if(isRootDeleted)
{
this.root = (childTreeRootAndFather.son.leftChild == null ? childTreeRootAndFather.son.rightChild :childTreeRootAndFather.son.leftChild) ;
return ;
}

if(childTreeRootAndFather.isLeftChild)
{
childTreeRootAndFather.father.leftChild = (childTreeRootAndFather.son.leftChild == null ? childTreeRootAndFather.son.rightChild :childTreeRootAndFather.son.leftChild) ;
}
else
{
childTreeRootAndFather.father.rightChild = (childTreeRootAndFather.son.leftChild == null ? childTreeRootAndFather.son.rightChild :childTreeRootAndFather.son.leftChild) ;
}

return ;
}

//左右子树均不为空
if((childTreeRootAndFather.son.leftChild != null) && (childTreeRootAndFather.son.rightChild !=null))
{
//删除根节点
if(isRootDeleted)
{
childTreeRootAndFather.father = new Node() ;
childTreeRootAndFather.isLeftChild = true ;
}

//要删除节点的右子节点没有左子树
if(childTreeRootAndFather.son.rightChild.leftChild == null)
{
if(childTreeRootAndFather.isLeftChild)
{
childTreeRootAndFather.father.leftChild = childTreeRootAndFather.son.rightChild ;
}
else
{
childTreeRootAndFather.father.rightChild = childTreeRootAndFather.son.rightChild ;
}
}
else
{
//寻找右子树的最小节点
IAndFather dest = new IAndFather() ;
dest.father = childTreeRootAndFather.son.rightChild ;
dest.son = childTreeRootAndFather.son.rightChild.leftChild ;
dest.isLeftChild = true ;

while(dest.son.leftChild != null)
{
dest.father = dest.son ;
dest.son = dest.son.leftChild ;
dest.isLeftChild = true ;
}

if(childTreeRootAndFather.isLeftChild)
{
childTreeRootAndFather.father.leftChild = dest.son ;
}
else
{
childTreeRootAndFather.father.rightChild = dest.son ;
}

dest.father.leftChild = null ;
}

//如果删除根节点
if(isRootDeleted)
{
this.root = childTreeRootAndFather.father.leftChild ;
}

return ;
}
}
#endregion

#region GetDepth
private int GetDepth()
{
ArrayList list = this.GetAllNodesAscend(true) ;
if(list == null)
{
return 0 ;
}

ArrayList depthList = new ArrayList() ;
foreach(Node node in list)
{
if(this.IsLeaf(node))
{
depthList.Add(this.ComputeDepth(node)) ;
}
}

int depth = (int)depthList[0] ;
foreach(int dep in depthList)
{
if(dep > depth)
{
depth = dep ;
}
}

return depth ;
}

private int ComputeDepth(Node leaf)
{
int count = 1 ;
Node curNode = leaf ;

while(this.GetFather(curNode) != null)
{
++ count ;
curNode = this.GetFather(curNode) ;
}

return count ;
}
#endregion

#region GetFather
private Node GetFather(Node node)
{
if(node == this.root)
{
return null ;
}

ArrayList list = this.GetAllNodesAscend(true) ;
if(list == null)
{
return null ;
}

foreach(Node tar in list)
{
if((tar.leftChild == node) ||(tar.rightChild == node))
{
return tar ;
}
}

return null ;
}
#endregion

#region IsLeaf
private bool IsLeaf(Node node)
{
if((node.leftChild == null) && (node.rightChild == null))
{
return true ;
}

return false ;
}
#endregion

#region CountAllNodes
private void CountAllNodes(Node childTreeRoot ,ref int count)
{
if(childTreeRoot == null)
{
return ;
}

++ count ;

this.CountAllNodes(childTreeRoot.leftChild ,ref count) ;
this.CountAllNodes(childTreeRoot.rightChild ,ref count) ;
}
#endregion

#endregion
}

public class IAndFather
{
public Node father ;
public Node son ;
public bool isLeftChild ;
}


麒麟区14733634955: C#进行二叉树排序的代码是什么? -
芒菡达贝: public interface ISorttedBinaryTree

麒麟区14733634955: 怎么用C#实现二叉树 -
芒菡达贝: 提示你下:二叉树有个特点,就是可以线性排列.比如 1 2 3 4 5 6 78 9 101112131415...可以表示为 1 2 3 4 5 6 7 8 9 101112131415.每个节点的上级节点是x/2,下级节点是x*2和x*2+1.

麒麟区14733634955: 求用C#写二叉排序树的查找和插入的代码 -
芒菡达贝: using System; using System.Collections.Generic; using System.Text; namespace structure {/// <summary> /// 二叉遍历算法解决方案 /// </summary> class Program {//二叉树结点数据结构的定义 二叉树结点数据结构的定义 //二叉树结点数据结构包...

麒麟区14733634955: C++二叉树排序代码 最好是从大到小排列的 谢谢了 -
芒菡达贝: #include "stdio.h"#include "stdlib.h"#include "string.h"#include "conio.h"#include "malloc.h" typedef struct _student { char number[20]; char name[20]; int score; }Student; typedef struct _node { Student student; _node *lchild, *rchild; }...

麒麟区14733634955: 求c#前中后序遍历二叉树的算法及思想 -
芒菡达贝: 下面简单介绍一下几种算法和思路: 先序遍历: 1. 访问根结点 2. 按先序遍历左子树; 3. 按先序遍历右子树; 4. 例如:遍历已知二叉树结果为:A->B->D->G->H->C->E->F 中序遍历: 1. 按中序遍历左子树; 2. 访问根结点; 3. 按中序遍历右子...

麒麟区14733634955: 二叉树遍历,递归与非递归,前序中序后序遍历,C代码 -
芒菡达贝: #include<stdio.h> #include<stdlib.h> typedef struct bitnode { char data; struct bitnode *lchild,*rchild; }bitnode,*bitree;//二叉树节点类型和节点指针类型 bitree create()//先序创建 { bitree root=NULL; char c; scanf("%c",&c); fflush(stdin); if(c=='#')...

麒麟区14733634955: 二叉排序树的查找(C语言)代码 -
芒菡达贝: #include #include #define INFMT "%d"#define OUTFMT "%d "/* #define NULL 0L */#define BOOL int#define TRUE 1#define FALSE 0#define LEN 10000 typedef int ElemType; typedef struct BSTNode { ElemType data; struct BSTNode *...

麒麟区14733634955: 构建一棵二叉排序树的C程序的设计 -
芒菡达贝: #include "stdafx.h"#include "math.h"#include "stdlib.h"#include "stdio.h"#define MAXSIZE 200 int leaf_num; int node_num; typedef struct tnode { int data; struct tnode *lchild,*rchild; }TNODE; TNODE *creatbt(int T[],int n,int i); //函数声明 ...

麒麟区14733634955: 用C语言实现二叉树的操作 -
芒菡达贝: #include#define STACK_MAX_SIZE 30 #define QUEUE_MAX_SIZE 30 #ifndef elemType typedef char elemType; #endif /************************************************************************/ /* 以下是关于二叉树操作的11个简单算法...

麒麟区14733634955: 跪求二叉树排序的 源程序(一定要C语言的)!!! -
芒菡达贝: 这是个调试程序.你自己也可以验正下.(递归想久了,头都大了) 还有这个排序只是对换数据.不是改变指针.这样结构体大时就不能用了#include <stdio.h>//二叉树排序 #include <stdlib.h> typedef struct btree//结构 { int a; struct btree *L; struct btree *...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网