Problem of the day
Download
1 / 42

Problem of the Day - PowerPoint PPT Presentation


  • 71 Views
  • Uploaded on

Problem of the Day. A person looks at a picture and says: Brothers and sisters I have none, but this man’s father is my father’s son. Who’s picture is he looking at ?. Problem of the Day.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Problem of the Day' - parley


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Problem of the day
Problem of the Day

  • A person looks at a picture and says:Brothers and sisters I have none, but this man’s father is my father’s son.Who’s picture is he looking at?


Problem of the day1
Problem of the Day

  • A person looks at a picture and says:Brothers and sisters I have none, but this man’s father is my father’s son.Who’s picture is he looking at?

  • He is looking at a picture of his son.


Csc 212 data structures

CSC 212 – Data Structures

Lecture 36:Tree Implementation


Node sounds technical
“Node” Sounds Technical…

  • Links in linked list commonly called “nodes”

  • Node is name for links in a Tree also

    • No relation to linked list class; totally different ideas

    • Each node’s data retrieved using getElement()

  • Node class often used by data structures

    • Node is not specific, but sounds better than “thingy”

    • More node classes to be seen, we use them a lot


Node heights
Node Heights

  • Longest count to leaf node

    • D's height is 1

    • B's height is 3

    • A's height is 4

A

D

B

C

E

G

H

F

K

I

J


Node heights1
Node Heights

  • Height 1 more than taller child

    • F's height is 2

    • B's height is 3

    • E's height is 1

  • Height of leaf always 1

A

D

B

C

E

G

H

F

K

I

J


Node depths
Node Depths

  • Distance to root node of Tree

    • A's depth is 0

    • C's depth is 1

    • I's depth is 3

A

D

B

C

E

G

H

F

K

I

J


Node depths1
Node Depths

  • Depth of a node is1 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

D

B

C

E

G

H

F

K

I

J


Binarytree interface
BinaryTree Interface

  • All of the methods defined by the interface

    E getRootElement()boolean contains(tgt)E find(tgt)Iterator<E> iteratorInOrder()Iterator<E> iteratorPreOrder() Iterator<E> iteratorPostOrder() Iterator<E> iteratorLevelOrder() Iterator<E> iterator()int size()booleanisEmpty()


Binarytree interface1
BinaryTree Interface

  • Often have other methods beyond these in class


Binarytree interface2
BinaryTree Interface

  • Often have other methods beyond these in class

    • For example, want methods to addor remove data


Binarytree interface3
BinaryTree Interface

  • Often have other methods beyond these in class

    • For example, want methods to addor remove data


Binarytree interface4
BinaryTree Interface

  • Often have other methods beyond these in class

    • For example, want methods to addor remove data

  • Good hint: Used in other ADTs through rest of year


Binarytree nodes
BinaryTreeNodes

  • BinaryTree interface never exposes nodes


Binarytree nodes1
BinaryTreeNodes

  • BinaryTree interface never exposes nodes

CENSORED


Binarytree nodes2
BinaryTreeNodes

  • BinaryTree interface never exposes nodes

    • Remain implementation-free; nodes detail how it works


Node class for tree
Node Class For Tree

class TNode<E> {private Eelement;private TNode<E>parent;private ArrayList<TNode<E>>kids;// Besides getters & setters, often include methods like:public TNode<E>getChild(inti) { return kids.get(i);}public void addChild(TNode<E> kid) { kids.addToRear(kid);}public TNode<E>removeChild(inti) { return kids.remove(i); }

}


Visualization of tree

Visualization of Tree

B

B

A

D

F

A

D

F

C

E

C

E


Node structure of the tree

Node Structure of the Tree

B

B

A

D

F

A

D

F

C

E

C

E


Links between tree s nodes

Links Between Tree's Nodes

B

B

A

D

F

A

D

F

C

E

C

E


Links between tree s nodes1

Links Between Tree's Nodes

B

B

A

D

F

A

D

F

C

E

C

E


Actual view of tree

Actual View of Tree

B

B

A

D

F

A

D

F

C

E

C

E



Linked node for binarytree
Linked Node for BinaryTree

class BTNode<E>{private Eelement;private BTNode<E>left;private BTNode<E>right;// Add getters & setters for each fieldpublic booleanhasLeftChild(){ return left != null; }public BTNode<E> getRight(){ return right; }

}


Picturing linked binarytree

D

C

B

A

Picturing LinkedBinaryTree

B

A

C

 

 

D


Nodes in linked binarytree

D

C

B

A

Nodes in LinkedBinaryTree

B

A

C





D


Pointers in linked binarytree
Pointers in LinkedBinaryTree

B

B

A

C

 

 

D

A

C

D


Array based binarytree
Array-based BinaryTree

  • Node at index specified for location in Tree

    • 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


Array based implementation
Array-based Implementation

  • Tree’s depth limited by array size

    • Where in array determined by location

  • Listovercomes limits

    • null added to pad space

    • Replace value at index to to add nodes


Array based implementation1
Array-based Implementation

  • Tree’s depth limited by array size

    • Where in array determined by location

  • Listovercomes limits

    • null added to pad space

    • Replace value at index to to add nodes


Array based implementation2
Array-based Implementation

  • Tree’s depth limited by array size

    • Where in array determined by location

  • Listovercomes limits

    • null added to pad space

    • Replace value at index to to add nodes


Array based implementation3
Array-based Implementation

  • Tree’s depth limited by array size

    • Where in array determined by location

  • Listovercomes limits

    • null added to pad space

    • Replace value at index to to add nodes

  • Cannot easily overcome limits

    • Be careful using this approach


Array based impl
Array-based Impl.

A

B

D

C

E

F

J

G

H


Array based impl1
Array-based Impl.

A

A

B

D

B

D

E

F

C

J

C

E

F

J

G

H

G

H


2 nd array based impl
2nd Array-based Impl.

A

G

B

D

H

F

C

J

E

A

B

D

C

E

F

J

G

H


Correct array based impl
Correct Array-based Impl.

A

0

G

C

E

B

D

F

J

A

1

2

B

D

3

4

5

6

C

E

F

J

9

10

G

H

H


Array refers to node s
Array Refers to Nodes

A

0

G

C

E

B

D

F

J

A

1

2

B

D

3

4

5

6

C

E

F

J

9

10

G

H

H


Node s implicitly linked only
Nodes Implicitly Linked Only

A

0

G

B

D

A

1

2

B

D

3

4

5

6

C

E

F

J

E

F

C

J

9

10

G

H

H


Node s implicitly linked only1
Nodes Implicitly Linked Only

A

0

G

B

D

A

1

2

B

D

3

4

5

6

C

E

F

J

E

F

C

J

9

10

G

H

H


Node for array based impl
Node for Array-based Impl.

public class BANode<E>{private Eelement;privateintmyIndex;privatebooleanhasLeft;privatebooleanhasRight;// Add getters & setters for each fieldpublic intgetLeft() { if (!hasLeft) { return -1; } return (myIndex* 2) + 1;}public void setRight() {hasRight= true; }

}


Your turn
Your Turn

  • Get into your groups and complete activity


For next lecture
For Next Lecture

  • Read 12.1 & online for Friday's lecture

    • What and how does a heap extend BinaryTree?

    • Why does heap sound familiar, but why is it not?

    • Can computer science ever be consistent with terms?

  • Week #13 posted today & due week from Tues.

  • Programming Assignment #2 available now


ad