Podcast Ch17d Title: Drawing a Binary Tree

Slides:



Advertisements
Similar presentations
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Advertisements

CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 17 Binary.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
In a rectangular grid, each cell is denoted by a pair (level, column) A rectangular grid : (0, 1)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 16 Binary.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
CS 206 Introduction to Computer Science II 09 / 26 / 2008 Instructor: Michael Eckmann.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
CHAPTER 10.1 BINARY SEARCH TREES    1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Level Order Traversal of a Binary Tree Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Podcast Ch24c Title: Breadth First Search
Podcast Ch23e Title: Implementing Huffman Compression
Planning & System installation
Podcast Ch17b Title: Iterative Tree Traversal
Chapter 12 – Data Structures
Trees Chapter 11 (continued)
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Recursive Objects (Part 4)
Podcast Ch26a Title: Representing Graphs
Trees Chapter 11 (continued)
Chapter 10.1 Binary Search Trees
Podcast Ch17a Title: Expression Trees
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Data Structures for Java William H. Ford William R. Topp
Podcast Ch25d Title: Minimum Path Algorithm
Podcast Ch18b Title: STree Class
Podcast Ch24b Title: Graphs and Digraphs
Binary Tree Traversals
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Podcast Ch22c Title: Deleting from a Heap
Podcast Ch23b Title: BitArray Implementation
Podcast Ch18c Title: BST delete operation
Podcast Ch25c Title: Shortest Path Algorithm
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch26b Title: Digraph Class Implementation
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Podcast Ch24b Title: Strongly Connected Components
Podcast Ch21b Title: Collision Resolution
Data Structures Using C++ 2E
Podcast Ch23a Title: Bit Arrays
Presentation transcript:

Podcast Ch17d Title: Drawing a Binary Tree Description: Drawing a binary tree; building a shadow tree, the displayTree method Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Drawing a Binary Tree The methods displayTree(), drawTree(), and drawTrees() display a binary tree as a string or graphically. The algorithms employ many of the basic features of a binary tree.

Drawing a Binary Tree (continued) View the display of the tree as a rectangular grid with a cell denoted by the pair (level, column). The level is a row in the grid corresponding to a level in the tree. The column coordinate designates a region of the display measured left to right.

Drawing a Binary Tree (continued)

Drawing a Binary Tree (continued) The algorithm to display a tree is a two step process. The first step uses the recursive function buildShadowTree() to carry out an inorder scan of the tree which creates a second tree, called a shadow tree. Its nodes store the value of the node in the original tree formatted as a string and the (level, col) position of the shadow tree node in the grid. The second step performs a level order scan of the shadow tree to display the nodes.

Building a Shadow Tree As buildShadowTree() uses an inorder scan it moves from one grid column to another. For instance, with Tree 0, the order of visit is B D A E C. Note in the figure that this is the column-order for the nodes in the tree.

Student Question Carefully explain how BOTH the level and the column can be found from the inorder traversal

Building a Shadow Tree (continued) The nodes of a shadow tree are objects of type TNodeShadow. class TNodeShadow { public static int columnValue; public String nodeValueStr; public int level, column; public TNodeShadow left, right; public TNodeShadow () {} }

Building a Shadow Tree (continued) Each recursive call of buildShadowTree() allocates a TNodeShadow object and assigns it the node value string. It then makes recursive calls that create the left and right subtrees. Each recursive call in the inorder scan creates a node in the column specified by the static value columnValue and then increments the variable for the next recursive call.

buildShadowTree() // inorder scan used to build the shadow tree private static <T> TNodeShadow buildShadowTree(TNode<T> t, int level) { // reference to new shadow tree node TNodeShadow newNode = null; if (t != null) { // create the new shadow tree node newNode = new TNodeShadow(); // allocate node for left child at next level // in tree; attach node newNode.left = buildShadowTree(t.left, level+1);

buildShadowTree() (concluded) // initialize variables of the new node // format conversion newNode.nodeValueStr = t.nodeValue.toString(); newNode.level = level; newNode.column = TNodeShadow.columnValue; // update column to next cell in the table TNodeShadow.columnValue++; // allocate node for right child at next level // in tree; attach node newNode.right = buildShadowTree(t.right, level+1); } return newNode;

Student Question When displaying the shadow tree, what factors need to be taken into account to decide the proper indention before printing a node value?

Displaying a Shadow Tree The method displayTree() takes the root, t, of the binary tree as an argument and calls buildShadowTree() to create the shadow tree. // build the shadow tree TNodeShadow shadowRoot = buildShadowTree(t, 0);

Displaying a Shadow Tree (cont) Display the tree with a level order scan of the shadow tree. The scan uses the node data to position and display each node. // use for the level-order scan of the shadow tree LinkedQueue<TNodeShadow> q = new LinkedQueue<TNodeShadow>(); String displayStr = ""; int colWidth = maxCharacters + 1; int currLevel = 0, currCol = 0; // use during the level order scan of the shadow tree TNodeShadow currNode;

displayTree() // return a string that displays a binary tree; // output of a node value requires no more than // maxCharacters public static <T> String displayTree(TNode<T> t, int maxCharacters) { // use for level-order scan of the shadow tree LinkedQueue<TNodeShadow> q = new LinkedQueue<TNodeShadow>(); String displayStr = ""; int colWidth = maxCharacters + 1; int currLevel = 0, currCol = 0; TNodeShadow.columnValue = 0; if (t == null) return displayStr; // build the shadow tree TNodeShadow shadowRoot = buildShadowTree(t, 0);

displayTree() (continued) // use during level order scan of the shadow tree TNodeShadow currNode; // insert the root in the queue and set current // level to 0 q.push(shadowRoot); // continue the iterative process until the queue // is empty while(!q.isEmpty()) { // delete front node from queue and make it // the current node currNode = q.pop();

displayTree() (continued) // if level changes, output a newline if (currNode.level > currLevel) { currLevel = currNode.level; currCol = 0; displayStr += '\n'; } // if a left child exists, insert the child // in the queue if (currNode.left != null) q.push(currNode.left); // if a right child exists, insert the child if (currNode.right != null) q.push(currNode.right);

displayTree() (concluded) // output formatted node value if (currNode.column > currCol) { displayStr += formatChar(( currNode.column - currCol) * colWidth, ' '); currCol = currNode.column; } displayStr += formatString(colWidth, currNode.nodeValueStr); currCol++; displayStr += '\n'; // delete the shadow tree shadowRoot = clearShadowTree(shadowRoot); return displayStr;

Student Question How could this approach be adapted to draw a binary tree in a GUI interface so that the tree has branches displayed as well as node values?