8.1 Tree Terminology and Applications

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Introduction to Trees Chapter 6 Objectives
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
COSC2007 Data Structures II
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
CS 201 Data Structures and Algorithms
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Data Structures and Design in Java © Rick Mercer
Problems with Linked List (as we’ve seen so far…)
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
CMSC 341 Introduction to Trees.
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
CSC 172– Data Structures and Algorithms
i206: Lecture 13: Recursion, continued Trees
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search Trees
Data Structures and Database Applications Binary Trees in C#
8.1 Tree Terminology and Applications
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
7.4 Problem Solving with Recursion
8.2 Tree Traversals Chapter 8 - Trees.
Trees and Binary Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Lab 07 – 3D Maze.
Trees Lecture 9 CS2110 – Fall 2009.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Trees Chapter 6.
CE 221 Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Pointers & Dynamic Data Structures
CS210- Lecture 9 June 20, 2005 Announcements
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

8.1 Tree Terminology and Applications Chapter 8 - Trees

Attendance Quiz #23 Stacks

Attendance Quiz #24 Stacks

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.

Lab 07 – 3D Maze

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.

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

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)

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)

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

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

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;

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.)

Trees Chapter 8

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

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

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).

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. 447-453

Tree Terminology Trees A tree is a hierarchical data structure of elements or nodes, with each node linked to its descendants.

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

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.

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.

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.

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

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)

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.

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

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