CS261 Data Structures Trees Introduction and Applications Goals - - PowerPoint PPT Presentation

cs261 data structures
SMART_READER_LITE
LIVE PREVIEW

CS261 Data Structures Trees Introduction and Applications Goals - - PowerPoint PPT Presentation

CS261 Data Structures Trees Introduction and Applications Goals Tree Terminology and Definitions Tree Representation Tree Application Examples of Trees Trees Ubiquitous they are everywhere in CS Probably ranks third


slide-1
SLIDE 1

CS261 Data Structures

Trees Introduction and Applications

slide-2
SLIDE 2

Goals

  • Tree Terminology and Definitions
  • Tree Representation
  • Tree Application
slide-3
SLIDE 3

Examples of Trees

slide-4
SLIDE 4

Trees

  • Ubiquitous – they are everywhere in CS
  • Probably ranks third among the most used data

structure:

  • 1. Arrays/Vectors
  • 2. Linked Lists
  • 3. Trees
slide-5
SLIDE 5

Tree Characteristics

  • A tree consists of a collection of nodes connected by directed arcs
  • A tree has a single root node

– By convention, the root node is usually drawn at the top

  • A node that points to (one or more) other nodes is the parent of

those nodes while the nodes pointed to are the children

  • Every node (except the root) has exactly one parent
  • Nodes with no children are leaf nodes
  • Nodes with children are interior nodes

a d b i e f g c h

slide-6
SLIDE 6

Tree Characteristics

a d b i e f g c h Nodes Directed Arcs

Interior Nodes: have one or more children Leaf Nodes: have no children

slide-7
SLIDE 7

Tree Characteristics

a d b i e f g c h Siblings Descendants of node d Subtree rooted at node d

slide-8
SLIDE 8

Tree Characteristics

  • Nodes that have the same parent are siblings
  • The descendants of a node consist of its

children, and their children, and so on – All nodes in a tree are descendants of the root node (except, of course, the root node itself)

  • Any node can be considered the root of a

subtree

  • A subtree rooted at a node consists of that

node and all of its descendants

a d b i e f g c h

slide-9
SLIDE 9

Tree Characteristics

  • There is a single, unique path from the root to any

node – Arcs don’t join together

  • A path’s length is equal to the number of arcs

traversed

  • A node’s height is equal to the maximum path length

from that node to a leaf node: – A leaf node has a height of 0 – The height of a tree is equal to the height of the root

  • A node’s depth is equal to the path length from the

root to that node: – The root node has a depth of 0 – A tree’s depth is the maximum depth of all its leaf nodes (which, of course, is equal to the tree’s height)

a d b i e f g c h

slide-10
SLIDE 10

Tree Characteristics

a d b i e f g c h

Root: height = 3 Depth = 0 Height = 2: path length to furthest leaf Depth = 3: path length to node from the root

slide-11
SLIDE 11

Tree Characteristics (cont.)

Root (depth = 0, height = 4)

A B C D E

Subtree rooted at node C

  • Nodes D and E are

children of node B

  • Node B is the parent of

nodes D and E

  • Nodes B, D, and E are

descendents of node A

(as are all other nodes in the tree…except A)

  • E is an interior node
  • F is a leaf node

F

Leaf node (depth = 4, height = 0)

height = 3)

slide-12
SLIDE 12

Tree Characteristics (cont.)

Are these trees? Yes No No

slide-13
SLIDE 13

Binary Tree

  • Binary Tree

–Nodes have no more than two children –Children are generally referred to as “left” and “right”

slide-14
SLIDE 14

Full Binary Tree

  • Every node is either a leaf or has exactly 2

children

slide-15
SLIDE 15

Perfect Full Binary Tree

Perfect & Full All leaves are at the same depth and all internal nodes have 2 children h = 1 # leaves = 2 # nodes = 3 h = 2 # leaves = 4 # nodes = 7 h = 3 # leaves = 8 # nodes = 15

Height of h will have 2h leaves Height of h will have 2h+1 – 1 nodes

slide-16
SLIDE 16

Complete Binary Tree

  • Complete Binary Tree:

full except for the bottom level which is filled from left to right

slide-17
SLIDE 17

Not a Complete Binary Tree

slide-18
SLIDE 18

Binary Tree Application: Animal Game

  • Purpose: computer guesses an animal that you (the player ) is

thinking of using a sequence of questions

–Internal nodes contain yes/no questions –Leaf nodes are animals (or answers!)

  • How do we build it?

Swim? Fish

Yes No

Cat Fly? Bird

Yes No

slide-19
SLIDE 19

Binary Tree Application: Animal Game

Cat Cat Swim? Fish

Yes No

Swim? Fish

Yes No

Cat Fly? Bird

Yes No

slide-20
SLIDE 20

Binary Tree Application: Animal Game

Initially, tree contains a single animal (e.g., a “cat”) stored in the root node

Guessing….

  • 1. Start at root.
  • 2. If internal node  ask yes/no question
  • Yes  go to left child and repeat step 2
  • No  go to right child and repeat step 2
  • 3. If leaf node  guess “I know. Is it a …”:
  • If right  done
  • If wrong  “learn” new animal by asking for a yes/no question that

distinguishes the new animal from the guess

slide-21
SLIDE 21

How many things can you distinguish between with q questions?

  • If you can ask at most q questions, the number of

possible answers we can distinguish between, n, is the number of leaves in a full binary tree with height at most q, which is at most 2q

  • Taking logs on both sides: log(n) = log(2q)
  • log(n) = q : for n outcomes,

we need q questions

  • For 1,048,576 outcomes we

need 20 questions

slide-22
SLIDE 22

CS261 Data Structures

Binary Search Trees Concepts

slide-23
SLIDE 23

Binary Search Tree

  • Binary search trees are binary trees where every

node’s value is:

– Greater than all its descendents in the left subtree – Less than or equal to all its descendents in the right subtree

slide-24
SLIDE 24

Intuition

50 25 75 35 60 85 30 45 65 80 20

All < 50 All >=50

slide-25
SLIDE 25

BST: Contains Example

Alex Abner Angela Adela Abigail Alice Audrey Adam Agnes Allen Arthur

Object to find

Agnes

slide-26
SLIDE 26

BST: Add

  • Do the same type of traversal from root to leaf
  • When you find a null value, create a new node

(children of leaves are NULL)

Alex Abner Angela Adela Alice Audrey Adam Agnes Allen Arthur Abigail

slide-27
SLIDE 27

BST: Add Example

Alex Abner Angela Adela Alice Audrey Adam Agnes Allen Arthur Before first call to add

Object to add

Aaron

“Aaron” should be added here

Aaron Abigail

slide-28
SLIDE 28

BST: Add Example

Alex Abner Angela Adela Alice Audrey Adam Agnes Allen After first call to add

Next object to add

Ariel Aaron Abigail

“Ariel” should be added here

Ariel Arthur

slide-29
SLIDE 29

BST: Remove

Alex Abner Angela Adela Alice Audrey Adam Agnes Allen Arthur Abigail How would you remove Abigail? Audrey? Angela?

slide-30
SLIDE 30

Who fills the hole?

  • Answer: the leftmost child of the right subtree

(smallest element in right subtree)

  • Try this on a few values
  • Alternatively: The rightmost child of the left

subtree

slide-31
SLIDE 31

Intuition…Remove 50

50 80 70 60 61 62 63 20 60 80 70 61 62 63 20

slide-32
SLIDE 32

BST: Remove Example

Alex Abner Angela Adela Abigail Alice Audrey Adam Agnes Allen Arthur Before call to remove

Element to remove

Replace with:

leftmost(rght)

slide-33
SLIDE 33

BST: Remove Example

Alex Abner Arthur Adela Abigail Alice Adam Agnes Allen Audrey After call to remove

slide-34
SLIDE 34

Special Case

  • What if you don’t have a right child?
  • Try removing “Audrey”

– Simply return left child

Angela Alice Audrey Allen Arthur

slide-35
SLIDE 35

Complexity Analysis (contains)

  • If tree is reasonably full (well balanced), searching

for an element is O(log n). Why?

– you’re dividing in half at each step: O(log n)

  • Alternatively, we are running down a path

from root to leaf

– We can prove by induction that in a complete tree (which is reasonably full), the path from root to leaf is bounded by floor(log n), so O(log n)

slide-36
SLIDE 36

Binary Search Tree: Useful Collection?

  • We’ve shown all operations (add, contains,

remove) to be proportional to the length of a path, rather than the number of elements in the tree

  • We’ve also said that in a reasonably full tree,

this path is bounded by : floor( (log2 n))

  • This is faster than our previous

implementations!

slide-37
SLIDE 37

Comparison

  • Average Case Execution Times

Operation Dynamic Array Linked List Ordered Array Binary Search Trees Add O(1+) O(1) O(n) O(logn) Contains O(n) O(n) O(logn) O(logn) Remove O(n) O(n) O(n) O(logn)

slide-38
SLIDE 38

Your Turn

  • Worksheet28_BSTPractice
slide-39
SLIDE 39

CS261 Data Structures

Binary Search Trees II Implementation

slide-40
SLIDE 40

Goals

  • BST Representation
  • Operations
  • Functional-style operations
slide-41
SLIDE 41

Binary Search Trees

struct BSTree { struct Node *root; int cnt; }; void addBSTree(struct BSTree *tree, TYPE val); int containsBSTree(struct BSTree *tree, TYPE val); void removeBSTree(struct BSTree *tree, TYPE val);

struct Node { TYPE val; struct Node *left struct Node *rght };

slide-42
SLIDE 42

Add - Recursive Approach

A useful trick: Recursive helper routine that returns the tree with the value inserted

Node addNode(Node current, TYPE value) if current is null then return new Node with value

  • therwise if value < Node.value

left child = addNode(left child, value) else right child = addNode(right child, value) return current node

slide-43
SLIDE 43

Visual Example

k q a m v Add “L” Add (k,“L”) k a = = q v Add(q,”L”) m Add(m, “L”) = Add(NULL, “L”) r e t u r n L

slide-44
SLIDE 44

BST Add: public facing add

void add(struct BSTree *tree, TYPE val) { tree->root = _addNode(tree->root, val); tree->cnt++; }

slide-45
SLIDE 45

Recursive Helper – functional flavor

struct Node *_addNode(struct Node *cur, TYPE val){ struct Node *newnode; if (cur == NULL){ /* insert: create Node with val* return /* the created node */ } if (val < cur->val) cur->left = _addNode(cur->left,val); else cur->right = _addNode(cur->right,val); return cur; }

slide-46
SLIDE 46

Iterative (non-recursive) Version

void add(struct BSTree *tree, TYPE val) { struct Node *cur = tree->root; if (tree->root == NULL) { // tree is empty tree->root == /* new Node with val */ ; } while (cur != null) { if (LT(val,cur->val)) if (cur->left == NULL) { cur->left = /* new Node with val */; return; } else cur = cur->left; else if (cur->right == NULL) { cur->right = /* new Node with val */; return; } else cur = cur->right;

slide-47
SLIDE 47

BST Remove

Alex Abner Angela Adela Alice Audrey Adam Agnes Allen Arthur Abigail How would you remove Abigail? Audrey? Angela?

slide-48
SLIDE 48

Who fills the hole?

  • Answer: the leftmost child of the right subtree

(smallest element in right subtree)

  • Useful to have a couple of private inner

routines:

slide-49
SLIDE 49

BST Remove Example

Alex Abner Angela Adela Abigail Alice Audrey Adam Agnes Allen Arthur Before call to remove

Element to remove

Replace with:

leftmost(rght)

slide-50
SLIDE 50

BST Remove Example

Alex Abner Arthur Adela Abigail Alice Adam Agnes Allen Audrey After call to remove

slide-51
SLIDE 51

Who fills the hole?

  • Answer: the leftmost child of the right subtree

(smallest element in right subtree)

  • Useful to have a couple of private inner routines:

TYPE _leftmost(struct Node *cur) { ... /* Return value of leftmost child of current node. */ } struct Node *_removeLeftmost(struct Node *cur) { ... /* Return tree with leftmost child removed. */ }

slide-52
SLIDE 52

BST Remove Pseudocode (recursive)

Node removeNode(Node current, TYPE value) if value = current.value if right child is null return left child else replace value with value in leftmost child of right subtree set right child to result of removeLeftmost(right) else if value < current.value left child = removeNode(left child, value) else right child = removeNode(right child, value) return current node

slide-53
SLIDE 53

Your Turn

  • Complete the BST implementation in

Worksheet #29

slide-54
SLIDE 54

Space Requirements

  • Does the recursive version require more or

less space than an iterative version?

  • Time overhead for recursive version?