Ali will ask the interview, handwriting Java binary tree

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_39662660/article/details/100563202

Ali will ask the interview, handwriting Java binary tree

 

Ali Interview

Now many companies in the recruitment development positions, that they will indicate in advance the interviewer should have knowledge and skills in jobs, but also in the interview process, some skills for mastery of the strict requirements of the company will also require the interviewer handwriting Code, this link is the test of basic skills and strength interviewer!

This does not, the other day when a friend went to Ali's interview, in the course of two faces will be asked to use Java to achieve binary tree, the king Dog In the absence of this knowledge to prepare, did not answer it, and then let go home and informed .

So have the opportunity to take advantage of the king, Dog explain binary tree, I combed the whole common interview binary point at issue for everyone to exchange study. I want to help your interview.

Binary Tree

It is a recursive binary tree data structure, where each node can have up to two child nodes.

A common type of binary search tree is a binary tree, each node of which the value is greater than or equal to the value of the left child node, a node value and less than or equal to the right child node.

This is a visual representation of this binary tree:

Ali will ask the interview, handwriting Java binary tree

 

For the implementation, we will use the  Node  class to store int value and saves a reference to each child node:

Ali will ask the interview, handwriting Java binary tree

 

Then, let's add the root of the tree, commonly known as  root :

Ali will ask the interview, handwriting Java binary tree

 

Let us work together to achieve lower

Now, let's look at the most common operations that can be performed on the binary tree What?

Insert elements

The first action we want to introduce new node is inserted.

First, we must find where we want to add new nodes to the tree sort. We start from the root to follow these rules:

  • If the value of the new node is lower than the value of the current node, we go to the left child
  • If the value of the new node is greater than the value of the current node, we will go to the right child
  • 节点当前为 null 时,我们已到达叶节点,我们可以在该位置插入新节点

首先,我们将创建一个递归方法来进行插入:

Ali will ask the interview, handwriting Java binary tree

 

接下来,我们将创建一个递归方法来创建根节点:

Ali will ask the interview, handwriting Java binary tree

 

现在让我们看看如何使用此方法从我们的示例中创建树:

Ali will ask the interview, handwriting Java binary tree

 

查找元素

现在让我们添加一个方法来检查树是否包含特定值。

和以前一样,我们首先创建一个遍历树的递归方法:

Ali will ask the interview, handwriting Java binary tree

 

在这里,我们通过将其与当前节点中的值进行比较来搜索该值,然后根据该值继续在左或右子节点中继续查找。

接下来,我们让创建一个公共方法来查找:

Ali will ask the interview, handwriting Java binary tree

 

现在,让我们创建一个简单的测试来验证树真的包含插入的元素:

Ali will ask the interview, handwriting Java binary tree

 

删除元素

另一种常见操作是从树中删除节点。

首先,我们必须以与之前类似的方式找到要删除的节点:

Ali will ask the interview, handwriting Java binary tree

 

一旦我们找到要删除的节点,就有 3 种主要的不同情况:

  • 节点没有子节点 - 这是最简单的情况; 我们只需要在其父节点中用 null 替换此节点
  • 节点只有一个子节点 - 在父节点中,我们用它唯一的子节点替换该节点。
  • 节点有两个子节点 - 这是最复杂的情​​况,因为它需要树重组

让我们看看当节点是叶节点时我们如何实现第一种情况:

Ali will ask the interview, handwriting Java binary tree

 

现在让我们继续讨论节点有一个子节点的情况:

Ali will ask the interview, handwriting Java binary tree

 

在这里,我们返回 非 null 子节点,以便将其分配给父节点。

最后,我们必须处理节点有两个子节点的情况。Java架构社区

首先,我们需要找到将替换已删除节点的节点。我们将使用节点的最小节点删除右侧子树:

Ali will ask the interview, handwriting Java binary tree

 

然后,我们将最小值分配给要删除的节点,之后,我们将从右侧子树中删除它:

Ali will ask the interview, handwriting Java binary tree

 

最后,我们让创建删除的公共方法:

Ali will ask the interview, handwriting Java binary tree

 

现在,让我们检查删除是否按预期工作:

Ali will ask the interview, handwriting Java binary tree

 

转换树

在此,我们将看到遍历树的不同方式,详细介绍深度优先和广度优先搜索。

我们将使用之前使用的相同树,并且我们将显示每个案例的遍历顺序。

深度优先搜索

深度优先搜索是一种在每个子节点探索下一个兄弟之前尽可能深入的遍历。

There are several ways to do depth-first search: in-order, pre-order and post-order.

in-order: First, visit the left subtree, then visit the root, right subtree last visit:

Ali will ask the interview, handwriting Java binary tree

 

If we call this method, the console output:

Ali will ask the interview, handwriting Java binary tree

 

pre-order: First, access the root node, then the left subtree, and finally the right subtree:

Ali will ask the interview, handwriting Java binary tree

 

If we call this method, the console output:

Ali will ask the interview, handwriting Java binary tree

 

post-order: visit the left subtree, right subtree, root last visit:

Ali will ask the interview, handwriting Java binary tree

 

If we call this method, the console output:

3 5 4 7 9 8 6

BFS

This is another common type of traversal, it shows all the nodes access levels before entering the next level.

This is also referred to by level traversal order and start from the root, from left to right to access all levels of the tree.

For the implementation, we will use the  queue  in order to save each level node. We extract each node from the list, print its value, and then add its child nodes to the queue:

Ali will ask the interview, handwriting Java binary tree

 

In this case, the order of the nodes will be:

6 4 8 3 5 7 9

to sum up

In this article, we have learned how to implement a sorted binary tree and the most common operation in Java. Are you gain from it? Even if you can gain a little experience, in this small series also gratified!

"A short step, a thousand miles" in the hope that you can become the future: a dream horse habitat everywhere! Come on, Junior!

Interview welfare

Ali will ask the interview, handwriting Java binary tree

 

Ali will ask the interview, handwriting Java binary tree

Plus Java architecture community : receive data, which will share some video recordings senior architect: There are Spring, MyBatis, Netty source code analysis, the principle of high concurrency, high performance, distributed micro-service architecture, JVM performance optimization these become architecture division necessary information

Guess you like

Origin blog.csdn.net/qq_39662660/article/details/100563202