§4 Additional Binary Tree Operations

Slides:



Advertisements
Similar presentations
-CHINESE TIME (中文时间): Free Response idea: 你周末做了什么?
Advertisements

第7章 樹與二元樹 (Trees and Binary Trees)
動畫與遊戲設計 Data structure and artificial intelligent
广德二中2006届高考 英语专题复习 单项填空 答题指导.
第5章 树( Tree )和二叉树 5.1 树的基本概念 5.2 二叉树 5.3 遍历二叉树和线索二叉树 5.4 树和森林
数据结构 第六章 树与二叉树 深圳大学计算机系 蔡茂国.
第五章 树 东南大学计算机学院 方效林 本课件借鉴了清华大学殷人昆老师 和哈尔滨工业大学张岩老师的课件.
第8章 查找 数据结构(C++描述).
Minimum Spanning Trees
Chap4 Tree.
Tree(樹) 什麼是「樹」? 「樹」的範例 「樹」的定義 「樹」的表示法.
第4章 鏈結串列(Linked Lists) 4-1 動態記憶體配置-(6) 4-2 鏈結串列的基礎-(7)
Chapter 5 Tree & Binary Tree
單向鏈結串列 Singly Linked Lists.
樹狀結構 陳怡芬 2018/11/16 北一女中資訊專題研究.
佇列 (Queue).
Chapter8 Binary and Other Trees
講師:郭育倫 第3章 基本資料結構 講師:郭育倫
英语教学课件系列 八年级(上) it! for Go.
Write a letter in a proper format
第七章 搜索结构 静态搜索结构 二叉搜索树 AVL树.
Friendship Bouquet 友谊之花 Music: Nightengale Serenade
哈夫曼编码.
Chapter 5 樹(Trees).
第12章 樹狀搜尋結構 (Search Trees)
C 語言簡介 - 2.
The expression and applications of topology on spatial data
子博弈完美Nash均衡 我们知道,一个博弈可以有多于一个的Nash均衡。在某些情况下,我们可以按照“子博弈完美”的要求,把不符合这个要求的均衡去掉。 扩展型博弈G的一部分g叫做一个子博弈,如果g包含某个节点和它所有的后继点,并且一个G的信息集或者和g不相交,或者整个含于g。 一个Nash均衡称为子博弈完美的,如果它在每.
第 七 章 樹狀結構 課程名稱:資料結構 授課老師:________ 2019/1/1.
重點 資料結構之選定會影響演算法 選擇對的資料結構讓您上天堂 程式.
客户服务 询盘惯例.
樹狀結構 Tree Structure chapter 7 德明科技大學資訊科技系.
親愛的老師您好 感謝您選用本書作為授課教材,博碩文化準備本書精選簡報檔,特別摘錄重點提供給您授課專用。 說明: 博碩文化:
Lesson 44:Popular Sayings
第7章 树和二叉树 7.1 树 7.2 二叉树 7.3 以结点类为基础的二叉树设计 7.4 二叉树类 7.5 二叉树的分步遍历
二叉树和其他树 (Binary and other trees)
第六章 樹狀結構 T r e e 版權屬作者所有,非經作者 同意不得用於教學以外用途.
第九章 查找 2019/2/16.
樹 2 Michael Tsai 2013/3/26.
2-3-4 Tree and how it relates to Red Black Tree
第十五课:在医院看病.
感謝同學們在加分題建議. 我會好好研讀+反省~
B+ Tree.
Tree & Binary Tree.
Chapter 5 Recursion.
二叉树的遍历.
Have you read Treasure Island yet?
Unit 8 Our Clothes Topic1 What a nice coat! Section D 赤峰市翁牛特旗梧桐花中学 赵亚平.
第六章 树和二叉树 £6.1 树 £6.2 二叉树 £6. 3 二叉树的存储结构 £6.4 二叉树的遍历与线索化 £6.1.1 树的定义
Respect cannot be demanded, it must be earned
BORROWING SUBTRACTION WITHIN 20
中央社新聞— <LTTC:台灣學生英語聽說提升 讀寫相對下降>
关联词 Writing.
第7章 樹與二元樹(Trees and Binary Trees)
中考英语阅读理解 完成句子命题与备考 宝鸡市教育局教研室 任军利
Philosophy of Life.
高考应试作文写作训练 5. 正反观点对比.
Chapter 6 樹狀結構.
9.1 何謂高度平衡二元搜尋樹 9.2 AVL-tree的加入 9.3 AVL-tree的刪除
计算机问题求解 – 论题1-5 - 数据与数据结构 2018年10月16日.
唐常杰 四川大学计算机学院 计算机科学技术系
進階 WWW 程式設計 -- PHP Array 靜宜大學資訊管理學系 蔡奇偉副教授
第4章 鏈結串列(Linked Lists) 4-1 動態記憶體配置-(6) 4-2 鏈結串列的基礎-(7)
Data Structure.
第10章 二元搜尋樹 (Binary Search Tree)
Respect cannot be demanded, it must be earned
JAVA 程式設計與資料結構 第十七章 Tree.
Train Track and Children
陳情表之外     with 三仁 三樂 歐陽宜璋製於 /10/23.
When using opening and closing presentation slides, use the masterbrand logo at the correct size and in the right position. This slide meets both needs.
Presentation transcript:

§4 Additional Binary Tree Operations  Copying Binary Trees: LRV where V ::= copy ( Program 5.6 on p.207 )  Testing for Equality of Binary Trees: VLR where V ::= test ( Program 5.7 on p.207 ) Read the two programs in the textbook and think: Is there any particular reason to choose one order over the others for the tasks?

§5 Threaded Binary Trees Let’s look at the rules first 2n links, if each node has only the left and right-child links. Here comes the typical question of mine: Why do we need threaded binary trees? Because I enjoy giving you headaches ... Just kidding. Okay, think of a full binary tree with n nodes. How many links are there? Any clue on how to improve the situation? We can replace the null links by “threads” which will make traversals easier. Then who should take the credit? They are A. J. Perlis and C. Thornton. You are such a genius ! n + 1. Oh well, I wish I’d have really done it Of course not! How many of them are NULL? You got it! Can I stand that?

§5 Threaded Binary Trees Rule 1: If ptr->left_child is null, replace it with a pointer to the inorder predecessor of ptr. Rule 2: If ptr->right_child is null, replace it with a pointer to the inorder successor of ptr. Rule 3: There must not be any loose threads. Therefore a threaded binary tree must have a head node of which the left child points to the first node. Structure typedef struct threaded_tree *threaded_ptr; typedef struct threaded_tree { short int left_thread; /* if it is TRUE, then left_child */ threaded_ptr left_child; /* is a thread, not a child ptr. */ char data; short int right_thread; /* if it is TRUE, then right_child */ threaded_ptr right_child; /* is a thread, not a child ptr. */ }

〖Example〗 Given the syntax tree of an expression (infix) A + B  C  D §5 Threaded Binary Trees 〖Example〗 Given the syntax tree of an expression (infix) A + B  C  D F + T A   D B C head node + A   D B C

The time complexity is still O( n ). What’s the advantage? §5 Threaded Binary Trees  Inorder Traversal of a Threaded Binary Tree void tinorder ( threaded_ptr tree ) { /* traverse the threaded binary tree inorder */ threaded_ptr temp = tree ; /* start from the head node */ for ( ; ; ) { temp = insucc ( temp ); /* find the inorder successor */ if ( temp == tree ) break ; /* if back to the head node, done */ printf ( “ %3c”, temp->data ) ; /* else print it out */ } /* end for-loop */ } The time complexity is still O( n ). What’s the advantage? The stack is gone so it runs faster. threaded_ptr insucc ( threaded_ptr tree ) { /* find the inorder successor of tree in a threaded binary tree */ threaded_ptr temp ; temp = tree->right_child ; /* go to the right_child of tree */ if ( !tree->right_thread ) /* if temp is indeed a right child */ while ( !temp->left_thread ) /* while temp has a left child */ temp = temp->left_child ; /* then keep going to the next left child */ return temp ; /* temp’s left thread must points to tree */ }

 Inserting a Node into a Threaded Binary Tree  child->left_thread §5 Threaded Binary Trees  Inserting a Node into a Threaded Binary Tree  child->left_thread = child->right_thread = TRUE Case 1 A P B X parent C child We will examine only the case of inserting a new node as the right child.  child->left_child = parent  child->right_child = parent->right_child  parent->right_thread = FALSE parent->right_child = child P A B parent C child  child->right_child = parent->right_child child->right_thread = parent->right_thread  child->left_child = parent child->left_thread = TRUE  parent->right_child = child parent->right_thread = FALSE Case 2  insucc(child)->left_child = child

void insert_right (threaded_pointer parent, threaded_pointer child) { /* insert child as the right child of parent in a threaded binary tree */     threaded_pointer temp;   child->right_child=parent->right_child;    child->right_therad=parent->right_thread;   child->left_child=parent;   child->left_thread=TRUE;   parent->right_child=child;   parent->right_thread=FALSE;   if ( !child->right_thread )   {     temp = insucc(child);     temp->left_child = child;   } }

Two important binary trees Huffman Tree Binary search tree Definition Searching Insertion Deletion

霍夫曼树 (Huffman Tree) 具有不同路径长度的二叉树 路径(Path) 从树中一个节点到另一个节点之间的分支构成这两个节点之间的路径。 路径长度 (Path Length) 两个结点之间的路径长度是连接两结点的路径上的分支数。树的路径长度是各叶结点到根结点的路径长度之和。 具有不同路径长度的二叉树

带权路径长度 树的带权路径长度是树的各叶结点所带的权值与该结点到根的路径长度的乘积的和。 带权路径长度 ( Weighted Path Length, WPL ) 树的带权路径长度是树的各叶结点所带的权值与该结点到根的路径长度的乘积的和。 具有不同带权路径长度的扩充二叉树

Huffman tree 设有n个权值{ω1, ω 2,…, ω n},试构造一棵有n个叶子结点的二叉树,每个叶子结点带权为ω i,则其中带权路径长度WPL最小的二叉树称作最优二叉树或Huffman树。 在霍夫曼树中,权值大的结点离根最近。 利用霍夫曼树可以在解某些判定问题时得到最佳判定算法。

霍夫曼树的构造算法 由给定的 n 个权值{w0, w1, w2, …, wn-1},构造具有 n 棵扩充二叉树的森林F = { T0, T1, T2, …, Tn-1 },其中每一棵扩充二叉树 Ti 只有一个带有权值 wi 的根结点,其左、右子树均为空。 重复以下步骤, 直到 F 中仅剩下一棵树为止: 在 F 中选取两棵根结点的权值最小的扩充二叉树, 做为左、右子树构造一棵新的二叉树。置新的二叉树的根结点的权值为其左、右子树上根结点的权值之和。 在 F 中删去这两棵二叉树。 把新的二叉树加入 F。

霍夫曼树的构造过程

霍夫曼编码 An example 设给出一段报文: CAST CAST SAT AT A TASA字符集合是 { C, A, S, T }, 各个字符出现的频度(次数)是 W={ 2, 7, 4, 5 }。 若给每个字符以等长编码 A : 00 T : 10 C : 01 S : 11 则总编码长度为 ( 2+7+4+5 ) * 2 = 36. 若按各个字符出现的概率不同而给予不等长编码,让出现次数多的字符采用尽可能短的编码,可望减少总编码长度。 前缀编码 任一个字符的编码都不是另一个字符的编码的前缀。 对每个字符设计长度不等的编码,且让电文中出现次数较多的字符采用尽可能短的编码。 但必须是任意一个字符的编码都不是另一个字符的编码的前缀--》前缀编码。

利用Huffman树设计前缀编码 因各字符出现的概率为{ 2/18, 7/18, 4/18, 5/18 },化整为 { 2, 7, 4, 5 },以它们为各叶结点上的权值,建立霍夫曼树。左分支赋 0,右分支赋 1,得霍夫曼编码(变长编码)。 结果: A : 0 T : 10 C : 110 S : 111 它的总编码长度: 7*1+5*2+( 2+4 )*3 = 35。 比等长编码的情形要短。 如何得到使电文总长最短的二进制前缀编码? 假设每种字符在电文中出现的次数为wi,其编码长度为li,电文中有n种字符, 则电文总长为∑wili。 对应到二叉树,设wi为叶结点的权,li恰为从根结点到叶结点的路径的长度, 则∑wili恰为二叉树上带权路径长度。 由此可见,设计电文总长最短的二进制前缀编码即为以n种字符出现的频率为权,设计一棵haffman树的问题。

Binary Search Trees 【Definition】A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties: (1) Every element has a key, and the keys are unique. (2) The keys in a nonempty left subtree must be smaller than the key in the root of the subtree. (3) The keys in a nonempty right subtree must be larger than the key in the root of the subtree. (4) The left and right subtrees are also binary search trees. 30 5 2 40 60 70 80 65 20 15 12 25 10 22

#include "tree.h" #include <stdlib.h> #include "fatal.h" #ifndef _Tree_H #define _Tree_H struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree; SearchTree MakeEmpty( SearchTree T ); Position Find( ElementType X, SearchTree T ); Position FindMin( SearchTree T ); Position FindMax( SearchTree T ); SearchTree Insert( ElementType X, SearchTree T ); SearchTree Delete( ElementType X, SearchTree T ); ElementType Retrieve( Position P ); #endif /* _Tree_H */ #include "tree.h" #include <stdlib.h> #include "fatal.h" struct TreeNode {   ElementType Element;   SearchTree Left;   SearchTree Right; }; Position Find( ElementType X, SearchTree T ) {   if( T == NULL )    return NULL;   if( X < T->Element )    return Find( X, T->Left );   else    if( X > T->Element )      return Find( X, T->Right );   else    return T; } SearchTree MakeEmpty( SearchTree T ) {   if( T != NULL )   {    MakeEmpty( T->Left );    MakeEmpty( T->Right );    free( T );   }   return NULL; }

Position FindMin( SearchTree T ) {   if( T == NULL )    return NULL;   else   if( T->Left == NULL )    return T;   else    return FindMin( T->Left ); } Position FindMax( SearchTree T ) {   if( T != NULL )     while( T->Right != NULL )       T = T->Right;   return T; }

Inserting into a Binary Search Tree Sketch of the idea: Insert 80 30 5 2 40  check if 80 is already in the tree  80 > 40, so it must be the right child of 40 25 35 80 This is the last node we encounter when search for the key number. It will be the parent of the new node. Insert 35  check if 35 is already in the tree  35 < 40, so it must be the left child of 40 Insert 25  check if 25 is already in the tree  25 > 5, so it must be the right child of 5

重复元的插入可以通过在节点记录中保留一个附加域以指示发生的频率来处理。 SearchTree Insert( ElementType X, SearchTree T ) {   if( T == NULL )   {    /* Create and return a one-node tree */     T = malloc( sizeof( struct TreeNode ) );    if( T == NULL )      FatalError( “Out of space!!!” );    else    {      T->Element = X;      T->Left = T->Right = NULL;    }   }   else     if( X < T->Element )     T->Left = Insert( X, T->Left );   else    if( X > T->Element )     T->Right = Insert( X, T->Right );   /* Else X is in the tree already; we‘ll do nothing */    return T;  /* Do not forget this line!! */ } 重复元的插入可以通过在节点记录中保留一个附加域以指示发生的频率来处理。

Deletion from a binary search tree Note: These kinds of nodes have degree at most 1. Delete leaf node Set the child field of its parent to NULL and free the node. Delete node that has a single child Erase the node and place the single child in the place of the erased node. Delete node that has two child Replace the node with either the largest element in its left subtree or the smallest element in its right subtree. Delete the replacing node from the subtree. 〖Example〗 Delete 60 40 50 45 55 52 60 70 20 10 30 h Tp = O( ? ) Solution 1: reset left subtree. 55 Solution 2: reset right subtree. 52 Of course you can do it by yourselves

SearchTree Delete( ElementType X, SearchTree T ) {   Position TmpCell;   if( T == NULL )     Error( "Element not found" );   else   if( X < T->Element )  /* Go left */     T->Left = Delete( X, T->Left );   else   if( X > T->Element )  /* Go right */     T->Right = Delete( X, T->Right );   else  /* Found element to be deleted */   if( T->Left && T->Right )  /* Two children */   {    /* Replace with smallest in right subtree */     TmpCell = FindMin( T->Right );     T->Element = TmpCell->Element;     T->Right = Delete( T->Element, T->Right );   }   else  /* One or zero children */   {     TmpCell = T;     if( T->Left == NULL )  /* Also handles 0 children */       T = T->Right;     else if( T->Right == NULL )       T = T->Left;     free( TmpCell );   }   return T; }

Height of a binary search tree Question: Place n elements in a binary search tree. How high can this tree be? Answer: The height depends on the order of insertion. 〖Example〗 Given elements 1, 2, 3, 4, 5, 6, 7. Insert them into a binary search tree in the orders: 4, 2, 1, 3, 6, 5, 7 and 1, 2, 3, 4, 5, 6, 7 4 2 3 1 4 5 6 7 6 Average h = O( log2 n ) 2 所有的操作都花费  O(d ),   d 是包含所访问的关键字的节点的深度。树的所有节点的平均深度是 O (LogN)一棵树的所有节点的深度的和称为内部路径长(internal path length) D( N ) = D ( i ) + D ( N - i - 1 ) + N - 1  D ( N ) 具有 N 个节点的某棵树 T 的内部路径长  D(1) = 0, 一棵N节点树是由一棵i节点左子树和一棵(N - i - 1)右子树 =O ( N log N ),   任意节点的期望深度为 O (LogN) h = 7 1 3 5 7 h = 3 Balanced search trees will be discussed in next class

Excercises Question: Place n elements in a binary search tree. How high can this tree be?

AVL Tree AVL(adelson-velskii and landis)树是带有平衡条件的二叉查找树。 平衡条件: (1)树的深度是 O ( Log N ) , 左右子树具有相同的高度 (2)每个节点的左子树和右子树的高度最多差1 大致上讲,一个 AVL树的高度最多=1.44 log( N + 2 ) - 0.328

在高度为h的 AVL树中,最少节点数, S ( h ),由  S ( h ) = S ( h - 1 ) + S ( h - 2 ) + 1给出 在插入以后,只有那些从插入点到根节点的路径上的节点的平衡可能被改变,因为只有这些节点的子树可能发生变化。

可能找到一个节点,它的新平衡破坏了 AVL 条件。 对 的左儿子的左子树进行一次插入 LL                                                单旋转 对    的左儿子的右子树进行一次插入 LR 双旋转 对    的右儿子的左子树进行一次插入 RL 对    的右儿子的右子树进行一次插入 RR                                                          

LL

RR

LR

RL