java 二叉树_漫画编程:Java如何在二叉树中进行添加,检索,删除和搜索

b017fa103bee895fd9632414cc4a1c03.png

介绍

二进制搜索树(BST)是基于节点的二进制树数据结构,具有以下属性:

节点的左子树仅包含键值小于节点键值的节点。节点的右子树仅包含键大于该节点的键的节点。左和右子树都必须也是二进制搜索树。

011be360dfddc14b4d1988f1caa6e6ab.png

使用代码

af085e7c0d469c3040055551cb2cd1bd.png
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.  `}`
8c7c92bb9d1915be8ffff805ac6d04e7.png
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.  `}`

兴趣点

160d0a0c4f900856d5eb98d722f4de13.png
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++)`7.  `Node temporaryNode =  null;` 8.  `//inserts in the BST`9.  `insertBST((AnyClass)theList.get(i));`10.  `//goes to the next element`11.  `}`12.  `}`
817cc2d5b9fa1d1ec70f5124966f9524.png
d7bb2375f087ecb3918d55d66cd2641d.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值