Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS261 Data Structures Trees Introduction and Applications.

Similar presentations


Presentation on theme: "CS261 Data Structures Trees Introduction and Applications."— Presentation transcript:

1 CS261 Data Structures Trees Introduction and Applications

2 Goals Tree Terminology and Definitions Tree Representation
Tree Application

3 Examples of Trees feature model bones hierarchy file system
family tree

4 Trees Ubiquitous – they are everywhere in CS
Probably ranks third among the most used data structure: Arrays/Vectors Lists Trees

5 What are the most important tree properties to learn?

6 How do you recognize when to use a tree?
Bag - BST General tree for hierarchical structures.

7 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

8 Tree Characteristics (cont…)
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

9 Tree Characteristics (cont.)
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)

10 Tree Characteristics (cont.)
B C D E F

11 Tree Characteristics (cont.)
Root (depth = 0, height = 4) 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 B C Subtree rooted at node C height = 3) D E F Leaf node (depth = 4, height = 0)

12 Tree Characteristics (cont.)
Are these trees? Yes No No

13 Full Binary Tree Binary Tree Full Binary Tree:
Nodes have no more than two children Children are generally referred to as “left” and “right” Full Binary Tree: every leaf is at the same depth Every internal node has 2 children Height of h will have 2h+1 – 1 nodes Height of h will have 2h leaves

14 Complete Binary Tree Complete Binary Tree: full except for the bottom
level which is filled from left to right

15 Complete Binary Tree Is this a complete binary tree?
MUCH more on these later!

16 Dynamic Memory Implementation
struct Node { TYPE val; struct Node *left; /* Left child. */ struct Node *rght; /* Right child. */ }; Like the Link structure in a linked list: we will use this structure in several data structures This representaiton does not suffer from space inefficiencies when unbalanced because it allocates nodes as needed, much like the linked list! So, we’ll see both of these implementations over the next two weeks, however, right now, I want to talk about an application of the binary tree.

17 Binary Tree Application: Animal Game
Purpose: guess an animal 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

18 Binary Tree Application: Animal Game
Initially, tree contains a single animal (e.g., a “cat”) stored in the root node Guessing…. Start at root. 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 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

19 Binary Tree Application: Animal Game
Swim? Fish Yes No Cat Fly? Bird Cat Cat Swim? Fish Yes No (I’m thinking of a Fish.) Starts with Nothing and immediately guesses: I know it’s a cat What is a question that distinguishes a cat from a fish? Does it swim? Yes is fish, no is cat. Ok, let’s play again….. (I’m thinking of a Bird) Does it swim? No I know, is it a cat? No. What is a questions to distinguish a cat from a bird? Does it fly? No: Cat Yes:Bird Ok, let’s play again…

20

21 Decision Tree 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 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 This binary tree is called a ‘decision tree’. If you can ask at most q questions, then the height of the tree is at most q. This tree has a max of 2^q leaves (ie. full binary tree of height h has t^h leaves). Therefore the max number o fthings, N, we can distinguish between is : n = 2^q , taking log of both tells us how many questions, q , we need. Log2 1 million is approximately 20 What does this mean? You can take 1million and break it in half approximately 20 times and thefeore distinguish between items using only 20 questions!

22 How do we ensure that our tree is balanced?
Generate lots of trees and choose those with best balance properties rebalance the tree?

23 Still To Come… Implementation Concepts Implementation Code


Download ppt "CS261 Data Structures Trees Introduction and Applications."

Similar presentations


Ads by Google