Trees CMSC 202, Version 5/02.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
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.
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.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
Data Structures and Design in Java © Rick Mercer
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
AA Trees.
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Data Structures Binary Trees 1.
UNIT III TREES.
B+ Tree.
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search Trees
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Trees 7/14/2009.
Find in a linked list? first last 7  4  3  8 NULL
CMSC 341 Introduction to Trees.
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
Data Structures – Week #5
CMSC 202 Trees.
B- Trees D. Frey with apologies to Tom Anastasio
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Binary Trees.
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees CMSC 202, Version 5/02

Tree Basics A tree is a set of nodes. A tree may be empty (i.e., contain no nodes). If not empty, there is a distinguished node, r, called the root and zero or more non-empty subtrees T1, T2, T3,….Tk, each of whose roots is connected by a directed edge from r. Trees are recursive in their definition and, therefore, in their implementation. CMSC 202, Version 5/02

A General Tree A B G K L C D H I J E F CMSC 202, Version 5/02

Tree Terminology Tree terminology takes its terms from both nature and genealogy. A node directory below the root, r, of a subtree is a child of r, and r is called its parent. All children with the same parent are called siblings. A node with one or more children is called an internal node. A node with no children is called a leaf or external node. CMSC 202, Version 5/02

Tree Terminology (con’t) A path in a tree is a sequence of nodes, (N1, N2, … Nk) such that Ni is the parent of Ni+1 for 1 <= i <= k. The length of this path is the number of edges encountered (k – 1). If there is a path from node N1 to N2, then N1 is an ancestor of N2 and N2 is a descendant of N1. CMSC 202, Version 5/02

Tree Terminology (con’t) The depth of a node is the length of the path from the root to the node. The height of a node is the length of the longest path from the node to a leaf. The depth of a tree is the depth of its deepest leaf. The height of a tree is the height of the root. True or False – The height of a tree and the depth of a tree always have the same value. CMSC 202, Version 5/02

Tree Storage First attempt - each tree node contains The data being stored We assume that the objects contained in the nodes support all necessary operations required by the tree. Links to all of its children Problem: A tree node can have an indeterminate number of children. So how many links do we define in the node? CMSC 202, Version 5/02

First Child, Next Sibling Since we can’t know how many children a node can have, we can’t create a static data structure -- we need a dynamic one. Each node will contain The data which supports all necessary operations A link to its first child A link to a sibling CMSC 202, Version 5/02

First Child, Next Sibling Representation To be supplied in class CMSC 202, Version 5/02

Tree Traversal Traversering a tree means starting at the root and visiting each node in the tree in some orderly fashion. The purpose of traversing a tree is to enable us to perform operations such as: print all data stored in the tree search for a particular data item in the tree compute the sum of all corresponding data items in the tree (e.g., sum all student exam scores where there is one score per node) CMSC 202, Version 5/02

Breadth-First Tree Traversals Start at the root. Visit all the root’s children. Then visit all the root’s grand-children. Then visit all the roots great-grand-children, and so on. This traversal goes down by levels. A queue can be used to implement this algorithm. CMSC 202, Version 5/02

BF Traversal Pseudocode Create a queue, Q, to hold tree nodes Q.enqueue (the root) while (the queue is not empty) Node N = Q.dequeue( ) for each child, X, of N Q.enqueue (X) The order in which the nodes are dequeued is the BF traversal order. CMSC 202, Version 5/02

Depth-First Traversal Start at the root. Choose a child to visit. Visit all of that child’s children. Visit all of that child’s children’s children, and so on. This traversal goes down a path until the end, then comes back and does the next path. A stack can be used to implement this algorithm. CMSC 202, Version 5/02

DF Traversal Pseudocode Create a stack, S, to hold tree nodes S.push (the root) While (the stack is not empty) Node N = S.pop ( ) for each child, X, of N S.push (X) The order in which the nodes are popped is the DF traversal order. CMSC 202, Version 5/02

Performance of BF and DF Traversals What is the asymptotic performance of breadth-first and depth-first traversals on a general tree? CMSC 202, Version 5/02

Binary Trees A binary tree is a tree in which each node may have at most two children and the children are designated as left and right. A full binary tree is one in which each node has either two children or is a leaf. A perfect binary tree is a full binary tree in which all leaves are at the same level. CMSC 202, Version 5/02

A Binary Tree CMSC 202, Version 5/02

A binary tree? A full binary tree? CMSC 202, Version 5/02

A binary tree? A full binary tree? A perfect binary tree? CMSC 202, Version 5/02

Binary Tree Traversals Because nodes in binary trees have at most two children (left and right), we can write specialized versions of BF and DF traversals. These are called In-order traversal Pre-order traversal Post-order traversal CMSC 202, Version 5/02

In-Order Traversal At each node visit my left child first visit me visit my right child last 8 5 3 9 12 7 15 6 2 10 CMSC 202, Version 5/02

In-Order Traversal Code void inOrderTraversal(Node *nodePtr) { if (nodePtr != NULL) { inOrderTraversal(nodePtr->leftPtr); cout << nodePtr->data << endl; inOrderTraversal(nodePtr->rightPtr); } CMSC 202, Version 5/02

Pre-Order Traversal At each node visit me first visit my left child next visit my right child last 8 5 3 9 12 7 15 6 2 10 CMSC 202, Version 5/02

Pre-Order Traversal Code void preOrderTraversal(Node *nodePtr) { if (nodePtr != NULL) { cout << nodePtr->data << endl; preOrderTraversal(nodePtr->leftPtr); preOrderTraversal(nodePtr->rightPtr); } CMSC 202, Version 5/02

Post-Order Traversal At each node visit my left child first visit my right child next visit me last 8 5 3 9 12 7 15 6 2 10 CMSC 202, Version 5/02

Post-Order Traversal Code void postOrderTraversal(Node *nodePtr) { if (nodePtr != NULL) { postOrderTraversal(nodePtr->leftPtr); postOrderTraversal(nodePtr->rightPtr); cout << nodePtr->data << endl; } CMSC 202, Version 5/02

Binary Tree Operations Recall that the data stored in the nodes supports all necessary operators. We’ll refer to it as a “value” for our examples. Typical operations: Create an empty tree Insert a new value Search for a value Remove a value Destroy the tree CMSC 202, Version 5/02

Creating an Empty Tree Set the pointer to the root node equal to NULL. CMSC 202, Version 5/02

Inserting a New Value The first value goes in the root node. What about the second value? What about subsequent values? Since the tree has no properties which dictate where the values should be stored, we are at liberty to choose our own algorithm for storing the data. CMSC 202, Version 5/02

Searching for a Value Since there is no rhyme or reason to where the values are stored, we must search the entire tree using a BF or DF traversal. CMSC 202, Version 5/02

Removing a Value Once again, since the values are not stored in any special way, we have lots of choices. Example: First, find the value via BF or DF traversal. Second, replace it with one of its descendants (if there are any). CMSC 202, Version 5/02

Destroying the Tree We have to be careful of the order in which nodes are destroyed (deallocated). We have to destroy the children first, and the parent last (because the parent points to the children). Which traversal (in-order, pre-order, or post-order) would be best for this algorithm? CMSC 202, Version 5/02

Giving Order to a Binary Tree Binary trees can be made more useful if we dictate the manner in which values are stored. When selecting where to insert new values, we could follow this rule: “left is less” “right is more” Note that this assumes no duplicate nodes (i.e., data). CMSC 202, Version 5/02

Binary Search Trees A binary tree with the additional property that at each node, the value in the node’s left child is smaller than the value in the node itself, and the value in the node’s right child is larger than the value in the node itself. CMSC 202, Version 5/02

A Binary Search Tree 50 57 42 67 30 53 22 34 CMSC 202, Version 5/02

Searching a BST Searching for the value X, given a pointer to the root If the value in the root matches, we’re done. If X is smaller than the value in the root, look in the root’s left subtree. If X is larger than the value in the root, look in the root’s right subtree. A recursive routine – what’s the base case? CMSC 202, Version 5/02

Inserting a Value in a BST To insert value X in a BST Proceed as if searching for X. When the search fails, create a new node to contain X and attach it to the tree at that node. CMSC 202, Version 5/02

Inserting 100 38 56 150 20 40 125 138 90 CMSC 202, Version 5/02

Removing a Value From a BST Non-trivial Three separate cases: node is a leaf (has not children) node has a single child node has two children CMSC 202, Version 5/02

Removing 100 150 50 70 120 30 130 20 40 60 80 85 55 65 53 57 CMSC 202, Version 5/02

Destroying a BST The fact that the values in the nodes are in a special order doesn’t help. We still have to destroy each child before destroying the parent. Which traversal can we use? CMSC 202, Version 5/02

Performance in a BST What is the asymptotic performance of: insert search remove Is the performance of insert, search, and remove for a BST improved over that for a plain binary tree? If so, why? If not, why not? CMSC 202, Version 5/02