Presentation is loading. Please wait.

Presentation is loading. Please wait.

8.1 Tree Terminology and Applications

Similar presentations


Presentation on theme: "8.1 Tree Terminology and Applications"— Presentation transcript:

1 8.1 Tree Terminology and Applications
Chapter 8 - Trees

2 Attendance Quiz #25 Trees

3 Tip #27: Copying Objects Trees Containers hold objects, but not the ones you give them! Instead, when you add an object to a container (via e.g., insert or push_back, etc.), what goes into the container is a copy of the object. Once in a container, it's not uncommon for it to be copied further. If your container resizes or shrinks, objects will be moved (copied) around. If you insert something or erase something from a vector, string, or deque, existing container elements are typically moved (copied) around. If you use any of the sorting algorithms, remove, unique, rotate or reverse, etc., objects will be moved (copied) around. Yes, copying objects is the STL way! An object is copied by using it copying member function, in particular, the copy constructor and its copy assignment operator. Take away: If you fill a container with objects where copying is expensive, the simple act of putting the objects into the container could prove to be a performance bottleneck. The more things that get moved around in the container, the more memory and cycles you'll blow on making copies.

4 Lab 07 – 3D Maze

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

6 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

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) (Open) 1 (Blocked) Layer Out IN (Open) 1 (Blocked) (Open) 1 (Blocked) Down Up Height 1 (Blocked) (Open) (Exit)

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

9 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

10 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][2][1] = 2; 2

11 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; } 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;

12 Requirements Trees Points Requirement (40 Points) 5
argv[1] and argv[2] used for input / output streams respectively. No execution user interaction (i.e. system("pause"); or getchr();). 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. The Maze toString() function returns a formatted string of the maze (as described above.) 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_03.txt). Your Maze program uses backtracking to detect an unsolvable maze (lab07_in_04.txt). 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. (lab07_in_05.txt). VS_MEM_CHECK macro is included in main to detect memory leaks. No Memory leaks are reported.

13 Trees Chapter 8

14 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

15 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 organization

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

17 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

18 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine

19 The node at the top of a tree is called its root
Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the top of a tree is called its root dog cat wolf canine

20 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The links from a node to its successors are called branches dog cat wolf canine

21 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The successors of a node are called its children dog cat wolf canine

22 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine The predecessor of a node is called its parent

23 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors Each node in a tree has exactly one parent except for the root node, which has no parent dog cat wolf canine

24 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors Nodes that have the same parent are siblings dog cat wolf canine

25 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A node that has no children is called a leaf node

26 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors Leaf nodes also are known as external nodes, and nonleaf nodes are known as internal nodes dog cat wolf canine A node that has no children is called a leaf node

27 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A generalization of the parent-child relationship is the ancestor-descendant relationship

28 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog is the parent of cat in this tree dog cat wolf canine A generalization of the parent-child relationship is the ancestor-descendant relationship

29 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine cat is the parent of canine in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

30 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine canine is a descendant of cat in this tree A generalization of the parent-child relationship is the ancestor-descendant relationship

31 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog is an ancestor of canine in this tree dog cat wolf canine A generalization of the parent-child relationship is the ancestor-descendant relationship

32 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A subtree of a node is a tree whose root is a child of that node

33 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A subtree of a node is a tree whose root is a child of that node

34 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine A subtree of a node is a tree whose root is a child of that node

35 The level of a node is determined by its distance from the root
Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is determined by its distance from the root dog cat wolf canine

36 The level of a node is its distance from the root plus 1
Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The level of a node is its distance from the root plus 1 (also called depth) dog cat wolf canine Level 1 Level 2 Level 3

37 The level of a node is defined recursively
Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine Level 1 The level of a node is defined recursively Level 2 Level 3

38 The level of a node is defined recursively
Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors dog cat wolf canine Level 1 The level of a node is defined recursively Level 2 Level 3 If node n is the root of tree T, its level is 1 If node n is not the root of tree T, its level is the level of its parent

39 Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node dog cat wolf canine

40 The height of this tree is 3
Tree Terminology Trees A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node dog cat wolf canine The height of this tree is 3

41 Tree Terminology Summary
Trees Ancestor – A parent of a node or its ancestors. Branch – Link between nodes. Child – An immediate successor node. Children – Successor nodes. Depth – Level of a node. Descendent – A child node or its descendants. External Node – A node without children (leaf node.) Height – Number of nodes in the longest path from the root to a leaf node. Internal Node – A non-external node (non-leaf node.) Leaf Node – A node that has no children. Level – Distance from the root plus 1. Parent – Predecessor of a node. Root – Node at top of tree (without parents.) Sibling – Node with the same parent. Subtree – A node and all of its children. Successor – A child of a node.

42 Binary Trees In a binary tree, each node has two subtrees.
A set of nodes T is a binary tree if either of the following is true: T is empty. If T is not empty, it has a root node r with 0, 1, or 2 nonempty binary subtrees whose roots are connected to r by a branch. (TL = left subtree; TR = right subtree) TL , TR, or both can be empty trees. From now on, we will consistently use a NULL pointer to represent an empty subtree.

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

44 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 files use Huffman codes.

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

46 Huffman Tree Trees Examples: d : 10110 e : 010

47


Download ppt "8.1 Tree Terminology and Applications"

Similar presentations


Ads by Google