CSC 143 Trees [Chapter 10].

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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 Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Fundamentals of Python: From First Programs Through Data Structures
Marc Smith and Jim Ten Eyck
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
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.
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.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree Data Structures.
TREES Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
353 3/30/98 CSE 143 Trees [Sections , 13.6,13.8]
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
Lecture 9 Binary Trees Trees General Definition Terminology
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Chapter 15.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
Trees.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Applications of Trees.
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

CSC 143 Trees [Chapter 10]

Everyday Examples of Trees Family tree Company organization chart Table of contents Any hierarchical organization of information, representing components in terms of their subcomponents NB: Some of these are not true trees in the CS definition of the term

A Tree root a j b c k m g f e d l i h nodes edges leaves

Tree Terminology Empty tree: tree with no nodes Child of a node u Any node reachable from u by 1 edge pointing away from u Nodes can have zero, one, or more children Leaves have no children If b is a child of a, then a is the parent of b All nodes except root have exactly one parent Root has no parent

Tree Terminology (2) Descendant of a node (recursive definition) P is a descendant of P If C is child of P, and P is a descendant of A, then C is a descendant of A Example: k and m are descendants of j, l is descendant of j, k, and l j k m l Ancestor of a node If D is a descendant of A, then A is an ancestor of D Example: j, k, and l are ancestors of l

Tree Terminology (3) Subtree Any node of a tree, with all of its descendants a b c g j k m Depth (recursive definition) Depth of root node is 0 Depth of any node other than root is one greater than depth of its parent a b c g j k m 1 2

Tree Terminology (4) Height Height of a tree is maximum of all depths of its leaves Warning: Definitions vary Some define depth of the root node as 1.

Binary Trees A binary tree is a tree each of whose nodes has exactly zero, one, or two children Two children are called the left child and right child a b c f e g h d i j k Left child Right child

Binary Tree Data Structure Usually use a variant of a Node: struct BTreeNode { int data; BTreeNode *left; BTreeNode *right; }; Keep a root pointer to the root node Empty tree has a NULL root Note the recursive data structure As the data structure is recursive, algorithms often are recursive as well root left right data

Example: Counting Nodes Base case: Empty tree has zero nodes Recursive case: Nonempty tree has root + nodes in left subtree + nodes in right subtree int CountNodes(BTreeNode *root) { if ( root == NULL ) return 0; // base else return 1+CountNodes(root->left) +CountNodes(root->right); }

Finding the Height Base case: Empty tree has height -1 Recursive case: Nonempty tree has height 1 more than maximum height of left and right subtrees int Height(BTreeNode *root) { if ( root == NULL ) return -1; else return 1+max(Height(root->left), Height(root->right)); }

Analyses What is running time of these algorithms? Base case: O(1) for both Recursive case: O(N) for both, where N is the number of nodes in tree How to write an iterative version? Try it

Recursive Tree Searching How to tell if a data item is in a binary tree? bool Find(BTreeNode *root,int item){ if ( root == NULL ) return false; else if ( root->data == item ) return true; else return (Find(root->left,item) || Find(root->right,item)); }

Recursive Tree Searching (2) What is the running time of this algorithm? Worst case: Has to visit every node in the tree, O(N) Can we do better?

Binary Search Trees [Section 13.3] Properties: No duplicates Prereq: The “greater-than” and “less-than” relations are well-defined for the data values. Sorting constraints: for every node v All data in left subtree of v < data in root All data in right subtree of v > data in root A binary tree with these constraints is called a binary search tree (BST) Given a set of values, there could be many possible BSTs

Examples and Non-Examples 9 4 2 7 3 6 8 1 12 15 14 9 8 2 7 3 5 6 1 12 15 14 Not a Binary Search Tree A Binary Search Tree

Finding an item in a BST Find(root,6) root Find(root,10) root 9 4 2 7 3 6 8 1 12 15 14 root Find(root,10) 9 4 2 7 3 6 8 1 12 15 14 root NULL

Finding an item in a BST (2) If we have a binary search tree, then Find can be done as: bool Find(BTreeNode *root, int item) { if ( root == NULL ) return false; else if (item == root->data) return true; else if (item < root->data) return Find(root->left, item); else return Find(root->right, item); }

Running time of Find Best case: O(1), item is at root Worst case: O(h), where h is height of tree Leads to a question: What is the height of a binary search tree with N nodes? “Perfect” tree (2d nodes at each depth d) is best case: N = 2h+1 - 1 h = log2(N+1) - 1 = O(log N) logarithmic running time 9 4 2 7 12 15 root 10

Running time of Find (2) What if tree isn’t balanced? Worst case is degenerate tree Height = N-1, N is number of nodes 9 12 15 18 root Running time of Find, worst-case, is O(N)

Tables Revisited Using a List or Array to implement Table can be inefficient for searching Could use binary search trees to implement a table (dictionary) Must support Insert and Delete in addition to Find Must maintain BST ordering constraint

Inserting in a BST To insert a new key: Base case: If tree is empty, create new node for item If root holds key, return (no duplicate keys allowed) Recursive case: If key < root’s value, recursively add to left subtree, otherwise to right subtree

Example Add 8, 10, 5, 1, 7, 11 to an empty BST, in that order:

Inserting in a BST (2) //Add data to tree root and return ptr to tree BTreeNode* Insert(BTreeNode* root, int data){ if ( root == NULL ) { BTreeNode *tmp = new BTreeNode; tmp->left = tmp->right = NULL; tmp->data = data; return tmp;} if (data < root->data ) root->left = Insert(root->left, data); else if (data > root->data ) root->right = Insert(root->right, data); return root;}

Example (2) What if we change the order in which the numbers are added? Add 1, 5, 7, 8, 10, 11 to a BST, in that order (following the algorithm):

Complexity of Insert Base case: O(1) How many recursive calls? For each node added, takes O(H), where H is the height of the tree Again, what is height of tree? Balanced trees yields best-case height of O(log N) for N nodes Degenerate trees yield worst-case height of O(N) for N nodes For random insertions, expected height is O(log N) -- true, but not simple to prove

Deleting an Item from a BST Simple strategy: lazy deletion (just mark the node as “deleted”) The hard way. Must deal with 3 cases 1. The deleted item has no children (easy) 2. The deleted item has 1 child (harder) 3. The deleted item has 2 children (hardest) 12 6 15 4 10 20

Deletion (2): Algorithm First find the node (call it N) to delete. If N is a leaf, just delete it. If N has just one child, have N’s parent bypass it and point to N’s child. If N has two children: Replace N’s key with the smallest key K of the right subtree (Recursively) delete the node that had key K (this node is now useless) Note: The smallest key always lives at the leftmost “corner” of a subtree (why?)

Deletion (3): Finding the Node This is the “easy” part: BTreeNode* delItem (int item, BTreeNode* t) { if (t != NULL) { if (item == t->data) t = delNode(t); else if (item > t->data) t->right=delItem(item,t->right); else t->left = delItem(item,t->left); } return t;}

Deletion (4): Deleting the Node BTreeNode* delNode(BTreeNode* t) { if (t->left && t->right) { // 2 children t->data = findMin(t->right); t->right = delItem(t->data, t->right); return t; } else { // 0 or 1 child BTreeNode* rval = NULL; if (t->left) // left child only rval = t->left; else if (t->right)// right child only rval = t->right; delete t; return rval;

Deletion (5): Finding Min All that remains is to figure out how to find the minimum value in a BST Remember, the minimum element lives at the leftmost “corner” of a BST //PRECONDITION: must be called on //non-NULL pointer int findMin(BTreeNode* t){ assert(t != NULL); while (t->left != NULL) t = t->left; return t->data;}

Balanced Search Trees BST operations are dependent on tree height O(log N) for N nodes if tree is balanced O(N) if tree is not Can we ensure tree is always balanced? Yes: Insert and Delete can be modified to reorganize tree if it gets unbalanced Exact details more complicated Results in O(log N) dictionary operations, even in worst case

Tree Traversal Would like to be able to iterate through all nodes in a tree How to have Start(), IsEnd(), NextElement()? What order should nodes be visited in? Top-down? Left-to-right? Bottom-up? How to deal with recursive nature of trees? Visiting nodes of a tree is called tree traversal

Types of Traversal Preorder traversal: Visit the node first Recursively do preorder traversal on its children, in some order Postorder traversal: Recursively do postorder traversals of children, in some order Visit the node last

Inorder (for Binary Trees) With a binary tree, each node has at most two children, called left and right Inorder traversal: Recursively do inorder traversal of left child Then visit the node Then recursively do inorder traversal of right child For preorder and postorder traversals, typically traverse left child before right one

Example of Tree Traversal 9 5 2 7 4 6 8 1 12 10 l1 13 Preorder: Inorder: Postorder:

Another Example What about this tree? Preorder: Inorder: Postorder: 6 3 1 4 2 5 8 7 l3 10 11 12 Inorder: Postorder:

Example Traversal: Printing // print the nodes of tree with // given root void Preorder (BTreeNode *root){ if ( root == NULL ) return; // Visit the node first cout << root->data << " "; Preorder (root->left); Preorder (root->right); }

Example Traversal: Printing void printInOrder(BTreeNode* t){ if (t != NULL) { printInOrder(t->left); cout << t->data << “ “; printInOrder(t->right);} } void printPreOrder(BTreeNode* t){ cout << t->data << " ";

Another Example Use a postorder traversal to return a whole tree to the heap. void deleteTree(BTreeNode* t) { if (t != NULL) { deleteTree(t->left); deleteTree(t->right); delete t;} } Would inorder or preorder work as well?

Analysis of Tree Traversal How many recursive calls? Two for every node in tree (plus one initial call); O(N) in total for N nodes How much time per call? Depends on complexity O(V) of the visit For printing and most other types of traversal, visit is O(1)time Multiply to get total O(N)*O(V) = O(N*V) Does tree shape matter?

Expression Trees [Section 13.5] Programs have a hierarchical structure All statements have a fixed form Statements can be ordered and nested almost arbitrarily (nested if-then-else) Can use a structure known as a syntax tree to represent programs Trees capture hierarchical structure

A Syntax Tree Consider the C++ statement: if ( a == b + 1 ) x = y; else ... statement expression statement ( ) if else equality expression ... ; == LHS expression = var a + const b 1 x y

Syntax Trees Compilers usually use syntax trees when compiling programs Can apply simple rules to check program for syntax errors (the “grammar” in Appendix K of textbook) Easier for compiler to translate and optimize than text file Process of building a syntax tree is called parsing

Binary Expression Trees A binary expression tree is a syntax tree used to represent meaning of a mathematical expression Normal mathematical operators like +, -, *, / Structure of tree defines result Easy to evaluate expressions from their binary expression tree

Example 5 * 3 + (9 - 1) / 4 - 1 5 3 * 9 1 - 4 / +

Traversing Expression Trees Traverse in preorder for prefix notation - + * 5 3 / - 9 1 4 1 Traverse in inorder for infix notation 5 * 3 + 9 - 1 / 4 - 1 Note that operator precedence may be wrong without adding parentheses at every step (((5*3) + ((9 - 1) / 4)) - 1) Traverse in postorder for postfix notation 5 3 * 9 1 - 4 / + 1 -

Evaluating Expressions Easy to evaluate an expression from its binary expression tree Use a postorder traversal Recursively evaluate the left and right subtrees and store those values Apply operator at root to stored values Much like using a stack to evaluate postfix notation

Trees Summary Tree as new hierarchical data structure Recursive definition and recursive data structure Tree parts and terminology Made up of nodes Root node, leaf nodes Children, parents, ancestors, descendants Depth of node, height of tree Subtrees

Trees Summary (2) Binary Trees Either 0, 1, or 2 children at any node Recursive functions to manipulate them Binary Search Trees Binary Trees with ordering invariant Recursive BST search Recursive Insert, Delete functions O(H) operations, where H is height of tree O(log N) for N nodes in balanced case O(N) in worst case

Trees Summary (3) Tree Traversals Preorder traversal Postorder traversal Binary Tree Traversals Inorder traversal Expression and Syntax Trees