Title: More%20Trees
1More Trees
- Java implementation of trees
- Tree traversal
- When should we use trees
2Binary Tree Class in Java
- public class BinTree // binary search tree
-
- private String value
- private BinTree left, right
- // constructor
- BinTree() // create an empty tree node
-
- value null
- left right null
-
- BinTree(String x)
- // create a leaf node with value x
-
- value x
- left right null
-
A binary tree node
value
left
right
3Binary Tree Class in Java- insertion method
- public void insertInOrder(String input_item)
-
- if(valuenull) // an empty tree
- value input_item
-
- else if(Func.lessThan(input_item, value))
- // insert to left subtree
- if(leftnull)
- left new BinTree(input_item)
- else left.insertInOrder(input_item)
-
- else // insert to right subtree
- if(rightnull)
- right new BinTree(input_item)
- else right.insertInOrder(input_item)
-
-
A binary tree node
Insert 7
4Tree traversal a systematic way of visiting all
nodes of a binary tree
- printLR() // print bin tree from left to right
- if(left ! null) // print left subtree
- left.printLR()
- System.out.print(value ) // do print
- if(right ! null) // print right subtree
- right.printLR()
-
5Tree traversal a systematic way of visiting all
nodes of a binary tree
- printLR()
- if(left ! null)
- left.printLR()
- // do print
- print(value )
- if(right ! null)
- right.printLR()
-
2
5
5
7
10
9
12
6List Class in Java
- public class List
-
- private int current_size
- private final int limit 8192
- private String data new Stringlimit//
array to hold list - private String tmp new Stringlimit //
tmp buffer, need it? - // constructor
- List(int size, String type)
- int i, n
- if(size lt 1) current_size 0 //
create an empty list - else current_size size
- if(type.equals("random"))
- Random Ran new Random(0) // use
seed 0 - for(i0 iltcurrent_size i) //
create a random list - n (int) (Ran.nextFloat()
limit) - datai Integer.toString(n)
-
-
- else if(type.equals("ordered"))
7List Class in Java ordered insertion
- public int insertInOrder(String input_item)
-
- if(current_size lt limit)
- int i,j
- for(i0i lt current_sizei)
- if(Func.lessThan(input_item, datai))
- break // find the position
-
- j current_size
- while(jgti) // shift every item to
its right - dataj dataj-1
- j--
-
- datai input_item // insert to
the location i - current_size // increase size
by 1 - return i // return inserted position
-
- else
- return -1 // overflow, failed to
insert
8Test Program using random numbers
- public class TreeInsertTestR
- static public void main(String args)
- Random Ran new Random(100)
- int size Integer.parseInt(args0)
- Stopwatch timer new Stopwatch()
- BinTree tree new BinTree() // make an empty
tree - timer.go()
- for(int i0 iltsize i)
- String S Integer.toString((int)(Ran.nextFlo
at()size)) - tree.insertInOrder(S)
-
- long ms timer.stop()
- tree.display_flat()
- System.out.println(
- "constructed a binary search tree by random
input. time spent " - ms " millisecond")
-
-
9Test Program using ordered numbers
- public class TreeInsertTestO
- static public void main(String args)
- // Random Ran new Random(100)
- int size Integer.parseInt(args0)
- Stopwatch timer new Stopwatch()
- BinTree tree new BinTree() // make an empty
tree - timer.go()
- for(int i0 iltsize i)
- String S Integer.toString(i)
- tree.insertInOrder(S)
-
- long ms timer.stop()
- tree.display_flat()
- System.out.println(
- "constructed a binary search tree by ordered
input. time spent " - ms " millisecond")
-
-
10My timing results pentium4 1.6GHz
time in millisecond
size 1000 2000 3000 4000 5000
List-R 189 771 1760 3150 4960
List-O 343 1531 3521 6324 10051
Tree-R 32 47 69 84 98
Tree-O 365 1584 3667 6699 10544
11Balanced and unbalanced trees
The worst case
The best case
height of tree size of tree
height of tree log (size of tree)
12The timing results show
- List insertion costs O(n)
- ? performing n insertions costs O(n2)
- Binary tree insertion costs O(log n)
- (if it is balanced)
- ? performing n insertions costs O(n log n)
- But in the worst case, insertion in an unbalanced
binary tree costs O(n)
13Conclusion
- Only use list structures if insertion/deletion
are not frequently needed - Tree structures are very efficient for accessing
and updating data - Important issue how to make a balanced tree
(ref. UQC108S2)
14The Challenge Question
- A list L contains n-1 unique integers in the
range 0,n-1, that is, there is one number from
this range is not in L. - For example, L (6,3,1,5,7,0,2),
- n8, and 4 is the missing number.
- Design an O(n)-time algorithm for finding that
missing number. You are only allowed to use O(1)
additional memory space besides the list L
itself.
15If O(n) additional memory is allowed
It is easy to have an O(n) algorithm to find out
the missing number. Two phases 1 Read the list
L, one by one, store the number X into an extra
list, at location X, 2. Go through the extra
list to find the missing number But, what if this
extra list is NOT allowed, ?