Java程序设计基础知识总结,漫画编程:Java如何在二叉树中进行添加

使用代码

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background: rgb(27, 25, 24); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, &quot;Microsoft Yahei&quot; !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

1.  `public  class  BNode  {`

3.  `public  BNode leftBNode, rightBNode;  // the nodes`

4.  `public  AnyClass anyClass;  //the AnyClass objext`

6.  `public  BNode(AnyClass anyClass )  {//constructor`

7.  `this.anyClass= anyClass;`

8.  `this.leftBNode =  null;`

9.  `this.rightBNode =  null;`

10.  `}`

12.  `public  void show()  {`

13.  `//calls the show method of the AnyClass`

14.  `System.out.print(anyClass.show());`

15.  `}`

16.  `}`

</pre>

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background: rgb(27, 25, 24); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, &quot;Microsoft Yahei&quot; !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

1.  `public  class  BinTree  {`

2.  `BNode theBTRootNode;`

4.  `public  BinTree()  // constructor`

5.  `{`

6.  `theBTRootNode =  null;`

7.  `}`

9.  `// ------------------ Addition of the node to the BST-------------------`

10.  `protected  BNode insertAB(BNode theRootNode,  BNode myNewNode)  {`

11.  `if  (theRootNode ==  null)  {`

12.  `theRootNode = myNewNode;`

13.  `//checks if the username is smaller than`

14.  `//the root object, if smaller appends to the left`

15.  `}  else  if  (myNewNode.anyClass.surname.compareTo(`

16.  `theRootNode.anyClass.surname)  <  0)  {`

17.  `theRootNode.leftBNode = insertAB(theRootNode.leftBNode, myNewNode);`

18.  `}  else  {`

19.  `// else if bigger appends to the right`

20.  `theRootNode.rightBNode =` 

21.  `insertAB(theRootNode.rightBNode, myNewNode);`

22.  `}`

23.  `return theRootNode;`

24.  `}`

26.  `public  void insertBST(AnyClass anyClass)  {`

27.  `BNode anyClassBTNode =  new  BNode(anyClass);`

28.  `//calls insert above`

29.  `theBTRootNode = insertAB(theBTRootNode, anyClassBTNode);`

30.  `}`

32.  `// ------------------ InOrder traversal-------------------`

33.  `protected  void inorder(BNode theRootNode)  {`

34.  `if  (theRootNode !=  null)  {`

35.  `inorder(theRootNode.leftBNode);`

36.  `theRootNode.show();`

37.  `inorder(theRootNode.rightBNode);`

38.  `}`

39.  `}`

41.  `//calls the method to do in order`

42.  `public  void inorderBST()  {`

43.  `inorder(theBTRootNode);`

44.  `}`

46.  `// ----- Search for key name and  returns ref.`

47.  `//              to BNode or null if not found--------`

48.  `protected  BNode search(BNode theRootNode,  String keyName)  {`

49.  `//if the root is null returns null`

50.  `if  (theRootNode ==  null)  {`

51.  `return  null;`

52.  `}  else  {`

53.  `//checks if they are equal`

54.  `if  (keyName.compareTo(theRootNode.anyClass.surname)  ==  0)  {`

55.  `return theRootNode;`

56.  `//checks id the key is smaller than the current`

57.  `//record  if smaller traverses to the left`

58.  `}  else  if  (keyName.compareTo(theRootNode.anyClass.surname)  <  0)  {`

59.  `return search(theRootNode.leftBNode, keyName);`

60.  `}  else  {`


# **读者福利**

分享一份自己整理好的Java面试手册,还有一些面试题pdf

**[领取方式;关注+点赞后,戳这里即可免费领取](https://gitee.com/vip204888/java-p7)**

**不要停下自己学习的脚步**

![字节跳动的面试分享,为了拿下这个offer鬼知道我经历了什么](https://img-blog.csdnimg.cn/img_convert/1deb6982cda9b9156df6768334aacce6.png)

![字节跳动的面试分享,为了拿下这个offer鬼知道我经历了什么](https://img-blog.csdnimg.cn/img_convert/50b7f5649125b4ed361712cab10cccd6.png)

后,戳这里即可免费领取](https://gitee.com/vip204888/java-p7)**

**不要停下自己学习的脚步**

[外链图片转存中...(img-GA7hV0e7-1628288872176)]

[外链图片转存中...(img-BNK6Udr2-1628288872177)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值