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 212 – Data Structures

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  Height of leaf 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 have other methods beyond these in class

11 Tree Interface  Often have other methods beyond these in class addremove  For example, want methods to add or remove data

12 Tree Interface  Often have other methods beyond these in class addremove  For example, want methods to add or remove data

13 Tree Interface  Often have other methods beyond these in class addremove  For example, want methods to add or remove data  Else root & size fields needed to change Tree

14 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); } }

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

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

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

18 D  B D A CE F B AF CE

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

20 View of an Actual Tree

21 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; } }

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

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

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

25 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

26 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

27 012345678910 Array-based Impl. A HG FE D C B J

28 012345678910 Array-based Impl. A HG FE D C B J A BD EFCJ GH

29 012345678910 2 nd Array-based Impl. A HG FE D C B J ABDEFCJGH

30 012345678910 Correct Correct Array-based Impl. A HG FE D C B J 4 F 6 J 0 A 1 B 2 D 3 E 5 C 9 G 10 H

31 0123456789 Position Array Refers to Position s A HG FE D C B J 4 F 6 J 0 A 1 B 2 D 3 E 5 C 9 G 10 H

32 123456789 Implicitly Node s Implicitly Linked Only A HG FE D C B J 4 F 6 J A 1 B 2 3 E 5 C 9 G 10 H D

33 0123456789 Implicitly Node s Implicitly Linked Only A HG FE D C B J F 6 J 0 A 1 B 2 3 E 5 C 9 G 10 H D

34 Node for Array-based Impl. 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; } }

35 Your Turn  Get into your groups and complete activity

36 For Next Lecture  Read GT 8.1 – 8.1.2 for Wednesday'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 posted tomorrow & due next Tuesday  Week #12 due tomorrow as you should be used to  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