Professional Documents
Culture Documents
Trees: Data Structures and Algorithms in Java 1/33
Trees: Data Structures and Algorithms in Java 1/33
Trees
Part 2
Balancing a tree:
1.Copy all tree nodes
to an array
2.Sort the array
Creating a binary search tree from an ordered array
3.Clear the tree
4.Call balance()
method to rebuild
the tree Data Structures and Algorithms in Java 5/33
Balancing a Tree - Simple Balance Algorithm - 2
6
Right rotation
Left rotation
PARENT (i)
return floor((i-1)/2)
The root of the tree A[0] and LEFT (i)
given index i of a node, the return 2i+1
indices of its parent, left child and RIGHT (i)
return 2(i + 1)
right child can be computed: