-
Notifications
You must be signed in to change notification settings - Fork 0
/
balanced2dTree.scala
126 lines (87 loc) · 3.21 KB
/
balanced2dTree.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Balanced 2-D tree.
* This is a Scala Program to implement a balanced 2-d tree by finding the median of the data for
* each recursive subdivision of those data.
* <p>
* @author Ahmed Alia <abualia4@najah.edu>
*/
class Node(var value:Array[Int]){
var point:Array[Int]=new Array[Int](2)
for(i<-0 to 1)
point(i)=value(i)
var left:Node=null
var right:Node=null
}
import util.control.Breaks._
class DBTree{
var root:Node=null
/**
* This mehod for creating balanced 2-D tree using the
* median strategy .
* @param points
* list of the 2d points
* @param depth
* of the node
*/
def buildKDTree( points: List[(Int,Int)], depth: Int ):Node ={
if(points.length==0 || points==null)
return null
println("depth"+depth)
var sortedPoints:List[(Int,Int)] =List()
var cd = depth%2
if(cd == 0){
sortedPoints=points.sortBy(x=>(x._1))
}
else{
sortedPoints=points.sortBy(x=>(x._2))
}
var node:Node=null
var leftSubtree: List[(Int,Int)]=List()
var rightSubtree: List[(Int,Int)]=List()
if(points.length>0){
var medianIndex:Int=(sortedPoints.length)/2
var arr:Array[Int]=Array(sortedPoints(medianIndex)._1,sortedPoints(medianIndex)._2)
node=new Node(arr)
leftSubtree =sortedPoints.take(medianIndex)
rightSubtree =sortedPoints.takeRight(sortedPoints.length-(medianIndex+1))
println(node.value(0)+","+node.value(1))
println(sortedPoints)
println(leftSubtree)
println(rightSubtree)
if(medianIndex-1>=0 &&leftSubtree.length>0){
node.left= buildKDTree(leftSubtree,depth+1)
}
if(medianIndex-1<sortedPoints.length &&rightSubtree.length>0){
node.right= buildKDTree(rightSubtree,depth+1)
}
} //end of if(points.length>0)
return node
}
/**
* This mehod for printing the content of a binary tree,
* (Root, left subtree and right subtree)
*/
def printTree(node: Node, start:Int):Unit={
if(node!=null) {
if(node.value(0)==start)
println("Root: "+node.value(0)+"," +node.value(1))
else if(node.value(0)<start)
println("Left subtree: " +node.value(0)+"," +node.value(1))
else
println("Right subtree: " +node.value(0)+"," +node.value(1) )
printTree(node.left,start)
printTree(node.right,start)
}
}
}
//Main part
object test{
def main(args:Array[String]){
val points = List((1, 7),(19,22),(4, 9), (12, 4),(5, 13),(7, 20),(14, 35),(15,39))
val dt:DBTree=new DBTree()
val bt= dt.buildKDTree(points,0)
println(bt.value(0))
dt.printTree(bt, bt.value(0) )
}
}
test.main(Array("test"))