Recall: Nodes and Linked Lists

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
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.
BST Data Structure A BST node contains: A BST contains
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees Chapter 6.
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.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 10-A Trees Modified
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Starting at Binary Trees
Chapter 20 Binary Search Trees
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
The Tree ADT.
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
COMP 53 – Week Fourteen Trees.
Pointers and Linked Lists
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
12 C Data Structures.
Week 6 - Wednesday CS221.
Section 8.1 Trees.
Data Structures & Algorithm Design
Week 11 - Friday CS221.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Tonga Institute of Higher Education
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
Pointers and Linked Lists
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 20 Binary Search Trees
Presentation transcript:

Chapter 13.1 – Binary Trees Chapter 19,20 – Trees and Binary Search Trees (Java Foundations )

Recall: Nodes and Linked Lists A Linked List: can grow and shrink while the program is running is constructed using pointers often consists of structs or classes that contain pointers connecting each other Advantages over Arrays and Vectors Easy to add and remove elements, slow random access Search: O(n) Insert at Head O(1), Insert in order or at End: O(n) 10 12 head 14 end Slide 13- 2

Nortel Corporate Presentation 13.2 Stacks A stack is a last-in/first-out data structure like the stack of plates in a cafeteria; adding a plate pushes down the stack and the top plate is the first one removed Used in our PostFix and PostFixRational Expression Evaluators Slide 13- 3 3 © 2004 Nortel

Nortel Corporate Presentation A Stack Class Create a stack class to store characters Adding an item to a stack is pushing onto the stack Member function push will perform this task Removing an item is popping the item off the stack Member function pop will perform this task Slide 13- 4 4 © 2004 Nortel

Nortel Corporate Presentation Function push The push function adds an item to the stack It uses a parameter of the type stored in the stack void push(char the_symbol); The same head_insert of the linked list For a stack, a pointer named top is used instead of a pointer named head Slide 13- 5 5 © 2004 Nortel

Nortel Corporate Presentation Function pop The pop function returns the item that was at the top of the stack char pop( ); Before popping an item from a stack, pop checks that the stack is not empty pop stores the top item in a local variable result, and the item is "popped" by: top = top->link; A temporary pointer must point to the old top item so it can be "deleted" to prevent a memory leak pop then returns variable result Slide 13- 6 6 © 2004 Nortel

Nortel Corporate Presentation Empty Stack An empty stack is identified by setting the top pointer to NULL or nullptr top = nullptr; What about memory leaks? What about the nodes that top pointed to? Slide 13- 7 7 © 2004 Nortel

Nortel Corporate Presentation Chapter 13.2 Queues A queue is a data structure that retrieves data in the same order the data was stored If 'A', 'B', and then 'C' are placed in a queue, they will be removed in the order ‘A', 'B', and then ‘C' A queue is a first-in/first-out data structure like the checkout line in a supermarket Slide 13- 8 8 © 2004 Nortel

Nortel Corporate Presentation Section 13.2 Conclusion Can you? Give the definition of stack member function push()? Know how to tell if a stack is empty? Know when to use a queue vs a stack? Slide 13- 9 9 © 2004 Nortel

Nortel Corporate Presentation Scope Trees: How Google finds websites Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals Expression trees © 2004 Nortel

Binary Search Trees A search tree is a tree whose elements are organized to facilitate finding a particular element when needed A binary search tree is a binary tree that, for each node n the left subtree of n contains elements less than the element stored in n the right subtree of n contains elements greater than or equal to the element stored in n This is how finds your websites!

How Google finds Websites Nortel Corporate Presentation How Google finds Websites Web Crawlers search the Internet for new websites Read every webpage and every word HUGE files of data – Petabytes! Data Centers process this data And Update Google Search Trees Every webpage, every day A petabyte (symbol: PB) is 1015 bytes of digital information. The prefix peta indicates the fifth power of 1000 and means 1015 in the International System of Units (SI), and therefore 1 petabyte is one quadrillion (short scale) bytes, or 1 billiard (long scale) bytes. 1 PB = 1000000000000000B = 1015 bytes = 1000terabytes. A related unit, the pebibyte (PiB), using a binary prefix, means 10245 bytes, which is more than 12% greater than 10005 bytes (250 bytes = 1125899906842624bytes). © 2004 Nortel

Google and Amazon Data Centers

Data Centers – A Closer Look All this just to update some search trees Some VERY BIG search trees! For more info: Dr. Mohamed Hefeeda Big-Data and Multimedia

Binary Search Trees To determine if a particular value exists in a tree start at the root compare target to element at current node move left from current node if target is less than element in the current node move right from current node if target is greater than element in the current node We eventually find the target or hit the end of a path (target is not found) How to find node with key value 38? Start at Root and compare 38 to 45 38 < 45 so go to left subtree 38 > 12 so go to the right 38 > 15 so go to the right again 38 < 42 so go left 38 > 33 so go right 38 found! Return Object stored at this node 45 12 15 42 33 38

Trees A Tree looks like an upside-down tree with the root at the top elements organized into a hierarchy a set of Nodes and Edges connecting those nodes Data elements are stored within the nodes Each node is located on a particular level There is only one root node in the tree No Cycles permitted Level 0 Level 1 Level 2 Root Node Edges Root Nodes

Trees Nodes at the lower level of a tree are the children of nodes at the previous level Nodes can have only one parent, but multiple children Nodes that have the same parent are siblings The root is the only node which has no parent Root has no Parent Root is Parent of A,B,C Level 0 Level 1 Level 2 Root Node A Node B Node C Node D Node E Node A is the Parent of Nodes D and E Children of Node A Siblings

Tree Terminology A node that has no children is a leaf node A node that is not the root and has at least one child is an internal node A subtree is a tree structure that makes up part of another tree We can follow a path through a tree from parent to child, starting at the root A node is an ancestor of a node if it is above it on the path from the root.

Trees Terminology Nodes that can be reached by following a path from a particular node are the descendants of that node The level of a node is the length of the path from the root to the node The path length is the number of edges to get from the root to the node The height of a tree is the length of the longest path from the root to a leaf Height = 2 Level 0 Level 1 Level 2 Root Node A Node B Node C Node D Node E

Trees – Quiz What are the descendents of node B? What is the level of node E? What is the path length to get from the root to node G? What is the height of this tree?

Classifying Trees Trees can be classified in many ways One important criterion is the maximum number of children any node in the tree may have This may be referred to as the order of the tree General trees have no limit to the number of children a node may have A tree that limits each node to no more than n children is referred to as an n-ary tree The tree for a TicTacToe game is an 9-ary tree

Binary Trees Trees in which nodes may have at most two children are called binary trees Root Node A Node B Node C Node D Node E Node F

Balanced Trees A tree is balanced if all of the leaves of the tree are on the same level or within one level of each other

Full and Complete Trees A balanced n-ary tree with m elements has a height of lognm A balanced binary tree with n nodes has a height of log2n An n-ary tree is full if all leaves of the tree are at the same height and every non-leaf node has exactly n children A tree is complete if it is full, or full to the next-to-last level with all leaves at the bottom level on the left side of the tree Root Node A Node B Node C Node D Node E Node F Full Binary Tree Root Node A Node B Node C Node D Node E Complete Binary Tree

Full and Complete Trees Three complete trees: Which trees are full? Only tree c is full

Implementing Trees An obvious choice for implementing trees is a linked structure data left right struct BinaryTreeNode { int data; BinaryTreeNode *left; BinaryTreeNode *right; }; data left right middle struct TertiaryTreeNode { int data; TertiaryTreeNode *left; TertiaryTreeNode *middle; TertiaryTreeNode *right; };

Tree Traversals For linear structures, the process of iterating through the elements is fairly obvious (forwards or backwards) For non-linear structures like a tree, it is more interesting Let's look at four classic ways of traversing the nodes of a tree All traversals start at the root of the tree Each node can be thought of as the root of a subtree A tree is a recursive data structure Traversing the tree will require a recursive algorithm

Tree Traversals Preorder : visit the root, then traverse the subtrees from left to right Inorder : traverse left subtree, then the root, then traverse right subtree Postorder : traverse the subtrees from left to right, then visit the root Level-order : visit each node at each level of the tree from top (root) to bottom and left to right

Tree Traversals Preorder: A B D E C Inorder: D B E A C Postorder: D E B C A Level-Order: A B C D E

Tree Traversals Recursion simplifies the implementation of tree traversals Preorder: Visit node Traverse (left child) Traverse (right child) Inorder: Postorder: Level-order traversal is more complicated and requires queues

Expression Trees An expression tree is a tree that shows the relationships among operators and operands in an expression An expression tree is evaluated from the bottom up

Decision Trees A decision tree is a tree whose nodes represent decision points, and whose children represent the options available The leaves of a decision tree represent the possible conclusions that might be drawn A simple decision tree, with yes/no questions, can be modeled by a binary tree Decision trees are useful in diagnostic situations (medical, car repair, etc.)

Decision Trees A simplified decision tree for diagnosing back pain:

Using Trees to Represent Knowledge - TicTacToe As was seen with LearningPlayer in TicTacToe, Trees can be used to represent and capture knowledge. TicTacToe Game Knowledge Tree empty board root +1 +1 child X takes Square 1 X takes Square 2 X takes Square 9 X X X O X O X O takes Square 1 O takes Square 9 Leaf

Key Things to take away: Trees: A tree is a nonlinear structure whose elements form a hierarchy Can be stored in an array using simulated link strategy Trees can be balanced or non-balanced A balanced X-ary tree with n elements has height logxn Four basic tree traversal methods: preorder, inorder, postorder, level order Preorder: visit the node first, then its children left to right Inorder: visit the left child, then the node, then its right child Postorder: visit the children left to right, then visit the node itself Level-Order: visit all nodes in a level left to right, starting with the root Decision Trees can be read from files and used to create an expert system Execution Trees can be used to Describe Recursive program flow Trees can be used to store and update knowledge for training AI programs

Binary Search Trees Binary search tree processing Using BSTs to solve problems BST implementations Strategies for balancing BSTs

Binary Search Trees A search tree is a tree whose elements are organized to facilitate finding a particular element when needed A binary search tree is a binary tree that, for each node n the left subtree of n contains elements less than the element stored in n the right subtree of n contains elements greater than or equal to the element stored in n This is how finds your websites!

How Google finds Websites Nortel Corporate Presentation How Google finds Websites Web Crawlers search the Internet for new websites Read every webpage and every word HUGE files of data – Petabytes! Data Centers process this data And Update Google Search Trees Every webpage, every day A petabyte (symbol: PB) is 1015 bytes of digital information. The prefix peta indicates the fifth power of 1000 and means 1015 in the International System of Units (SI), and therefore 1 petabyte is one quadrillion (short scale) bytes, or 1 billiard (long scale) bytes. 1 PB = 1000000000000000B = 1015 bytes = 1000terabytes. A related unit, the pebibyte (PiB), using a binary prefix, means 10245 bytes, which is more than 12% greater than 10005 bytes (250 bytes = 1125899906842624bytes). © 2004 Nortel

Google and Amazon Data Centers

Data Centers – A Closer Look All this just to update some search trees Some VERY BIG search trees! For more info: Dr. Mohamed Hefeeda Big-Data and Multimedia

Binary Search Trees To determine if a particular value exists in a tree start at the root compare target to element at current node move left from current node if target is less than element in the current node move right from current node if target is greater than element in the current node We eventually find the target or hit the end of a path (target is not found) How to find node with key value 38? Start at Root and compare 38 to 45 38 < 45 so go to left subtree 38 > 12 so go to the right 38 > 15 so go to the right again 38 < 42 so go left 38 > 33 so go right 38 found! Return Object stored at this node 45 12 15 42 33 38

Binary Search Trees The particular shape of a binary search tree depends on the order in which the elements are added The shape may also be dependant on any additional processing performed on the tree to reshape it Binary search trees can hold any type of data, so long as we have a way to determine relative ordering Objects implementing the Comparable interface provide such capability

Binary Search Trees Process of adding an element is similar to finding an element New elements are added as leaf nodes Start at the root, follow path dictated by existing elements until you find no child in the desired direction Then add the new element

BST Element Removal Removing a target in a BST is not as simple as that for linear data structures After removing the element, the resulting tree must still be valid Three distinct situations must be considered when removing an element Node to remove is a leaf Node to remove has one child Node to remove has two children

BST Element Removal Dealing with the situations Node is a leaf: it can simply be deleted Node has one child: the deleted node is replaced by the child Node has two children: an appropriate node is found lower in the tree and used to replace the node Good choice: inorder successor (node that follows in inorder traversal) The inorder successor is guaranteed not to have a left child Thus, removing the inorder successor to replace the deleted node will result in one of the first two situations (it’s a leaf or has one child)

Balancing BSTs As operations are performed on a BST, it could become highly unbalanced (a degenerate tree)

Balancing BSTs AVL trees and red/black trees ensure the BST stays balanced We will explore rotations – operations on binary search trees to assist in the process of keeping a tree balanced Rotations do not solve all problems created by unbalanced trees, but show the basic algorithmic processes that are used to manipulate trees

Balancing BSTs A right rotation can be performed at any level of a tree, around the root of any subtree Corrects an imbalance caused by a long path in the left subtree of the left child of the root To correct the imbalance A: Make the left child element of the root the new root element B: Make the former root element the right child element of the new root C: Make the right child of what was the left child of the former root, the new left child of the former root

Balancing BSTs A left rotation can be performed at any level of a tree, around the root of any subtree Corrects an imbalance caused by a long path in the right subtree of the left child of the root To correct the imbalance A: Make the right child element of the root the new root element B: Make the former root element the left child element of the new root C: Make the left child of what was the right child of the former root, the new right child of the former root

Key Things to take away: Trees – Part 2: CMPT 135 introduces trees and tree algorithms Will go into more depth in CMPT225 and beyond A binary search tree is a binary tree with the added properties that: the left child’s key is less than the parent’s key value the right child’s key is more than the parent’s this is a recursive definition which results in maintaining sorted order and allows for O(logxN) searches Trees are used to provide efficient implementations for other collections Trees are critical to making the Internet Searching work

Nortel Corporate Presentation References: J. Lewis, P. DePasquale, and J. Chase., Java Foundations: Introduction to Program Design & Data Structures. Addison-Wesley, Boston, Massachusetts, 3rd edition, 2014, ISBN 978-0-13-337046-1 © 2004 Nortel