Find in a linked list? first last 7  4  3  8 NULL

Slides:



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

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Marc Smith and Jim Ten Eyck
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Binary Trees.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Binary Trees.
Tree.
Chapter 25 Binary Search Trees
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Problems with Linked List (as we’ve seen so far…)
Trees.
Tree.
CMSC 341 Introduction to Trees.
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
Binary Tree and General Tree
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
8.2 Tree Traversals Chapter 8 - Trees.
Chapter 21: Binary Trees.
Binary Trees.
General Trees & Binary Trees
Binary Trees.
Trees Lecture 9 CS2110 – Fall 2009.
General Trees & Binary Trees
Binary Trees, Binary Search Trees
Binary Trees.
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
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:

Find in a linked list? first last 7  4  3  8 NULL 7  4  3  8 NULL Find 3 in the list Find 6 in the list O(n) Is there a better way? Binary search tree!

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

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.

Fullness and Completeness (for binary tree): 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.

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

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 n, and the number of levels is l, the number of nodes given level l is between 2l-1 and 2l

How ‘bout this one? How many comparisons to find if 1600 is in the tree? How many nodes in the tree? The number of levels is 5, and the number of nodes n is between 2l-1 and 2l – 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 (12 levels to the tree) If a tree has 1048570 nodes, at most it will take 20 steps to find anything in the tree (20 levels) 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 2l-1and 2l (excluding 2l) nodes, it will take at most l steps to find any node in the tree, which is 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