Binary Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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,
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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,
1 Introduction to trees Instructor: Dimitrios Kosmopoulos.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
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
CSCE 210 Data Structures and Algorithms
Tree.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Building Java Programs
The Tree Data Structure
Data Structures Binary Trees 1.
Data Structures and Algorithms
Trees Another Abstract Data Type (ADT)
Trees Tree nomenclature Implementation strategies Traversals
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
CSC 172– Data Structures and Algorithms
CMSC 341 Lecture 13 Leftist Heaps
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Binary Search Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Chapter 8 – Binary Search Tree
TREES General trees Binary trees Binary search trees AVL trees
Find in a linked list? first last 7  4  3  8 NULL
Trees Another Abstract Data Type (ADT)
Trees.
Data Structures and Algorithms for Information Processing
Week nine-ten: Trees Trees.
Chapter 11 Data Compression
Trees Another Abstract Data Type (ADT)
Trees CMSC 202, Version 5/02.
Data Structures – Week #5
CMSC 202 Trees.
CE 221 Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Non-Linear Structures
CE 221 Data Structures and Algorithms
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CS 261 – Data Structures Trees.
Building Java Programs
Binary Tree Properties & Representation
General Tree Concepts Binary Trees
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Trees

Binary Trees Trees with no more than two children per node We call the children of a node left child and right child 11/29/2018

Binary Trees Root Left child Right child Right Sub-tree 11/29/2018

Recursive Definition of Binary Trees A binary tree, T, is either empty or such that: T has a root node; T has two sets of nodes, LT and RT, called the left sub-tree and right sub-tree of T, respectively; LT and RT are binary trees Left Sub-tree Right Sub-tree 11/29/2018

Full Binary Tree Every leaf node has the same depth Every non-leaf node has two children Every non-leaf node has two children Every leaf node has the same depth 11/29/2018

Full Binary Tree Not full Not full Non-leaf node with only one child Leaf nodes with different depths Not full Not full Non-leaf node with only one child 11/29/2018

Number of Nodes in a Full Binary Tree Depth Nodes at Depth Total Nodes 1 2 3 4 n 1 N0=1 2 N1=1+2=3 4 N2=3+4=7 8 N3=7+8=15 16 N4=15+16=31 Nn=Nn-1+2n =2n+1-1 2n 11/29/2018

Only one place to add another node Complete Binary Tree Every level but lowest has all possible nodes If lowest level is not full, all nodes as far left as possible Only one place to add another node 11/29/2018

Binary Tree Applications Expression trees Decision Trees Huffman codes Search trees (next topic) 11/29/2018

Arithmetic Expression Tree Binary tree associated with an arithmetic expression non-leaf nodes: operators leaf nodes: operands Example: arithmetic expression tree for the expression (2  (a - 1)) + (3  b) + 2  (a - 1) 3  b   3 b 2 a-1 a 1 - 11/29/2018

Decision Tree Binary tree associated with a decision process non-leaf nodes: questions with yes/no answer leaf nodes: decisions Example: mail service decision 11/29/2018

Decision Tree YES NO YES NO NO YES Is speed of delivery important? Is contents valuable? YES NO Is contents fragile? NO YES Insured air freight Air freight Rail freight Bus parcel express 11/29/2018

Huffman codes Used for signal compression Suppose we have a message consisting of 5 symbols, e.g. [►♣♣♠☻►♣☼►☻] How can we code this message using 0/1 so the coded message will have minimum length (for transmission or saving!) 5 symbols  at least 3 bits (fixed length codes) For a simple encoding, length of code is 10*3=30 bits 11/29/2018

Huffman codes Uses variable length codes Intuition: Those symbols that are more frequent should have smaller codes For Huffman code, length of encoded message will be ►♣♣♠☻►♣☼►☻ =3*2 +3*2+2*2+3+3=24bits 11/29/2018

Huffman Coding Algorithm 1. Take the two least probable symbols: Assign them longest codes, equal length, differing in last digit 2. Combine these two symbols into a single Virtual symbol, and repeat. 11/29/2018

Huffman Coding Algorithm Symbols/Probability ={ a/0.25 , b/0.25 , c/0.2 , d/0.15 , e/0.15 } 1.0 1 0.55 1 0.45 1 0.3 1 a 0.25 b 0.25 c 0.2 d 0.15 e 0.15 00 10 11 010 011 11/29/2018

Binary Tree Representations There are two ways to represent a Tree: Arrays Linked nodes 11/29/2018

Array Representation of Binary Trees Number the nodes as follows: Number the nodes 1 through 2h – 1. Number by levels from top to bottom. Within a level number from left to right. 1 2 3 4 6 5 7 8 9 11/29/2018 10 11 12 13 14 15

Array Representation of Binary Trees Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent. Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child. Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child. 11/29/2018

Array Representation of Binary Trees Number the nodes as described earlier. The node that is numbered i is stored in tree[i]. b a c d e f g h i j 1 2 3 4 5 6 7 8 9 10 tree[] 5 10 a b c d e f g h i j 11/29/2018

Array Representation of Binary Trees 1 3 c 7 d 15 tree[] 5 10 a - b c 15 d An n node binary tree needs an array whose length is between n+1 and 2n. 11/29/2018

Linked Representation of Binary Trees Each binary tree node is represented as an object whose data type is BTNode. class BTNode { Object data; // any type BTNode left; // left sub-Tree BTNode right; //right sub-Tree } 11/29/2018

Linked Representation of Binary Trees class BTNode { Object data; // any type BTNode left; // left sub-Tree BTNode right; //right sub-Tree } 11/29/2018

Generic Binary node structures Use a generic type parameter for the data class BTNode<E> { E data; BTNode left; BTNode right; } 11/29/2018

BTNode class (assuming Object data) Constructor Public BTNode( Object initialData, BTNode initialLeft, BTNode initialRight ) { data = initialData; left = initialLeft; right = initialRight; } 11/29/2018

BTNode class (assuming Object data) Methods regular accessors and manipulators e.g., public BTNode getLeft() //Gets reference to left child { return left; } public void setData(Object newData) //Set data of this node data = newData; 11/29/2018

BTNode class (assuming Object data) Methods properties of node in tree e.g., public boolean isLeaf() { return (left == null) && (right == null); } 11/29/2018

BTNode class (assuming Object data) Methods tree methods e.g., public void preOrderPrint() public BTNode removeLeftmost() static tree methods e.g., public static treeCopy(BTNode source) public static int treeSize(BTNode root) public static int height() // not in Main 11/29/2018

public BTNode removeLeftmost() starting from root, how do you find the leftmost node? how do you remove the leftmost node and repair the tree structure? 11/29/2018

public BTNode removeLeftmost() How do you find the leftmost node? follow left child links to node with no left child 11/29/2018

public BTNode removeLeftmost() How do you remove the leftmost node and repair the tree structure? leftmost node has no left child attach right child (may be null) to parent of leftmost node as left child 11/29/2018

public BTNode removeLeftmost() { if (left==null) return right; left = left.removeLeftmost(); return this; } 11/29/2018

Traversals of Binary Trees Pre-order In-order Post-order 11/29/2018

Traversals of Binary Trees Pre-order Process the root Process the nodes in the left sub-Tree with a recursive call Process the nodes in the right sub-Tree with a recursive call 1 2 3 11/29/2018

Traversals of Binary Trees Pre-order public void printPreOrder() { System.out.println(data); //1 if (left != null) left.printPreOrder(); //2 if (right != null) right.printPreOrder(); //3 } 1 2 3 11/29/2018

Traversals of Binary Trees In-order Process the nodes in the left sub-Tree with a recursive call Process the root Process the nodes in the right sub-Tree with a recursive call 1 3 2 11/29/2018

Traversals of Binary Trees In-order public void printInOrder() { if (left != null) left.printInOrder(); //1 System.out.println(data); //2 if (right != null) right.printInOrder(); //3 } 1 3 2 11/29/2018

Traversals of Binary Trees Post-order Process the nodes in the left sub-Tree with a recursive call Process the nodes in the right sub-Tree with a recursive call Process the root 1 2 3 11/29/2018

Traversals of Binary Trees Post-order public void printPostOrder() { if (left != null) left.printPostOrder(); //1 if (right != null) right.printInOrder(); //2 System.out.println(data); //3 } 1 2 3 11/29/2018

Recursive method: height calculating height of a tree of BTNode: (=height of root) height: length of path to deepest descendent leaf Recursive design: BASE CASES: height of empty tree is -1 height of one-node tree is 0 RECURSIVE CASES: height is 1 more than height of higher child 3 2 1

Recursive view of the node height calculation: HT = Max (HL + 1, HR + 1) 11/29/2018 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Recursive method: height calculating height of a tree of BTNode: (=height of root) public static int height(BTNode b) { if (b == null) return -1; return 1 + Math.max(height(b.left),height(b.right)); }

Recursive view used to calculate the size, ST, of a tree: ST = SL + SR + 1. 11/29/2018 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Recursive method: size of tree How many nodes in a tree? write recursive method with header: static int size (BTNode b) { if (b == null ) return 0; return 1 + size(b.left) + size(b.right); }