卑微打工人!漫画编程:Java如何在二叉树中进行添加

![](https://upload-images.jianshu.io/upload_images/15590149-44b52f110186a7c3?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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  {`

61.  `// if bigger traverses to the left`

62.  `return search(theRootNode.rightBNode, keyName);`

63.  `}`

64.  `}`

65.  `}`

67.  `//returns null if no result else returns`

68.  `//the AnyClass object matched with the keyName`

69.  `public  AnyClass searchBST(String keyName)  {`

70.  `BNode temp = search(theBTRootNode, keyName);`

71.  `if  (temp ==  null)  {`

72.  `//noresults found`

73.  `return  null;`

74.  `}  else  {`

75.  `//result found`

76.  `return temp.anyClass;`

77.  `}`

78.  `}`

79.  `}`

**兴趣点**

![](https://upload-images.jianshu.io/upload_images/15590149-5e57eb0650657643?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.  `public  void populateBinTree(List theList)  {`

2.  `//clearing the root as not to append,`

3.  `//if you want to append just remove the below line`

4.  `theBTRootNode =  null;`

5.  `//keeps looping untill reaches the end of the list`

6.  `for(int i =  0;i < theList.size();i++)`



## 一线互联网大厂Java核心面试题库

![image](https://img-blog.csdnimg.cn/img_convert/1b26947acb2fff48053724d28e5bd3a6.png)

`//keeps looping untill reaches the end of the list`

6.  `for(int i =  0;i < theList.size();i++)`



## 一线互联网大厂Java核心面试题库

[外链图片转存中...(img-0Vd8kM2C-1628146529064)]

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等...已整理上传在**我的[腾讯文档【一线互联网大厂Java核心面试题库】点击即可领取](https://gitee.com/vip204888/java-p7)**,并会持续更新...感兴趣的朋友可以看看支持一波!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值