程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> java-請問樹的垂直結構是怎樣的?

java-請問樹的垂直結構是怎樣的?

編輯:編程解疑
請問樹的垂直結構是怎樣的?

         1
        /    \
       2      3
      / \    / \
     4   5  6   7
             \   \
              8   9 


The output of print this tree vertically will be:
4
2
1 5 6
3 8
7
9 

看不懂這個例子

最佳回答:


 http://stackoverflow.com/questions/20521098/print-a-tree-vertically

>To understand what's same vertical line, we need to define horizontal distances first. If two nodes have the same Horizontal Distance (HD), then they are on same vertical line. The idea of HD is simple. HD for root is 0, a right edge (edge connecting to right subtree) is considered as +1 horizontal distance and a left edge is considered as -1 horizontal distance. For example, in the below tree, HD for Node 4 is at -2, HD for Node 2 is -1, HD for 5 and 6 is 0 and HD for node 7 is +2.

關鍵點是理解HD,垂直距離。

          1(0)
        /    \
       2 (-1)   3(1)
      / \    / \
     4(-2)5(0) 6(0)   7(2)
             \   \
              8(1)   9(3)

然後排序輸出
-2 4
-1 2
0 1 5 6
1 3 8
2 7
3 0
XwinterwinterwinterX
XwinterwinterwinterX
XwinterwinterwinterX
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved