Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,

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.
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 Chapter 8.
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.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 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.
Binary Trees CS 400/600 – Data Structures. Binary Trees2 A binary tree is made up of a finite set of nodes that is either empty or consists of a node.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees.
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.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
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 ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
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,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Starting at Binary Trees
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
Trees Chapter 15.
Chapter 25 Binary Search Trees
Chapter 5 : Trees.
Binary Search Tree (BST)
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Find in a linked list? first last 7  4  3  8 NULL
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees, called the left and right subtrees, which are disjoint from each other and from the root.

Binary Tree Example Definitions: Node – element of the tree Root – top node Subtree – binary tree associated with the root node (left and right) Children – the root node of a node’s subtrees Edge – a link from a node to its children Parent – the node to which the current node is a child Ancestor, descendant

Binary Tree Example Path – a sequence of nodes n1,n2,…,nk s.t. ni is a parent of ni+1 for 1 <= i < k *Length of a path – k -1 *Depth – length of path from root to a node *Height – depth of the deepest node +1 *Level – all nodes of depth d Leaf – node with two empty children Internal node – node with at least one non-empty child *No standard definitions

Full and Complete Binary Trees *Full binary tree: Each node is either a leaf or internal node with exactly two non-empty children. *Complete binary tree: If the height of the tree is d, then all leaves except possibly level d are completely full. The bottom level has all nodes to the left side. These terms can be hard to distinguish. Students will need to remember which is which, since this notation will be used several times during the course.

Full Binary Tree Theorem (1) Theorem: The number of leaves in a non-empty full binary tree is one more than the number of internal nodes. Proof (by Mathematical Induction): Base cases: A full, non-empty binary tree with 0 internal nodes must have 1 leaf node. A full binary tree with 1 internal node must have two leaf nodes. Induction Hypothesis: Assume any full binary tree T containing n internal nodes has n+1 leaves. This theorem is important, because it helps us to calculate space requirements. It tells us how many nodes are internal and how many are leaf. We need to know this if we have separate implementations for internal and leaf nodes

Full Binary Tree Theorem (2) Induction Step: Given tree T with n internal nodes, pick leaf node L and make it internal (by adding two children). T’ is still full. How many internal leaf nodes does it contain? We added two new leaf nodes, but lost one n + 1 + 2 – 1 = n + 2 We created one new internal node n + 1 T’ has n+1 internal nodes, and n+2 leaf nodes, or n’ internal nodes and n’+1 leaf nodes By induction, the theorem holds for n >= 0

Traversals (1) Any process for visiting the nodes in some order is called a traversal. Any traversal that lists every node in the tree exactly once is called an enumeration of the tree’s nodes.

Traversals (2) Preorder traversal: Visit each node before visiting its children. Postorder traversal: Visit each node after visiting its children. Inorder traversal: Visit the left subtree, then the node, then the right subtree. Level-order traversal: Visit all nodes in level 0, then level 1, then level 2, etc. Always visiting the left child first

Recursive Implementation Pre-, post-, and in-order traversals are all easily written as recursive functions (or with a stack) Level-order requires an additional data structure (a queue)

What are the traversals? Give the pre-, post-, in-, and level order traversal of the tree.

Counting Nodes We can count the number of nodes in a tree using a traversal. Key point – the number of nodes in a tree with root node r is the number of nodes in r’s left subtree plus the number of nodes in r’s right subtree plus 1 (for r)

Array Implementation This is a good example of logical representation vs. physical implementation. Since the complete binary tree is so limited in its shape (there is only one possible shape for n nodes), it is reasonable to expect that space efficiency can be achieved.

Array Implementation Parent(r) = (r-1)/2 if r <> 0 and r < n. Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n. Parent(r) = (r-1)/2 if r <> 0 and r < n. Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n.

Binary Tree Implementation Standard implementation (illustrating BinNodePtr class shown earlier). Leaf implementation is identical to internal node implementation, resulting in much wasted space due to null pointers.

Array Implementation This is a good example of logical representation vs. physical implementation. Since the complete binary tree is so limited in its shape (there is only one possible shape for n nodes), it is reasonable to expect that space efficiency can be achieved.

Array Implementation Parent(r) = (r-1)/2 if r <> 0 and r < n. Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n. Parent(r) = (r-1)/2 if r <> 0 and r < n. Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n.

Binary Search Trees BST Property: All elements stored in the left subtree of a node with value K have values < K. All elements stored in the right subtree of a node with value K have values >= K. Lists have a major problem: Either insert/delete on the one hand, or search on the other, must be (n) time. How can we make both update and search efficient? Answer: Use a new data structure.

BST Search Algorithm – search(K,T) for key K and tree T if currnode is NULL, return false if K < currnode->value return search(K,currnode->left) else if K > currnode->value return search(K,currnode->right) else // must equal currnode return true

BST Insert (1)

BST Insert (2) Insert algorithm (no duplicates) insert(K,T) – insert key K into Tree T Find leaf node L where search(K,T) fails If K < L->value add K as L’s left child else add K as L’s right child

BST Remove (1)

BST Remove Remove algorithm – Remove(K,T) – remove key K from tree T find node N with K using search(K,T) if N is a leaf, remove and exit else find the smallest node in the right subtree of N, called n N’ exchange values for N and N’ remove N’

Cost of BST Operations Find: Insert: Delete: All cost depth of the node in question. Worst case: (n). Average case: (log n). All cost depth of the node in question. Worst case: (n). Average case: (log n).

Heaps Heap: Complete binary tree with the heap property: Min-heap: All values less than child values. Max-heap: All values greater than child values. The values are partially ordered. Heap representation: Normally the array-based complete binary tree representation.

Building the Heap (a) (4-2) (4-1) (2-1) (5-2) (5-4) (6-3) (6-5) (7-5) (7-6) (b) (5-2), (7-3), (7-1), (6-1) How to get a good number of exchanges? Induction leads to the following algorithm: Heapify the root’s subtrees, then push the root to the correct level.

Siftdown (1) For fast heap construction: Work from high end of array to low end. Call siftdown for each item. Don’t need to call siftdown on leaf nodes. Siftdown(H) if(H->value > H->left->value and H > H->right->value) then return; else H’ = max(H->left, H->right) swap values(H,H’) siftdown(H’)

Siftdown (2)

Buildheap Cost i=1 Cost for heap construction: log n  (i - 1) n/2i  n. i=1 (i-1) is number of steps down, n/2i is number of nodes at that level.

Remove Max Value RemoveMax(H) -- get the max element off heap H maxelem = H->value; // Max is at the top H->value = last elem->value; shiftdown(H); return maxelem;

Priority Queues (1) A priority queue stores objects, and on request releases the object with greatest value. Example: Scheduling jobs in a multi-tasking operating system. The priority of a job may change, requiring some reordering of the jobs. Implementation: Use a heap to store the priority queue.

Priority Queues (2) To support priority reordering, delete and re-insert. Need to know index for the object in question.

Huffman Coding Trees ASCII codes: 8 bits per character. Fixed-length coding. Can take advantage of relative frequency of letters to save space. Variable-length coding Build the tree with minimum external path weight. Z K F C U D L E 2 7 24 32 37 42 120

Huffman Tree Construction (1)

Huffman Tree Construction (2)

Assigning Codes Letter Freq Code Bits C 32 D 42 E 120 F 24 K 7 L U 37 Z 2 C: 1110 D: 101 E: 0 F: 1111 K: 111101 L: 110 U: 100 Z: 111100

Coding and Decoding A set of codes is said to meet the prefix property if no code in the set is the prefix of another. Code for DEED: Decode 1011001110111101: Expected cost per letter: Code for DEED: 101 0 0 101 Decode: DUCK Expected cost: (1 * 120 + 3 * 121 + 4 * 32 + 5 * 24 + 6 * 9)/ 306 = 785/306 = 2.57