Download presentation
Presentation is loading. Please wait.
1
CS 261 – Data Structures Trees
2
Trees Ubiquitous – they are everywhere in CS
Probably ranks third among the most used data structure: Arrays/Vectors Lists Trees
3
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
4
Tree Characteristics (cont.)
Nodes that have the same parent are siblings The descendents of a node consist of its children, and their children, and so on All nodes in a tree are descendents of the root node (except, of course, the root node itself) Any node can be considered the root of a subtree Like a subset, a subtree need not be “proper” (i.e., be strictly smaller than the original) A subtree rooted at a node consists of that node and all of its descendents
5
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)
6
Tree Characteristics (cont.)
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 Root (depth = 0, height = 4) B C Subtree rooted at node C D E F Leaf node (depth = 4, height = 0)
7
Tree Characteristics (cont.)
Are these trees? Yes No No
8
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 n will have 2n+1 – 1 nodes Height of n will have 2n leaves
9
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 n will have 2n+1 – 1 nodes Height of n will have 2n leaves Complete Binary Tree: full except for the bottom level which is filled from left to right
10
Complete Tree: Height and Node Count
What is the height of a complete binary tree that has n nodes? We will come back to this later when we have algorithms whose time complexity is proportional to the path length
11
Dynamic Array Implementation
Complete binary tree has structure that is efficiently implemented with a DynArr: Children of node i are stored at 2i + 1 and 2i + 2 Parent of node i is at floor((i - 1) / 2) a Root b c a 1 b 2 c 3 d 4 e 5 f 6 7 d e f Why is this a bad idea if tree is not complete?
12
Dynamic Array Implementation (cont.)
If the tree is not complete (it is thin, unbalanced, etc.), the DynArr implementation will be full of holes a b c d e f a 1 b 2 c 3 4 d 5 6 e 7 8 9 10 11 12 13 f 14 15 Big gaps where the level is not filled!
13
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
14
Binary Tree Application: Animal Game
Purpose: guess an animal using a sequence of questions Internal nodes contain yes/no questions Leaf nodes are animals Initially, tree contains a single animal (e.g., a “cat”) stored in the root node 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 ask “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
15
Binary Tree Application: Animal Game
Swim? Fish Yes No Cat Fly? Bird Cat Cat Swim? Fish Yes No
16
Binary Tree Traversals
Just as a list, it is often necessary to examine every node in a tree A list is a simple linear structure: can be traversed either forward or backward – but usually forward What order do we visit nodes in a tree? Most common traversal orders: Pre-order In-order Post-order
17
Binary Tree Traversals (cont.)
All traversal algorithms have to: Process node Process left subtree Process right subtree Traversal order determined by the order these operations are done Six possible traversal orders: Node, left, right Pre-order Left, node, right In-order Left, right, node Post-order Node, right, left Right, node, left Right, left, node Most common traversals. Subtrees are not usually analyzed from right to left.
18
Pre-Order Traversal Process order Node, Left subtree, Right subtree
void preorder(struct Node *node) { if (node != 0){ process (node->val); preorder(node->left); preorder(node->rght); } Example result: p s a m a e l r t e e p s e a m l r a t e e
19
Post-Order Traversal Process order Left subtree, Right subtree, Node
void postorder(struct Node *node) { if (node != 0){ postorder(node->left); postorder(node->rght); process (node->val); } Example result: a a m s l t e e r e p p s e a m l r a t e e
20
In-Order Traversal Process order Left subtree, Node, Right subtree
void inorder(struct Node *node) { if (node != 0){ inorder(node->left); process(node->val); inorder(node->rght); } Example result: a sample tree p s e a m l r a t e e
21
Binary Tree Traversals: Euler Tour
An Euler Tour “walks” around the tree’s perimeter Each node is visited three times: 1st visit: left side of node 2nd visit: bottom side of node 3rd visit: right side of node Leaf nodes are visited three times in succession Traversal order depends on when node processed: Pre-order: 1st visit In-order: 2nd visit Post-order: 3rd visit p s e a m l r a t e e
22
Traversal Example Pre-order: + a * + b c d (Polish notation)
In-order: a + (b + c) * d (parenthesis added) Post-order: a b c + d * + (reverse Polish notation) + a * + d b c
23
Traversals Computational complexity: Problems with traversal code:
Each traversal requires constant work at each node (not including recursive calls) Order not important Iterating over all n elements in a tree requires O(n) time Problems with traversal code: If internal: ties (task dependent) process method to tree implementation If external: exposes internal structure (access to nodes) Not good information hiding Recursive function can’t return single element at a time Solution Iterator (more on this later)
24
Level-Order Enumeration
Haven’t seen this traversal yet: Traverse nodes a level at a time from left to right Start with root level and then traverse its children and then their children and so on Example result: p s e a m l r a t e e p s e a m l r a t e e
25
Representing General Trees
Binary trees are the most common type found in CS: However, it is sometimes useful to allow more than two children per node General trees (any number of children per node): In-order traversal is not well defined Pre-order, post-order, and level-order will still work Any general tree can be represented using a binary tree: A node’s left data member refers to its first child A node’s rght data member refers to its next sibling
26
General Tree: Binary Tree Representation
left rght rght rght left left left left
27
General Trees: Pre- and Post-Order Traversal
The pre-order traversal of the general tree corresponds to a pre-order traversal of the binary tree The post-order traversal of the general tree corresponds to an in-order traversal of the binary tree
28
Questions Lesson on tree
Next topic: implementing a useful tree data structure
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.