CSE 373 Data Structures Lecture 7

Slides:



Advertisements
Similar presentations
Binary Search Trees CSE 331 Section 2 James Daly.
Advertisements

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.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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.
Tree Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
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 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CC 215 Data Structures Trees
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Data Structures Binary Trees 1.
UNIT III TREES.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
i206: Lecture 13: Recursion, continued Trees
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees CSE 373 Data Structures.
Trees.
CMSC 202 Trees.
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Chapter 20: Binary Trees.
Tree.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

CSE 373 Data Structures Lecture 7 Trees CSE 373 Data Structures Lecture 7

Readings Reading Chapter 4.1-4.3 1/21/03 Trees - Lecture 7

Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories or folders Moves in a game Hierarchies in organizations Can build a tree to support fast searching 1/21/03 Trees - Lecture 7

Tree Jargon root nodes and edges leaves parent, children, siblings ancestors, descendants subtrees path, path length height, depth A B C D E F 1/21/03 Trees - Lecture 7

More Tree Jargon Length of a path = number of edges Depth of a node N = length of path from root to N Height of node N = length of longest path from N to a leaf Depth of tree = depth of deepest node Height of tree = height of root depth=0, height = 2 A B C D depth=1, height =0 E F depth = 2, height=0 1/21/03 Trees - Lecture 7

Definition and Tree Trivia A tree is a set of nodes,i.e., either it’s an empty set of nodes, or it has one node called the root from which zero or more trees (subtrees) descend A tree with N nodes always has N-1 edges (prove it by induction) Two nodes in a tree have at most one path between them 1/21/03 Trees - Lecture 7

Paths Can a non-zero path from node N reach node N again? No. Trees can never have cycles (loops) Does depth (height) of nodes in a non-zero path increase or decrease? Depth always increases in a non-zero path Height always decreases in a non-zero path 1/21/03 Trees - Lecture 7

Implementation of Trees One possible pointer-based Implementation tree nodes with value and a pointer to each child but how many pointers should we allocate space for? A more flexible pointer-based implementation 1st Child / Next Sibling List Representation Each node has 2 pointers: one to its first child and one to next sibling Can handle arbitrary number of children 1/21/03 Trees - Lecture 7

Arbitrary Branching A B C D E F A B C D E F Data FirstChild Sibling Nodes of same depth B C D B C D E F E F Data FirstChild Sibling 1/21/03 Trees - Lecture 7

Binary Trees Every node has at most two children Most popular tree in computer science Given N nodes, what is the minimum depth of a binary tree? At depth d, you can have N = 2d to N = 2d+1-1 nodes 1/21/03 Trees - Lecture 7

Minimum depth vs node count At depth d, you can have N = 2d to 2d+1-1 nodes minimum depth d is (log N) T(n) = (f(n)) means T(n) = O(f(n)) and f(n) = O(T(n)), i.e. T(n) and f(n) have the same growth rate 1 2 3 d=2 N=22 to 23-1 (i.e, 4 to 7 nodes) 4 5 6 7 1/21/03 Trees - Lecture 7

Maximum depth vs node count What is the maximum depth of a binary tree? Degenerate case: Tree is a linked list! Maximum depth = N-1 Goal: Would like to keep depth at around log N to get better performance than linked list for operations like Find 1/21/03 Trees - Lecture 7

A degenerate tree A linked list with high overhead 1 A linked list with high overhead and few redeeming characteristics 2 3 4 5 6 7 1/21/03 Trees - Lecture 7

Traversing Binary Trees The definitions of the traversals are recursive definitions. For example: Visit the root Visit the left subtree (i.e., visit the tree whose root is the left child) and do this recursively Visit the right subtree (i.e., visit the tree whose root is the right child) and do this recursively Traversal definitions can be extended to general (non-binary) trees 1/21/03 Trees - Lecture 7

Traversing Binary Trees Preorder: Node, then Children (starting with the left) recursively + * + A B C D Inorder: Left child recursively, Node, Right child recursively A + B * C + D Postorder: Children recursively, then Node A B + C * D + + D * C + A B 1/21/03 Trees - Lecture 7

Binary Search Trees Binary search trees are binary trees in which all values in the node’s left subtree are less than node value all values in the node’s right subtree are greater than node value Operations: Find, FindMin, FindMax, Insert, Delete What happens when we traverse the tree in inorder? 9 5 94 10 97 96 99 1/21/03 Trees - Lecture 7

Operations on Binary Search Trees How would you implement these? Recursive definition of binary search trees allows recursive routines Call by reference helps too FindMin FindMax Find Insert Delete 9 5 94 10 97 96 99 1/21/03 Trees - Lecture 7

Binary SearchTree 9 9 5 94 5 94 10 97 10 97 96 99 data 96 99 left right 1/21/03 Trees - Lecture 7

Find Find(T : tree pointer, x : element): tree pointer { case { T = null : return null; T.data = x : return T; T.data > x : return Find(T.left,x); T.data < x : return Find(T.right,x) } 1/21/03 Trees - Lecture 7

FindMin Design recursive FindMin operation that returns the smallest element in a binary search tree. FindMin(T : tree pointer) : tree pointer { // precondition: T is not null // ??? } 1/21/03 Trees - Lecture 7

Insert Operation ? Example: Insert 95 Do a “Find” operation for X Insert(T: tree, X: element) Do a “Find” operation for X If X is found  update (no need to insert) Else, “Find” stops at a NULL pointer Insert Node with X there Example: Insert 95 94 ? 10 97 96 99 1/21/03 Trees - Lecture 7

Insert 95 94 94 10 97 10 97 96 99 96 99 95 1/21/03 Trees - Lecture 7

Insert Done with call-by-reference Insert(T : reference tree pointer, x : element) : integer { if T = null then T := new tree; T.data := x; return 1;//the links to //children are null case T.data = x : return 0; T.data > x : return Insert(T.left, x); T.data < x : return Insert(T.right, x); endcase } Advantage of reference parameter is that the call has the original pointer not a copy. 1/21/03 Trees - Lecture 7

Call by Value vs Call by Reference Copy of parameter is used Call by reference Actual parameter is used F(p) p p used inside call of F 1/21/03 Trees - Lecture 7

Delete Operation Delete is a bit trickier…Why? Suppose you want to delete 10 Strategy: Find 10 Delete the node containing 10 Problem: When you delete a node, what do you replace it by? 94 10 97 5 24 11 17 1/21/03 Trees - Lecture 7

Delete Operation Problem: When you delete a node, what do you replace it by? Solution: If it has no children, by NULL If it has 1 child, by that child If it has 2 children, by the node with the smallest value in its right subtree (the successor of the node) 94 10 97 5 24 11 17 1/21/03 Trees - Lecture 7

Delete “5” - No children Find 5 node Then Free the 5 node and NULL the 94 94 Find 5 node 10 97 10 97 5 24 5 24 Then Free the 5 node and NULL the pointer to it 11 11 17 17 1/21/03 Trees - Lecture 7

Delete “24” - One child 94 94 Find 24 node 10 97 10 97 5 24 5 24 Then Free the 24 node and replace the pointer to it with a pointer to its child 11 11 17 17 1/21/03 Trees - Lecture 7

Delete “10” - two children Find 10, Copy the smallest value in right subtree into the node 94 94 10 97 11 97 5 24 5 24 Then (recursively) Delete node with smallest value in right subtree Note: it cannot have two children (why?) 11 11 17 17 1/21/03 Trees - Lecture 7

Then Delete “11” - One child 94 94 Remember 11 node 11 97 11 97 5 24 5 24 Then Free the 11 node and replace the pointer to it with a pointer to its child 11 11 17 17 1/21/03 Trees - Lecture 7

FindMin Solution FindMin(T : tree pointer) : tree pointer { // precondition: T is not null // if T.left = null return T else return FindMin(T.left) } 1/21/03 Trees - Lecture 7