Presentation is loading. Please wait.

Presentation is loading. Please wait.

Traverse this binary search tree using:

Similar presentations


Presentation on theme: "Traverse this binary search tree using:"— Presentation transcript:

1 Traverse this binary search tree using:
preorder traversal. in order traversal, postorder traversal.

2 Secret technique for doing traversals by hand:

3

4

5 Announcements: Assignment #1 solutions have been posted. I added C/C++ code after class yesterday. No office hours on Friday Oct. 2. Bring your programs from assignment #1 as/is to lab next Monday (working or not) for attendance bonus marks.

6 Analysis of binary tree sort.
CSC 225 Lecture 11: Analysis of binary tree sort. How much time does the binary tree sort take to sort: in the best case? in the worst case?

7 Best case: Assume n= 2h+1 – 1 and the resulting tree is a complete binary tree:
3 4

8 From Lecture 8: n h 1 3 7 2 15 2h+1 - 1 The height h of a complete binary tree on n nodes is: h= log2(n + 1) – 1

9 public void addTree(int value)
{ if (value <= data) if (leftChild == null) leftChild= new BinaryTreeNode(value); else leftChild.addTree(value); } if (rightChild == null) rightChild= new BinaryTreeNode(value); rightChild.addTree(value);

10 Since each call to addTree takes only O(1) time, the time that it takes to build the tree of this shape is: T(n) = * * * 4 + … + (h+1) * 2 h where n= 2h+1 – 1. Using our standard techniques to bound a sum, what do we get? The time is θ(n log2(n)).

11 On the average, the height of the tree is roughly 2.5 * log2(n).
This means that the average time is also in θ( n log2 (n)). We won’t prove this.

12 Worst Case Time Complexity

13 T(n) = n + T(n-1), T(1) = 1. T(n) is in Θ(n2).

14 public static BinaryTreeNode readTree(Scanner in)
{ BinaryTreeNode root; int i, n, value; root=null; n= readInteger(in); if (n > 0) { for (i=0; i < n; i++) { value= readInteger(in); if (i==0) root= new BinaryTreeNode(value); else root.addTree(value); } return(root); Error detection code removed.

15 public static void main(String args[])
{ BinaryTreeNode root; Scanner in = new Scanner(System.in); root= readTree(in); while (root != null) System.out.println("The sorted data"); root.inOrder(); System.out.println(); }

16 In summary: How much time does the binary tree sort take to sort: in the best case? Θ ( n log (n) ) in the worst case? Θ ( n2) 3. on an average case? Θ ( n log (n) ) [No proof given]

17 Give examples of input which can be provided for arbitrary n which realize the:
best case behaviour? Sort the data in an array, make a complete binary tree from sorted data, then get input for the best case with a level order traversal.

18 2. Worst case behaviour: Sorted data.

19 How much extra space does it use:
in the best case? Θ(n) in the worst case? Θ(n) Justification: InOrder and AddTree use O(1) space per call, and the worst case depth of a call is n. So the amount of extra space due to the recursive calls is in O(n). Each data item has two pointers associated with its tree node and the space for these is in Θ(n).

20 Space used: this, return address. public class BinaryTreeNode {
int data; BinaryTreeNode leftChild; BinaryTreeNode rightChild public void inOrder() if (leftChild!= null) leftChild.inOrder(); System.out.print(data + " "); if (rightChild!= null) rightChild.inOrder(); } Space used: this, return address.

21 public void addTree(int value) Implicit: this, return address.
{ if (value <= data) if (leftChild == null) leftChild= new BinaryTreeNode(value); else leftChild.addTree(value); } if (rightChild == null) rightChild= new BinaryTreeNode(value); rightChild.addTree(value); Implicit: this, return address.


Download ppt "Traverse this binary search tree using:"

Similar presentations


Ads by Google