Download presentation
Presentation is loading. Please wait.
1
8.1 Tree Terminology and Applications
Chapter 8 - Trees
2
Attendance Quiz #23 Stacks
3
Attendance Quiz #24 Stacks
4
Tip #25: Compiler Warnings
Recursion Many programmers routinely ignore compiler warnings. If the problem were serious, it would be an error, right? Not so in C++! Sloppy programming may lead to erroneous program behavior, followed by a lot of difficult debugging. #include <iostream> using namespace std; class B { public: virtual void func() const { cout << "B::func"; } }; class D : public B virtual void func() { cout << "D::func"; } warning: D::func() hides B:func() "Of course, that's what it's supposed to do!" int main() { B* d = new D(); d->func(); return 0; } Take compiler warnings seriously and strive to compile warning-free at the maximum warning level supported by your compiler. Don't become dependent on compiler warnings, because different compilers warn about different things – porting to a new compiler may eliminate warning messages you've come to rely on.
5
Lab 07 – 3D Maze
6
Finding a Path through a Maze
Trees Problem Use backtracking to find and display the path through a maze. From each point in a maze you can move to the next cell in a horizontal or vertical direction if the cell is not blocked. Analysis The maze will consist of an array of cells. The starting point is at the top left corner maze[0][0][0]. The exit point is at the bottom right corner, that is maze[HEIGHT - 1][WIDTH - 1][LAYERS - 1]. All cells on the path will have a OPEN value. All cells that represent barriers will have a BLOCKED value. Cells that we have visited will have a TEMPORARY value. If we find a path through the maze, the exit cell value will be EXIT and all cells on the path will have the PATH value.
7
The Maze Layout Trees The maze size is defined by height x width x layer as specified on the first line of the test file. Width Left Right START 0,0,0 0,1,0 0,2,0 0,3,0 1,0,0 1,1,0 1,2,0 1,3,0 2,0,0 2,1,0 2,2,0 2,3,0 3,0,0 3,1,0 3,2,0 3,3,0 Layer Out IN 0,0,1 0,1,1 0,2,1 0,3,1 1,0,1 1,1,1 1,2,1 1,3,1 2,0,1 2,1,1 2,2,1 2,3,1 3,0,1 3,1,1 3,2,1 3,3,1 0,0,2 0,1,2 0,2,2 0,3,2 1,0,2 1,1,2 1,2,2 1,3,2 2,0,2 2,1,2 2,2,2 2,3,2 3,0,2 3,1,2 3,2,2 3,3,2 Down Up Height 0,0,3 0,1,3 0,2,3 0,3,3 1,0,3 1,1,3 1,2,3 1,3,3 2,0,3 2,1,3 2,2,3 2,3,3 3,0,3 3,1,3 3,2,3 EXIT 3,3,3
8
The Maze Layout Trees The maze size is defined by height x width x layer as specified on the first line of the test file. Width Left Right (START) (Open) 1 (Blocked) Layer Out IN (Open) 1 (Blocked) (Open) 1 (Blocked) Down Up Height 1 (Blocked) (Open) (Exit)
9
The Maze Layout (Bonus)
Trees Maze values tell which way to proceed thru the maze ("L", "R", "U", "D", "I", or "O".) Width Left Right R (Right) I (In) (Open) 1 (Blocked) Layer Out IN I (In) L (Left) 1 (Blocked) (Open) Down Up Height R (Right) I (In) 1 (Blocked) (Open) 1 (Blocked) D (Down) R (Right) E (Exit)
10
Dynamic Arrays Trees A dynamic array is basically an array of pointers to arrays. A 2 dimensional dynamic array is created/deleted using a loop as follows: int height = 10; int width = 10; int **myArray = new int*[height]; for(int i = 0; i < height; ++i) { myArray[i] = new int[width]; } Constructor for(int i = 0; i < height; ++i) { delete [] myArray[i]; } delete [] myArray; Destructor
11
3D Maze Array Trees The 3D maze array is of data type "int***" - a pointer to a pointer to a pointer to an int. int** int* int int*** maze_ vs. int maze_[3][2][2] maze_[2][1][1] = 2; 2
12
2D Implementation bool find_maze_path(int height, int width) {
Trees bool find_maze_path(int height, int width) { if ((height < 0) || (height >= HEIGHT) || (width < 0) || (width >= WIDTH)) return false; // out of bounds (base case #1) if (maze_[height][width] != OPEN) return false; // blocked (base case #2) if ((height == HEIGHT - 1) && (width == WIDTH - 1)) maze_[height][width] = EXIT; // success! (base case #3) return true; } // NOTE: not the search pattern used for 3d solutions maze_[height][width] = PATH; // possible path, try all paths if (find_maze_path(height - 1, width) || // check up find_maze_path(height + 1, width) || // check down find_maze_path(height, width - 1) || // check left find_maze_path(height, width + 1)) // check right return true; // one of the paths works maze_[height][width] = TEMPORARY; // none work, don’t visit again return false;
13
Requirements 8 (+2) 8 2 -10 Trees Points Requirement (40 + 8 Points)
Backtracking used to correctly solve a 4 x 4 x 4 maze (lab07_in_01.txt). Backtracking used to correctly solve a 5 x 5 x 5 maze (lab07_in_02.txt). Backtracking used to correctly solve a 5 x 10 x 6 maze (lab07_in_02.txt). Backtracking used to correctly solve a large maze. BONUS: Your maze program outputs the solution path using 'L' for move left, 'R' for move right, 'U' for move up on the same layer, 'D' for move down on the same layer, 'I' for move to the next layer (layer + 1), and 'O' for move to the previous layer (layer - 1). In addition, 'E' indicates the exit point. 8 Your Maze program uses backtracking to detect and report an unsolvable maze (lab07_in_05.txt). -10 Memory leaks, g++ compiler warnings, array out-of-bounds, or use of STL container detected. Points Peer Review 2 A Maze class contains a dynamically sized 3-dimensional maze array as specified by the first line of the input file. The Maze class is derived from the abstract MazeInterface interface class. No STL container is used anywhere in your Maze class. The Maze toString() function returns a formatted string of the maze (as described above.)
14
Trees Chapter 8
15
Chapter Objectives Trees To learn how to use a tree to represent a hierarchical organization of information To learn how to use recursion to process trees To understand the different ways of traversing a tree To understand the differences between binary trees, binary search trees, and heaps To learn how to implement binary trees, binary search trees, and heaps using linked data structures and arrays To learn how to use a binary search tree to store information so that it can be retrieved in an efficient manner To learn how to use a Huffman tree to encode characters using fewer bits than ASCII or Unicode, resulting in smaller files and reduced storage requirements
16
Trees - Introduction Trees All previous data organizations we've studied are linear—each element can have only one predecessor and one successor. Accessing all elements in a linear sequence is O(n). Trees are nonlinear and hierarchical. Tree nodes can have multiple successors (but only one predecessor). Trees can represent hierarchical organizations of information: Class hierarchy Disk directory and subdirectories Business organizations Networking topologies Family organizations
17
Trees - Introduction Trees Trees are recursive data structures because they can be defined recursively. Many methods to process trees are written recursively. post-order pre-order in-order This chapter focuses on the binary tree. In a binary tree each element has two successors. Binary trees can be represented by arrays and by linked data structures. Searching a binary search tree, generally is more efficient than linearly searching an ordered list—O(log n) versus O(n).
18
8.1, pgs. 447-453 8.1 Tree Terminology and Applications
Binary Trees Some Types of Binary Trees Fullness and Completeness General Trees 8.1, pgs
19
Tree Terminology Trees A tree is a hierarchical data structure of elements or nodes, with each node linked to its descendants.
20
Tree Terminology Trees A tree is a hierarchical data structure of elements or nodes, with each node linked to its descendants. The node at the top of a tree is called the root The link from a node to its descendant is called a branch Dog Cat Wolf Bear A node is a parent of its immediate descendant nodes Descendants of a parent node are called its children Nodes that have the same parent are siblings A node that has no children is called a leaf node A subtree of a node is a tree whose root is a child of that node
21
Tree Terminology Trees Each node in a tree has exactly one parent, except for the root node which has no parent. Leaf nodes also are known as external nodes, and non-leaf nodes are known as internal nodes. A generalization of the parent-child relationship is the ancestor-descendant relationship. The depth of a node is the number of nodes from the root (defined recursively): If a node is the root of a tree, its depth is 0. If a node is not the root of a tree, its depth is 1 + the depth of its parent. Depth 0 Height 3 Level 1 Depth 2 Height 1 Level 3 Depth 1 Level 2 Dog Cat Pig Wolf The height of a node is the number of nodes in the longest path from the node to a leaf node. The level of a node is its depth + 1.
22
Fullness, Predecessor, Successor
Trees Every node in a binary tree has at most 2 children. Each node contains Data Pointer to left child (TL). Pointer to right child (TR). 7 10 1 12 9 3 5 2 11 6 4 13 Root Successor A full binary tree is a binary tree where all internal nodes have exactly 2 children. A node predecessor is the left right-most node. Root Predecessor A node successor is the right left-most mode.
23
Completeness A complete binary tree of height h
Trees Note: not a full binary tree as node 9 only has 1 child. 7 10 3 11 9 5 1 6 4 8 2 Height: 4 Level: 1 Height: 3 Level: 2 is filled (has a value) down to level (h – 1) and, Height: 2 Level: 3 Height: 1 Level: 4 at level h, any un-filled nodes are to the right. A complete binary tree of height h Also, with a complete binary tree, All nodes down to level (h – 2) have two children. If a node at level (h – 1) has one child, it is a left child and all nodes to the left have two children.
24
Tree Height: Root Predecessor: Root Successor: Full Complete 3 6 10
Tree Height: Root Predecessor: Root Successor: Full Complete 4 6 10 8 4 12 2 6 10 14 8 4 12 2 6 10 14 11 15 Tree Height: Root Predecessor: Root Successor: Full Complete 4 7 10 Tree Height: Root Predecessor: Root Successor: Full Complete 4 7 9 8 4 12 2 6 10 14 1 3 5 7 8 4 12 2 6 10 14 5 7 9 11 13 15 Tree Height: Root Predecessor: Root Successor: Full Complete 4 10 Tree Height: Root Predecessor: Root Successor: Full Complete 4 6 10 8 4 12 2 10 14 1 15 8 4 12 2 6 10 14 1 3 5
25
Expression Tree Each node contains an operator or an operand.
Trees Each node contains an operator or an operand. Operands are stored in leaf nodes. Parentheses are not stored in the tree because the tree structure dictates the order of operand evaluation. Operators in nodes at higher tree levels are evaluated after operators in nodes at lower sub-tree levels. (x + y) * ((a + b) / c)
26
Huffman Tree Trees A Huffman tree represents Huffman codes for characters that might appear in a text file. As opposed to ASCII or Unicode, Huffman code uses different numbers of bits to encode letters; more common characters use fewer bits. Many programs that compress text files use Huffman codes.
27
Huffman Tree Trees To form a code, traverse the tree from the root to the chosen character, appending 0 if you branch left, and 1 if you branch right. Examples: e : 010 d : 10110
28
General Trees Trees In general, any tree can be represented using a binary tree. Children Siblings Children Siblings Children Siblings Children Siblings The left branch of a node is the oldest child, and each right branch is connected to the next younger sibling (if any). Children Siblings Siblings
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.