Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?

Similar presentations


Presentation on theme: "Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?"— Presentation transcript:

1 Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?

2 Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?  Take the 2 nd glass & pour it into the 5 th glass!

3 CSC 213 – Large Scale Programming

4 “Node” Sounds Technical…  Links in linked list commonly called “nodes”  Nodes name for links in a T REE also  Position interface implemented by the node class  Each node’s data retrieved using element()  Node class often used by data structures  Node is not specific, but sounds better than “thingy”  Position interface implemented by node (usually)

5 Node Heights  Longest distance to leaf node  D's height is 0  B's height is 2  A's height is 3 A B D C G H E F I J K

6 Node Heights  Height 1 more than taller child  F's height is 1  B's height is 2  E's height is 0  Leaf's height always 0 A B D C G H E F I J K

7 Node Depths  Distance to root node of Tree  A's depth is 0  C's depth is 1  I's depth is 3 A B D C G H E F I J K

8 Node Depths  Depth of a node is 1 more than its parent  Root's depth always 0  B, C, & D have depth of 1  E, F, G, H have depth of 2  I, J, K have depth of 3 A B D C G H E F I J K

9 Tree Interface  Interesting methods from the interface Position root() Position parent(p) Iterable > children(p) boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) E replace (p, e)

10 Tree Interface  Often include other methods beyond just these addremove  Normally want methods to add or remove data  Else use root & size fields to start in Tree

11 Node Class For Tree class TNode implements Position { private E element; private TNode parent; private Sequence > kids; // Besides getters & setters, often include methods like: public TNode getChild(int i) { return kids.get(i); } public void addChild(TNode kid) { kids.addLast(kid); } public TNode removeChild(int i) { return kids.remove(i); } }

12 Tree D  Visualization of Tree B D A CE F B AF CE Tree root size  6

13 D  Node Structure of the Tree B D A CE F B AF CE

14 D  Links Between Tree 's Nodes B D A CE F B AF CE

15 D  B D A CE F B AF CE

16 Tree D  Actual View of Tree B D A CE F B AF CE Tree root size  6

17 View of an Actual Tree

18 Linked Node for BinaryTree class BTNode implements Position { private E element; private BTNode parent; private BTNode left; private BTNode right; // Add getters & setters for each field public Iterable > children(){ IndexList > il = new … il.add(0, left); il.add(1, right); return il; } }

19 BinaryTree   Picturing Linked BinaryTree B C A D   BACD BinaryTree root size  4

20   Nodes in Linked BinaryTree B C A D   BACD

21  Pointers in Linked BinaryTree B C A D   B AC D

22 Array-based BinaryTree  Node at index specified for location in T REE  Root node stored at index 0  Root’s left child at index 1  Right child of root at index 2  Left child’s right child at index 4  Right child’s left child at index 5  Node at index n ’s left child is at index 2n + 1  Node at index n ’s right child is at index 2n + 2

23 Array-based Implementation  Tree’s depth limited by array size  Where in array determined by location  Sequence overcomes limits  null added to pad space  replace() used to add nodes

24 Array-based Implementation A HG FE D C B J 01234567891010

25 A HG FE D C B J 01234567891010

26 A HG FE D C B J 01234567891010

27 Node in array-based BinaryTree class BANode implements Position { private E element; private int myIndex; private boolean hasLeft; private boolean hasRight; // Add getters & setters for each field public int getLeft() { if (!hasLeft) { return -1; } return (myIndex * 2) + 1; } public void setRight() { hasRight = true; } }

28 Your Turn  Get into your groups and complete activity

29 For Next Lecture  Read GT 8.1 – 8.1.2 for Friday's lecture  What is an Entry and why would you use Entry ?  Comparable sounds cool; what does it mean?  What if we need more complex data than elements?  Week #13 assignment posted & due Tuesday  Programming Assignment #3  Programming Assignment #3 available now


Download ppt "Problem of the Day  Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?"

Similar presentations


Ads by Google