Binary Tree.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
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, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
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.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Tree.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
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.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary Search Trees Chapter 7 Objectives
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
ITEC324 Principle of CS III
Tree.
Fundamentals of Programming II Introduction to Trees
Chapter 5 : Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Trees Another Abstract Data Type (ADT)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Tonga Institute of Higher Education
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Find in a linked list? first last 7  4  3  8 NULL
Trees Another Abstract Data Type (ADT)
Trees Another Abstract Data Type (ADT)
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
CSC 205 – Java Programming II
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Tree

Tree Definition Tree is a finite set T which satisfies the following conditions. It has one root. Except the root, nodes are divided into n subsets, T1, T2, T3, …, Tn. Each subset is also a tree. Thus, tree is defined recursively.

Quick Review: Basic terms (1) L F C G D H I J M  Level 0  Level 1  Level 2  Level 3 Node: a basic component which can contain data and references to 0 or more other nodes. (ex) A~M Root: the node on the level 0. (ex) A Level

Quick Review: Basic Terms (2) L F C G D H I J M  Level 0  Level 1  Level 2  Level 3 Subtree: a node and all of its descendents (ex) {B, E, F, K, L}, {E, K, L}, … Leaf (or Terminal node): a node of which degree is 0. (ex) F, G, I, J, K, L, M

Quick Review: Basic terms (3) L F C G D H I J M  Level 0  Level 1  Level 2  Level 3 Interior node (or Nonterminal node): a node of which degree is greater than 0. Child/children Parent Siblings: nodes which have the same parent node. (ex) K and L are siblings. H, I and J are siblings.

Quick Review: Basic terms (4) L F C G D H I J M  Level 0  Level 1  Level 2  Level 3 Ancestors: the ancestors of a node N are all the nodes on the path from the root to the node N. (ex) the ancestors of node L are A, B, E. Descendants Forest: a set of separated trees

Quick Review: Binary Tree [Definition] If every node in a tree can have at most two children, the tree is called a binary tree. [Term] Left child Right child

Quick review: Binary Search Tree (BST) Definition A binary tree in which every node is greater than its left child and less than its right child, if the left and/or right child exists. class Node BST operations find: p378 insert (or add): always add new leaf. p380 traverse min and max in tree (or subtree) delete

Quick Review: Traverse in details (1) inorder traversal (in a recursive way) call itself to traverse the node’s left subtree visit the node call itself to traverse the node’s right subtree

Quick Review: Traverse in details (2) preorder traversl (in a recursive way) visit the node call itself to traverse the node’s left subtree call itself to traverse the node’s right subtree

Quick Review: Traverse in details (3) postorder traversl (in a recursive way) call itself to traverse the node’s left subtree call itself to traverse the node’s right subtree visit the node Example: p386  infix notation, prefix notation, postfix notation

Quick Review: Traverse in details (4) Infix: A*(B+C) Prefix: *A+BC Postfix: ABC+*

Delete operation in details (1) Deleting a node is the most complicated common operation required for binary search trees. There are three possible cases when you delete a node. The node to be deleted is a leaf (had no children). : easy The node to be deleted has one child. The node to be deleted has two children. : quite complicated

Delete operation in details (2) Case 1: The node to be deleted is a leaf (had no children). [How?] Change the appropriate child field in the node’s parent to point to null. Java will automatically collect the garbage. Before deletion After deletion

Delete operation in details (3) Case 2: The node to be deleted has one child. [How?] Connect its parent directly to its child. In other words, change the appropriate reference in the parent (leftChild or rightChild) to point to the deleted node’s child.

Delete operation in details (4) Case 3: The node to be deleted has two children. [Definition] Inorder successor: For each node, the node with the next-highest key is called its inorder successor, or simply its successor. [How to delete?] Replace the node to be deleted with its inorder successor and then delete it.  fig 8.16 in page 394 Finding the successor: fig 8.17 in page 395

Delete operation in details (4) Case 3 (continued) Successor can be the right child of the delNode: fig 8.18 in page 396 Deletion: fig 8.19 in page 399 Unplug current from the rightChild field of its parent (or leftChild field if appropriate), and set this filed to point to successor. Unplug current’s left child from current, and plug it into the leftChild field of successor.

Delete operation in details (5) Case 3 (continued) Successor can be a left descendant of the right child of delNode Deletion: fig 8.20 in page 400 Plug the right child of successor into the leftChild field of the successor’s parent. Plug the right child of the node to be deleted into the rightChild field of successor. Unplug current from the rightChild field of its parent, and set this field to point to successor. Unplug current’s left child from current, and plug it into the leftChild field of successor.

The efficiency of Binary Search Trees Time Complexity of Binary Search Tree: O(log N)

Tree Represented as Arrays the node array[index]’s left child = array[2*index + 1] right child = array[2*index + 2] parent = array[(index -1) /2] fig 8.21 in page 404

Duplicate Keys Possible – insert() method in the following tree.java will insert a duplicate key as the right child of its twin. However, it is recommended to forbid duplicate keys.

Binary Search Tree Java code of BST operations

The Huffman Code (1) Discovered by David Huffman in 1952 The Huffman code uses a binary tree and is applied to data compression. (Ex) [Character Codes] Some ASCII Codes Character Decimal Binary A B C … X Y Z 65 66 67 88 89 90 01000001 01000010 01000011 01011001 01011010 01011100

The Huffman Code (2) Frequency Table (example) Character Count A E I S Space Linefeed 2 3 6 1 4

22 9 13 6 4 5 7 S sp 3 4 2 3 I A 2 2 1 2 Y T E 1 1 If U

Creating the Huffman Code (3) Huffman Code: Textbook (pages 416~420) Character Code A E I S T U Y Space Linefeed 010 1111 110 10 0110 01111 1110 00 01110