Tree (new ADT) Terminology: A tree is a collection of elements (nodes)

Slides:



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

Trees Types and Operations
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.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Trees.
Trees Chapter 15.
Binary Trees.
Tree.
Week 6 - Wednesday CS221.
Problems with Linked List (as we’ve seen so far…)
Trees.
Tree.
Trees.
Lecture 18. Basics and types of Trees
ITEC 2620M Introduction to Data Structures
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
Ch. 11 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.
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
Binary Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
AVL Trees: AVL Trees: Balanced binary search tree
Binary Trees.
Trees Lecture 9 CS2110 – Fall 2009.
General Trees & Binary Trees
Binary Trees, Binary Search Trees
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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 list have? Each node has exactly one predecessor (called the parent) Except the starting node, called the root Links from node to its successors are called branches Nodes with same parent are siblings Nodes with no children are called leaves

Tree We also use words like ancestor and descendent Pets is the parent of Dogs and Cats Poodle and Beagle are the children of Dogs Poodle, Beagle, Persian, and Siamese are descendents of Pets, Pets is the ancestor of Poodle, Beagle, Persian, and Siamese

Tree Terminology Subtree of a node: Depth of a node: 1 2 3 4 A tree whose root is a child of that node Depth of a node: A measure of its distance from the root: Depth of the root = 0 Depth of other nodes = 1 + depth of parent 1 2 3 4

Binary Trees Binary tree: a node has at most 2 nonempty subtrees Set of nodes T is a binary tree if either of these is true: T is empty Root of T has two subtrees, both binary trees (Notice that this is a recursive definition) class Node { string data; node *left; node *right; }; This is a binary tree

Fullness and Completeness: Trees grow from the top down New values inserted in new leaf nodes In a full tree, every node has 0 or 2 non-null children A complete tree of height h is filled up to depth h-1, and, at depth h, any unfilled nodes are on the right.

Example of binary tree: Simple sentence parsing: used to model relationship between words in a sentence: Used for topic determination Learning tools Language translation Etc.

Huffman Coding Huffman codes used to compress information Basic idea: JPEGs (images) use Huffman as part of their compression process Basic idea: Store most frequently occurring info with a shorter representation than info that occurs less frequently. E.g., Represent e with:010 Represent z with: 1100001010 Each file will be different, based on frequencies

“double double toil and trouble” 3 4 2 1 D O U B L E T N 1 2 3 4 N T D U B E O L 3 4 D U B E O L 3 4 6 U B E O L 3 4 6 E O L 3 D 1 N 2 T 3 3 3 D 3 U 3 B 1 N 2 T 1 N 2 T 4 6 7 L 3 3 D 3 U 3 B 3 E 4 O 1 N 2 T

4 6 7 L 6 7 10 10 13 3 3 D 3 U 3 B 3 E 4 O 3 U 3 B 3 E 4 O 4 L 6 4 L 6 6 7 1 N 2 T 3 3 D 23 3 D 3 U 3 B 3 E 4 O 3 1 N 2 T 1 N 2 T 10 13 Code: To get code, start at root. Every left, add a 1, every right, add a 0. You get: L: 00 N:0100 T:0101 D:011 U:100 B:101 E:110 O:111 4 L 6 6 7 3 U 3 E 4 O 3 3 D 3 B 1 N 2 T

Examples: Huffman Binary Tree Represents Huffman codes for characters appearing in a file or stream Code may use different numbers of bits to encode different characters Code for b = 100000 Code for w = 110001 Code for s = 0011 Code for e = 010

Node Class Definition for a Tree: class NodeT { int data; NodeT *left; NodeT *right; NodeT *parent; //optional: int height; // height up from lowest descendent leaf public: NodeT(int x); ~NodeT(); void printNodeT(); };

Traversals of Binary Trees Can walk the tree and visit all the nodes in the tree in order This process is called tree traversal Three kinds of binary tree traversal: Preorder e.g., copying Inorder – e.g., bst Postorder –e.g., deleting or freeing nodes order in which we visit the subtree root with respect to its children Why do we worry about traversing in different orders? Trees represent data – we may want to find or represent data in different ways depending on the data and the solution we are looking for

Tree Traversal: Preorder Used for copying: Visit root, traverse left, traverse right Preorder: a, b, d, g,e,h, c, f, i, j <- left right ->

Tree Traversals: InOrder Used for creating sorted list: Traverse left (go till no more lefts), visit root, traverse right (always go to the left if there’s a left) Inorder (left, center, right) d, g, b, h, e, a, i, f, j, c <- left right ->

Tree Traversal: Postorder Used for deleting: Traverse left, traverse right, visit root Postorder: g, d, h, e, b, i, j, f, c, a <- left right ->

Pre? In? Post? 36 16 48 15 21 40 11 23 44 41 PRE: 36 16 15 11 21 23 48 40 44 41 IN: 11 15 16 21 23 36 40 41 44 48 POST: 11 15 23 21 16 41 44 40 48 36 <- left right ->

Given this code, what is printed out? void BST::printTreeio(NodeT *n) { //recursive function if (n == NULL) { return; } else { printTreeio(n->left); n->printNode(); printTreeio(n->right); 36 16 48 15 21 40 11 23 44 41 <- left right ->

Binary Search Tree: A tree in which the data in every left node is less than the data in its parent, and the data in the right node is greater than the data in its parent. Inserting/Finding Data: Data is inserted(found) by comparing the new data to the root We move to either the left or the right child of the root depending on whether the data we’re looking for/inserting is less than or greater than the root. The child, in essence, becomes the root of the subtree the process continues until data is found or the child is null if inserting, the data is inserted If child is null and finding, data not in tree 8,3,6,10,7,14,1,13,4 <- left right ->

Binary Search Tree Inserting: 17, 13, 26,12, 15, 11, 14, 28, 33, 32 17 13 26 12 15 28 11 14 33 32 <- left right ->

BST: Inserting pseudocode: Bool InsertIt(int x): // iterative version if root is NULL: set root to new Node, with data x else { set n to be the root while n is not NULL { if x < n’s data if n’s left child is NULL set n’s left child to new Node with data x set the new node’s parent to be n; return True otherwise set n to be n’s left child else if x > n’s data if n’s right child is NULL set n’s right child to new Node with data x otherwise set n to be n’s right child else return false; //x already in tree } Bool InsertRec(int x, Node n): // recursive version if n is NULL: set root to new Node, with data x else { if x < n’s data if n’s left child is NULL set n’s left child to new Node with data x set the new node’s parent to be n; return True otherwise call InsertRec with x and n’s left child else if x > n’s data if n’s right child is NULL set n’s right child to new Node with data x otherwise call InsertRec with x and n’s right child else return false; }

Removing: case 1 Search for node and then, if it’s in the tree, remove it. 3 cases: Node to be removed has no children: Just delete the node, and make the parent point to NULL (must keep track of parent) <- left right ->

Removing: case 2 Node to remove has one child: Parent points to node’s child Delete node <- left right ->

Removing: case 3 Node has 2 children. Remember, we must maintain the BST properties when we remove a node What if we want to remove 12 and we have the following tree: 7 10 7 10 7 10 Got here. Find the MINIMUM VALUE IN THE RIGHT SUBTREE Replace the node with the value found Remove the value from the right subtree Is the tree still a binary search tree? <- left right ->

Remove rat from the tree

Remove rat from the tree shaven

How many steps? How many comparisons to find if 12 is in the tree? How many nodes in the tree? The number of nodes is between 2n-1 and 2n – what’s n?

How ‘bout this one? How many comparisons to find if 1600 is in the tree? How many nodes in the tree? The number of nodes is between 2n-1 and 2n – what’s n?

Analysis: If a binary search tree has 2044 nodes, in the best case, how many layers does it have? 11 How many steps (at most) to find any node in the tree (best case)? If a tree has 8100 nodes, at most it will take 12 steps to find anything in the tree If a tree has 1048570 nodes, at most it will take 20 steps to find anything in the tree If a tree has 2,147,483,640 nodes, it will take at most 31 steps to find/insert/remove anything in the tree. WOW! Can you see how, the more nodes, the bigger the savings for finding/inserting/deleting from a binary search tree?

Create a tree by inserting the following data [1 | 2 | 4 | 7 | 13 | 24 | 29 | 32 | 36 | 42 | 55] 1 2 4 7 13 24 29 32 There are 11 nodes in this tree: How many comparisons to find 55 in this tree? What did we just learn about binary search trees and efficiency? 36 42 55 <- left right ->

BST Example If there are between 2n-1and 2n (excluding 2n) nodes, it will take at most n steps to find any node in the tree O(log n) At every comparison, we should eliminate half of the nodes necessary for future comparison … if tree is balanced Balanced: at any node, the height of the left subtree and the height of the right subtree differ at most by 1 How do we make sure a tree stays balanced (in a reasonable amount of time? 24 36 29 55 32 42 13 2 4 1 7 17 3 62

AVL Trees: AVL Trees: Balanced binary search tree Ensures that every node can be reached in O(log n) or less Named after inventors: Adelson-Velskii and Landis To stay balanced, the AVL tree maintains the following properties: For every node, the height of the left child and the height of the right child differ by no more than 1 Every subtree is an AVL tree <- left right ->

AVL Trees: Searching: done like a binary tree Traversal: done like a binary tree The sticky ones: Insertion and Deletion of nodes At each node n, we keep track of its “balance” n->leftchild->height - n->rightchild->height 0 (3 – 3) 0 (2 – 2) 1(2 – 1) 0 (0 – 0) 0 (1 – 1) 1 (1 – 0) -1 (0 – 1) 0(0-0) 0 (0 – 0) 0 ( 0 – 0) 0 (0 – 0) <- left right ->

A: What is the height of each node? B: What is the balance of each node? C: Is this an AVL tree? D: What changes if you insert -9 H:4 B:-1 B:0 H:2 H:3 H:3 B:-1 B:1 B:-2 H:1 H:2 H:2 H:1 B:1 B:0 B:1 B:0 H:1 -9 B:0 H:1 B:0