一、二叉树介绍
在计算机科学中,二叉树是每个结点最多有两个子树的树结构。二叉树由各种节点组成,如下图所示:
- 每个节点都可以有左子节点,右子节点
- 每一个节点都有一个值
二叉树遍历:
二叉树的遍历分左序,中序,右序
- 左序即: 中间的数遍历后放在左边
- 中序即: 中间的数遍历后放在中间
- 右序即: 中间的数遍历后放在右边
二叉树排序法的思路:
假设通过二叉树对如下10个随机数进行排序
67,7,30,73,10,0,78,81,10,74
第一步:插入数据,插入基本逻辑是,小、相同的放左边,大的放右边
1. 67 放在根节点
2. 7 比 67小,放在67的左节点
3. 30 比67 小,找到67的左节点7,30比7大,就放在7的右节点
4. 73 比67大, 放在67得右节点
5. 10 比 67小,找到67的左节点7,10比7大,找到7的右节点30,10比30小,放在30的左节点。
...
...
9. 10比67小,找到67的左节点7,10比7大,找到7的右节点30,10比30小,找到30的左节点10,10和10一样大,放在左边
插入数据之后的结果如下图所示:
- 第二步:二叉树“中序遍历”,把这些已经排好序的数据,遍历成我们常用的List或者数组的形式。
如图二所见,我们排序淡然希望遍历后的结果是从小到大的,所以应该采用中序遍历
Java源代码如下:
在插入和遍历的过程中都用到了迭代的思想。
package
启动程序,控制台打印的数据,效果如下:
D:javajdkJDK8_64binjava.exe "-javaagent:D:javaidea201903config-pathclassIntelliJ IDEA 2019.3.3libidea_rt.jar=55936:D:javaidea201903config-pathclassIntelliJ IDEA 2019.3.3bin" -Dfile.encoding=UTF-8 -classpath D:javajdkJDK8_64jrelibcharsets.jar;D:javajdkJDK8_64jrelibdeploy.jar;D:javajdkJDK8_64jrelibextaccess-bridge-64.jar;D:javajdkJDK8_64jrelibextcldrdata.jar;D:javajdkJDK8_64jrelibextdnsns.jar;D:javajdkJDK8_64jrelibextjaccess.jar;D:javajdkJDK8_64jrelibextjfxrt.jar;D:javajdkJDK8_64jrelibextlocaledata.jar;D:javajdkJDK8_64jrelibextnashorn.jar;D:javajdkJDK8_64jrelibextsunec.jar;D:javajdkJDK8_64jrelibextsunjce_provider.jar;D:javajdkJDK8_64jrelibextsunmscapi.jar;D:javajdkJDK8_64jrelibextsunpkcs11.jar;D:javajdkJDK8_64jrelibextzipfs.jar;D:javajdkJDK8_64jrelibjavaws.jar;D:javajdkJDK8_64jrelibjce.jar;D:javajdkJDK8_64jrelibjfr.jar;D:javajdkJDK8_64jrelibjfxswt.jar;D:javajdkJDK8_64jrelibjsse.jar;D:javajdkJDK8_64jrelibmanagement-agent.jar;D:javajdkJDK8_64jrelibplugin.jar;D:javajdkJDK8_64jrelibresources.jar;D:javajdkJDK8_64jrelibrt.jar;D:Mapducetargetclasses;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-common3.2.0hadoop-common-3.2.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-annotations3.2.0hadoop-annotations-3.2.0.jar;D:javaUTIL-PackagesmavenBigDatacomgoogleguavaguava11.0.2guava-11.0.2.jar;D:javaUTIL-PackagesmavenBigDatacommons-clicommons-cli1.2commons-cli-1.2.jar;D:javaUTIL-PackagesmavenBigDataorgapachecommonscommons-math33.1.1commons-math3-3.1.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachehttpcomponentshttpclient4.5.2httpclient-4.5.2.jar;D:javaUTIL-PackagesmavenBigDataorgapachehttpcomponentshttpcore4.4.4httpcore-4.4.4.jar;D:javaUTIL-PackagesmavenBigDatacommons-codeccommons-codec1.11commons-codec-1.11.jar;D:javaUTIL-PackagesmavenBigDatacommons-iocommons-io2.5commons-io-2.5.jar;D:javaUTIL-PackagesmavenBigDatacommons-netcommons-net3.6commons-net-3.6.jar;D:javaUTIL-PackagesmavenBigDatacommons-collectionscommons-collections3.2.2commons-collections-3.2.2.jar;D:javaUTIL-PackagesmavenBigDatajavaxservletjavax.servlet-api3.1.0javax.servlet-api-3.1.0.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-server9.3.24.v20180605jetty-server-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-http9.3.24.v20180605jetty-http-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-io9.3.24.v20180605jetty-io-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-util9.3.24.v20180605jetty-util-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-servlet9.3.24.v20180605jetty-servlet-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-security9.3.24.v20180605jetty-security-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-webapp9.3.24.v20180605jetty-webapp-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-xml9.3.24.v20180605jetty-xml-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDatajavaxservletjspjsp-api2.1jsp-api-2.1.jar;D:javaUTIL-PackagesmavenBigDatacomsunjerseyjersey-core1.19jersey-core-1.19.jar;D:javaUTIL-PackagesmavenBigDatajavaxwsrsjsr311-api1.1.1jsr311-api-1.1.1.jar;D:javaUTIL-PackagesmavenBigDatacomsunjerseyjersey-servlet1.19jersey-servlet-1.19.jar;D:javaUTIL-PackagesmavenBigDatacomsunjerseyjersey-json1.19jersey-json-1.19.jar;D:javaUTIL-PackagesmavenBigDataorgcodehausjettisonjettison1.1jettison-1.1.jar;D:javaUTIL-PackagesmavenBigDatacomsunxmlbindjaxb-impl2.2.3-1jaxb-impl-2.2.3-1.jar;D:javaUTIL-PackagesmavenBigDataorgcodehausjacksonjackson-core-asl1.9.2jackson-core-asl-1.9.2.jar;D:javaUTIL-PackagesmavenBigDataorgcodehausjacksonjackson-mapper-asl1.9.2jackson-mapper-asl-1.9.2.jar;D:javaUTIL-PackagesmavenBigDataorgcodehausjacksonjackson-jaxrs1.9.2jackson-jaxrs-1.9.2.jar;D:javaUTIL-PackagesmavenBigDataorgcodehausjacksonjackson-xc1.9.2jackson-xc-1.9.2.jar;D:javaUTIL-PackagesmavenBigDatacomsunjerseyjersey-server1.19jersey-server-1.19.jar;D:javaUTIL-PackagesmavenBigDatacommons-loggingcommons-logging1.1.3commons-logging-1.1.3.jar;D:javaUTIL-PackagesmavenBigDatalog4jlog4j1.2.17log4j-1.2.17.jar;D:javaUTIL-PackagesmavenBigDatacommons-beanutilscommons-beanutils1.9.3commons-beanutils-1.9.3.jar;D:javaUTIL-PackagesmavenBigDataorgapachecommonscommons-configuration22.1.1commons-configuration2-2.1.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachecommonscommons-lang33.7commons-lang3-3.7.jar;D:javaUTIL-PackagesmavenBigDataorgapachecommonscommons-text1.4commons-text-1.4.jar;D:javaUTIL-PackagesmavenBigDataorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;D:javaUTIL-PackagesmavenBigDataorgslf4jslf4j-log4j121.7.25slf4j-log4j12-1.7.25.jar;D:javaUTIL-PackagesmavenBigDataorgapacheavroavro1.7.7avro-1.7.7.jar;D:javaUTIL-PackagesmavenBigDatacomthoughtworksparanamerparanamer2.3paranamer-2.3.jar;D:javaUTIL-PackagesmavenBigDataorgxerialsnappysnappy-java1.0.5snappy-java-1.0.5.jar;D:javaUTIL-PackagesmavenBigDatacomgooglere2jre2j1.1re2j-1.1.jar;D:javaUTIL-PackagesmavenBigDatacomgoogleprotobufprotobuf-java2.5.0protobuf-java-2.5.0.jar;D:javaUTIL-PackagesmavenBigDatacomgooglecodegsongson2.2.4gson-2.2.4.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-auth3.2.0hadoop-auth-3.2.0.jar;D:javaUTIL-PackagesmavenBigDatacomnimbusdsnimbus-jose-jwt4.41.1nimbus-jose-jwt-4.41.1.jar;D:javaUTIL-PackagesmavenBigDatacomgithubstephencjcipjcip-annotations1.0-1jcip-annotations-1.0-1.jar;D:javaUTIL-PackagesmavenBigDatanetminidevjson-smart2.3json-smart-2.3.jar;D:javaUTIL-PackagesmavenBigDatanetminidevaccessors-smart1.2accessors-smart-1.2.jar;D:javaUTIL-PackagesmavenBigDataorgow2asmasm5.0.4asm-5.0.4.jar;D:javaUTIL-PackagesmavenBigDataorgapachecuratorcurator-framework2.12.0curator-framework-2.12.0.jar;D:javaUTIL-PackagesmavenBigDatacomjcraftjsch0.1.54jsch-0.1.54.jar;D:javaUTIL-PackagesmavenBigDataorgapachecuratorcurator-client2.12.0curator-client-2.12.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachecuratorcurator-recipes2.12.0curator-recipes-2.12.0.jar;D:javaUTIL-PackagesmavenBigDatacomgooglecodefindbugsjsr3053.0.0jsr305-3.0.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachehtracehtrace-core44.1.0-incubatinghtrace-core4-4.1.0-incubating.jar;D:javaUTIL-PackagesmavenBigDataorgapachezookeeperzookeeper3.4.13zookeeper-3.4.13.jar;D:javaUTIL-PackagesmavenBigDatajlinejline0.9.94jline-0.9.94.jar;D:javaUTIL-PackagesmavenBigDataorgapacheyetusaudience-annotations0.5.0audience-annotations-0.5.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachecommonscommons-compress1.4.1commons-compress-1.4.1.jar;D:javaUTIL-PackagesmavenBigDataorgtukaanixz1.0xz-1.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-simplekdc1.0.1kerb-simplekdc-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-client1.0.1kerb-client-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerby-config1.0.1kerby-config-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-core1.0.1kerb-core-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerby-pkix1.0.1kerby-pkix-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerby-asn11.0.1kerby-asn1-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerby-util1.0.1kerby-util-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-common1.0.1kerb-common-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-crypto1.0.1kerb-crypto-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-util1.0.1kerb-util-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbytoken-provider1.0.1token-provider-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-admin1.0.1kerb-admin-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-server1.0.1kerb-server-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerb-identity1.0.1kerb-identity-1.0.1.jar;D:javaUTIL-PackagesmavenBigDataorgapachekerbykerby-xdr1.0.1kerby-xdr-1.0.1.jar;D:javaUTIL-PackagesmavenBigDatacomfasterxmljacksoncorejackson-databind2.9.5jackson-databind-2.9.5.jar;D:javaUTIL-PackagesmavenBigDatacomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;D:javaUTIL-PackagesmavenBigDatacomfasterxmljacksoncorejackson-core2.9.5jackson-core-2.9.5.jar;D:javaUTIL-PackagesmavenBigDataorgcodehauswoodstoxstax2-api3.1.4stax2-api-3.1.4.jar;D:javaUTIL-PackagesmavenBigDatacomfasterxmlwoodstoxwoodstox-core5.0.3woodstox-core-5.0.3.jar;D:javaUTIL-PackagesmavenBigDatadnsjavadnsjava2.1.7dnsjava-2.1.7.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-hdfs3.2.0hadoop-hdfs-3.2.0.jar;D:javaUTIL-PackagesmavenBigDataorgeclipsejettyjetty-util-ajax9.3.24.v20180605jetty-util-ajax-9.3.24.v20180605.jar;D:javaUTIL-PackagesmavenBigDatacommons-daemoncommons-daemon1.0.13commons-daemon-1.0.13.jar;D:javaUTIL-PackagesmavenBigDataionettynetty3.10.5.Finalnetty-3.10.5.Final.jar;D:javaUTIL-PackagesmavenBigDataionettynetty-all4.0.52.Finalnetty-all-4.0.52.Final.jar;D:javaUTIL-PackagesmavenBigDataorgfusesourceleveldbjnileveldbjni-all1.8leveldbjni-all-1.8.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-mapreduce-client-core3.2.0hadoop-mapreduce-client-core-3.2.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-yarn-client3.2.0hadoop-yarn-client-3.2.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-yarn-api3.2.0hadoop-yarn-api-3.2.0.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-yarn-common3.2.0hadoop-yarn-common-3.2.0.jar;D:javaUTIL-PackagesmavenBigDatajavaxxmlbindjaxb-api2.2.11jaxb-api-2.2.11.jar;D:javaUTIL-PackagesmavenBigDatacomsunjerseyjersey-client1.19jersey-client-1.19.jar;D:javaUTIL-PackagesmavenBigDatacomgoogleinjectguice4.0guice-4.0.jar;D:javaUTIL-PackagesmavenBigDatajavaxinjectjavax.inject1javax.inject-1.jar;D:javaUTIL-PackagesmavenBigDataaopallianceaopalliance1.0aopalliance-1.0.jar;D:javaUTIL-PackagesmavenBigDatacomsunjerseycontribsjersey-guice1.19jersey-guice-1.19.jar;D:javaUTIL-PackagesmavenBigDatacomfasterxmljacksonmodulejackson-module-jaxb-annotations2.9.5jackson-module-jaxb-annotations-2.9.5.jar;D:javaUTIL-PackagesmavenBigDatacomfasterxmljacksonjaxrsjackson-jaxrs-json-provider2.9.5jackson-jaxrs-json-provider-2.9.5.jar;D:javaUTIL-PackagesmavenBigDatacomfasterxmljacksonjaxrsjackson-jaxrs-base2.9.5jackson-jaxrs-base-2.9.5.jar;D:javaUTIL-PackagesmavenBigDataorgapachehadoophadoop-hdfs-client3.2.0hadoop-hdfs-client-3.2.0.jar;D:javaUTIL-PackagesmavenBigDatacomsquareupokhttpokhttp2.7.5okhttp-2.7.5.jar;D:javaUTIL-PackagesmavenBigDatacomsquareupokiookio1.6.0okio-1.6.0.jar;D:javaUTIL-PackagesmavenBigDatacomgoogleinjectextensionsguice-servlet4.0guice-servlet-4.0.jar com.bigdata.hadoop.mapreduce.second.Node
数据插入二叉树并排序完成,接下来完成遍历输出:
[0, 7, 10, 10, 30, 67, 73, 74, 78, 81]Process finished with exit code 0