using System;
using 所以;
using System. Collections. Generic;
namespace BST
{ class Program { static void Main ( string [ ] args) { int i; int tmp; List< int > scoreList = new List < int > ( ) ; Tree Data = new Tree ( ) ; Tree Data1 = new Tree ( ) ; Tree Datatmp = new Tree ( ) ; Console. WriteLine ( "1024个数的BST树的中序输出" ) ; for ( i = 0 ; i < 1024 ; i++ ) { Data. Add ( i, i) ; } Data. Print1 ( ) ; Console. WriteLine ( "1024个数的查找的平均查找长度计算" ) ; for ( i = 0 ; i < 1024 ; i++ ) { Data. Search ( i) ; } Data. Printcishu ( ) ; for ( i = 0 ; i < 10 ; i++ ) { Random rd = new Random ( ) ; tmp = rd. Next ( 0 , 100 ) ; scoreList. Add ( tmp) ; Data1. Add ( tmp, tmp) ; } Console. WriteLine ( "生成10个随机数的BST树的中序输出" ) ; Data1. Print1 ( ) ; Console. WriteLine ( "10个随机数的查找的平均查找长度计算" ) ; for ( i = 0 ; i < 10 ; i++ ) { Data1. Search ( scoreList[ i] ) ; } Data1. Printcishu ( ) ; Console. ReadKey ( ) ; } }
}
using System;
using System. Collections. Generic;
namespace 所以
{ class Tree { int cishu = 0 ; public class TreeNode { public int Data; public int number; public TreeNode Left; public TreeNode Right; public TreeNode ( int Dta, int numb) { this . Data = Dta; this . number = numb; this . Left = null ; this . Right = null ; } public TreeNode AddNode ( TreeNode Root, TreeNode NewNode) { if ( Root == null ) { Root = NewNode; return Root; } else if ( Root. Data > NewNode. Data) { Root. Left = AddNode ( Root. Left, NewNode) ; } else { Root. Right = AddNode ( Root. Right, NewNode) ; } return Root; } } private TreeNode Root = null ; public void Add ( int Data, int number) { TreeNode newNodeObj = new TreeNode ( Data, number) ; Root = newNodeObj. AddNode ( Root, newNodeObj) ; } private void Print ( TreeNode Root) { if ( Root == null ) { return ; } else { Console. WriteLine ( Root. number) ; Print ( Root. Left) ; Print ( Root. Right) ; } } public void Print1 ( ) { InOrder ( Root) ; } public void InOrder ( TreeNode Root) { if ( Root == null ) { return ; ; } else { { InOrder ( Root. Left) ; Console. WriteLine ( Root. number) ; InOrder ( Root. Right) ; } } } public void Print2 ( ) { PostOrder ( Root) ; } public void PostOrder ( TreeNode Root) { if ( Root == null ) { return ; } else { PostOrder ( Root. Left) ; PostOrder ( Root. Right) ; Console. WriteLine ( Root. number) ; } } public void Print ( ) { Print ( Root) ; } public void Printcishu ( ) { Console. WriteLine ( "本次查找的平均查找长度如下" ) ; Console. WriteLine ( cishu) ; } public void PrintLevelOrder ( ) { int i; int j; int flag; flag = 0 ; i = 0 ; j = 0 ; List< TreeNode> Data = new List < TreeNode > ( ) ; Data. Add ( Root) ; while ( Data. Count != 0 ) { TreeNode temp = Data[ 0 ] ; Data. RemoveAt ( 0 ) ; Console. WriteLine ( $"编号为{temp.number % 100}的元素优先度为{temp.number / 10}" ) ; if ( temp. Left != null ) { Data. Add ( temp. Left) ; i = i + 1 ; } j = j + 1 ; if ( temp. Right != null ) { Data. Add ( temp. Right) ; i = i + 1 ; } j = j + 1 ; if ( ( i != j) && ( ( i == 6 ) ) ) { flag = 1 ; } } } public void Search ( int FindIt) { SearchIt ( Root, FindIt) ; } private void SearchIt ( TreeNode Root, int FindIt) { cishu = cishu + 1 ; if ( Root == null ) { Console. WriteLine ( "Not Found!!" ) ; return ; } else if ( FindIt == Root. Data) { Console. WriteLine ( "Found!!" ) ; Console. WriteLine ( "该节点中存储的值分别为:" ) ; Console. WriteLine ( Root. Data) ; Console. WriteLine ( Root. number) ; return ; } else if ( FindIt > Root. Data) { SearchIt ( Root. Right, FindIt) ; } else if ( FindIt < Root. Data) { SearchIt ( Root. Left, FindIt) ; } return ; } } }