数组二叉树java_关于Java:从数组创建二叉树

在通过以下算法处理元素之后,我需要从数组创建树:

1. let A[n] be the array

2. while lenght(A) > 1

3.      for i = 0 to lenght(A)-2

4.          new_A[i] = max(A[i], A[i+1]) + 1

5.      end for

6.      [minValue, minIndex] = someFunction(new_A) // someFunction returns the minimum value and index of the same

7.      delete A[minIndex] and A[minIndex + 1] from A and insert minValue in that place // basically A[minIndex] and A[minIndex + 1] are being replaced by minValue

8.  // This is how the length of A is being decreased by 1 in each iteration

9. end while

例:

+--------------+----------------------+-----------------+---------------+

| iteration No | Array A              | Array new_A     | Remarks       |

+--------------+----------------------+-----------------+---------------+

|            1 | 5-9-3-2-1-6-8-3      |10-10-4-3-7-9-9  | minValue = 3  |

|              |                      |                 | minIndex = 3  |

+--------------+----------------------+-----------------+---------------+

|            2 | 5-9-3-3-6-8-3        |10-10-4-7-9-9    | minValue = 4  |

|              |                      |                 | minIndex = 2  |

+--------------+----------------------+-----------------+---------------+

|            3 | 5-9-4-6-8-3          |10-10-7-9-9      | minValue = 7  |

|              |                      |                 | minIndex = 2  |

+--------------+----------------------+-----------------+---------------+

|            4 | 5-9-7-8-3            |10-10-9-9        | minValue = 9  |

|              |                      |                 | minIndex = 2  |

+--------------+----------------------+-----------------+---------------+

|            5 | 5-9-9-3              |10-10-10         | minValue = 10 |

|              |                      |                 | minIndex = 0  |

+--------------+----------------------+-----------------+---------------+

|            6 | 10-9-3               |11-10            | minValue = 10 |

|              |                      |                 | minIndex = 1  |

+--------------+----------------------+-----------------+---------------+

|            7 | 10-10                |11               | minValue = 11 |

|              |                      |                 | minIndex = 0  |

+--------------+----------------------+-----------------+---------------+

|            8 | 11                   |--               | --            |

+--------------+----------------------+-----------------+---------------+

到此为止一切都还好。但是我需要用树来表示。结果树将如下所示:

iteration# 8           11

/  \

/    \

iteration# 7        /      \------- 10

/               /  \

iteration# 6     10            

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值