Chapter 10 Trees and Binary Trees Part 2. ?Traversal level by level.

Slides:



Advertisements
Similar presentations
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
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.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Data Structures Using C++1 Chapter 11 Binary Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
 B+ Tree Definition  B+ Tree Properties  B+ Tree Searching  B+ Tree Insertion  B+ Tree Deletion.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
COSC 2007 Data Structures II Chapter 15 External Methods.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
2-3 Trees, Trees Red-Black Trees
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Starting at Binary Trees
Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
1 More Trees Trees, Red-Black Trees, B Trees.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Data Structures Using C++ 2E Chapter 11 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.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Trees Chapter 15.
AA Trees.
BST Trees
Binary Search Tree (BST)
ITEC 2620M Introduction to Data Structures
Binary Tree and General Tree
Wednesday, April 18, 2018 Announcements… For Today…
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Find in a linked list? first last 7  4  3  8 NULL
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Self-Balancing Search Trees
1 Lecture 13 CS2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Chapter 10 Trees and Binary Trees Part 2

?Traversal level by level

Definitions

Rooted trees with four vertices (Root is at the top of tree.)

Ordered trees with four vertices

Implementations of Ordered Trees Multiple links first child and next sibling links Correspondence with binary trees datachild1Child2 … datafirst childNext sibling

Conversion (from forest/Orchard to binary tree Corresponded binary tree respectively Corresponded binary tree

Huffman Tree( 哈夫曼树 ) Path Length ( 路径长度 ) Path Length of the binary tree Total length of all from leaves to root

Weighted Path Length (WPL, 带权路径长度 ) 树的带权路径长度是树的各叶结点所带的权值 与该结点到根的路径长度的乘积的和。

How about the WPLof the following binary trees

Huffman tree A (extended) binary tree with minimal WPL 。 A (extended) binary tree with minimal WPL 。

Processes of huffman tree

Huffman coding Compression suppose we have a message : CAST CAST SAT AT A TASA CAST CAST SAT AT A TASA alphabet ={ C, A, S, T } , frequency of them ( 次 数 ) are W = { 2, 7, 4, 5 } 。 alphabet ={ C, A, S, T } , frequency of them ( 次 数 ) are W = { 2, 7, 4, 5 } 。 equal length coding first case equal length coding A : 00 T : 10 C : 01 S : 11 A : 00 T : 10 C : 01 S : 11 Total coding length of the message is ( ) * 2 = 36. ( ) * 2 = 36.

A : 0 T : 10 C : 110 S : 111 A : 0 T : 10 C : 110 S : 111 Total length of huffman coding : 7*1+5*2+( 2+4 )*3 = 35 Which is shorter than that of equal length coding 。 霍夫曼编码是一种无前缀编码。解码时不会混淆。 Huffman tree ??

Binary Search Trees Can we find an implementation for ordered lists in which we can search quickly (as with binary search on a contiguous list) and in which we can make insertions and deletions quickly (as with a linked list)?

DEFINITION A binary search tree is a binary tree that is either empty or in which the data entry of every node has a key and satisfies the conditions: 1. The key of the left child of a node (if it exists) is less than the key of its parent node. 2. The key of the right child of a node (if it exists) is greater than the key of its parent node. 3. The left and right subtrees of the root are again binary search trees. We always require: No two entries in a binary search tree may have equal keys.

different views We can regard binary search trees as a new ADT. We may regard binary search trees as a specialization of binary trees. We may study binary search trees as a new implementation of the ADT ordered list.

The Binary Search Tree Class

Recursive auxiliary function: template Binary node *Search tree :: search for node( Binary node * sub root, const Record &target) const { if (sub root == NULL || sub root->data == target) return sub root; else if (sub root->data < target) return search for node(sub root->right, target); else return search for node(sub root->left, target); }

Nonrecursive version: template Binary node *Search tree :: search for node( Binary node *sub root, const Record &target) const { while (sub root != NULL && sub root->data != target) if (sub root->data right; else sub root = sub root->left; return sub root; }

Public method for tree search: template Error code Search tree :: tree search(Record &target) const { Error code result = success; Binary node *found = search for node(root, target); if (found == NULL) result = not present; else target = found->data; return result; }

Binary Search Trees with the Same Keys

search

Creating a BST by insertion

insertion

Method for Insertion

Analysis of insertion

Treesort When a binary search tree is traversed in inorder, the keys will come out in sorted order. This is the basis for a sorting method, called treesort: Take the entries to be sorted, use the method insert to build them into a binary search tree, and then use inorder traversal to put them out in order.

Treesort

Comparison First advantage of treesort over quicksort: The nodes need not all be available at the start of the process, but are built into the tree one by one as they become available. Second advantage: The search tree remains available for later insertions and removals. Drawback: If the keys are already sorted, then treesort will be a disasteróthe search tree it builds will reduce to a chain. Treesort should never be used if the keys are already sorted, or are nearly so.

Removal from a Binary Search Tree

Removal from a Binary Search Tree (continue)

Height Balance: AVL Trees Definition: An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are again AVL trees. With each node of an AVL tree is associated a balance factor that is left higher, equal, or right higher according, respectively, as the left subtree has height greater than, equal to, or less than that of the right subtree.

Example AVL trees

Example non-AVL trees

Insertions into an AVL tree

Rotations of an AVL Tree

Double Rotation

Deletion with no rotations

Deletion with single left rotations

Deletion with double rotation

Worst-Case AVL Trees

Fibonacci Trees

Analysis of Fibonacci Trees

Multiway Search Trees An m-way search tree is a tree in which, for some integer m called the order of the tree, each node has at most m children.

Balanced Multiway Trees (B-Trees)

B-Tree Example

Insertion into a B-Tree In contrast to binary search trees, B-trees are not allowed to grow at their leaves; instead, they are forced to grow at the root. General insertion method: 1. Search the tree for the new key. This search (if the key is truly new) will terminate in failure at a leaf. 2. Insert the new key into to the leaf node. If the node was not previously full, then the insertion is finished.

Insertion into a B-Tree 3. When a key is added to a full node, then the node splits into two nodes, side by side on the same level, except that the median key is not put into either of the two new nodes. 4. When a node splits, move up one level, insert the median key into this parent node, and repeat the splitting process if necessary. 5. When a key is added to a full root, then the root splits in two and the median key sent upward becomes a new root. This is the only time when the B-tree grows in height.

Growth of a B-Tree

Deletion from a B-Tree