830 likes | 1.2k Vues
Chapter 10 BINARY TREES. 1. General Binary Trees. 2. Binary Search Trees. 3. Building a Binary Search Tree. 4. Height Balance: AVL Trees. 5. Splay Trees. 6 . Pointers and Pitfalls. 10.1 Binary Trees. 1. Definitions.
E N D
Chapter 10 BINARY TREES 1. General Binary Trees 2. Binary Search Trees 3. Building a Binary Search Tree 4. Height Balance: AVL Trees 5. Splay Trees 6. Pointers and Pitfalls
10.1 Binary Trees 1. Definitions DEFINITION A binary treeis either empty, or it consists of a node called the roottogether with two binary trees called the left subtreeand the right subtreeof the root.
2. Traversal of Binary Trees preorder preorder inorder inorder postorder postorder At a given node there are three tasks to do in some order: Visit the node itself (V); traverse its left subtree (L); traverse its right subtree (R). There are six ways to arrange these tasks: VLRLVR LRV VRL RVL RLV By standard convention, these are reduced to three by considering only the ways in which the left subtree is traversed before the right. These three names are chosen according to the step at which the given node is visited.
◆With preorder traversal we first visit a node, then traverse its left subtree, and then traverse its right subtree. ◆With inorder traversal we first traverse the left subtree, then visit the node, and then traverse its right subtree. ◆With postorder traversal we first traverse the left subtree, then traverse the right subtree, and nally visit the node. See pg.432-434
3. Linked implementation of Binary Trees Node structure of Binary Trees example
Linked Binary Trees Specifications 二叉树根结点指针 Binary trees class template <class Entry> class Binary_tree { public: // Add methods here. protected: // Add auxiliary function prototypes here. Binary_node<Entry> *root; };
Binary node class 指向右孩子的指针 数据域 指向左孩子的指针 template <class Entry> struct Binary_node { Entry data; Binary_node<Entry> *left; Binary_node<Entry> *right; Binary_node( ); Binary_node(const Entry & x); };
Inorder traversal: method 函数参数 (访问数据) 调用递归中序 遍历函数 递归中序遍历函数 多一个参数 template <class Entry> void Binary_tree<Entry>::inorder(void (*visit)(Entry &)) { recursive_inorder(root, visit); } template <class Entry> void Binary_tree<Entry>:: recursive_inorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &)) /* Pre: sub_root is either NULL or points to a subtree of the inary_tree. Post: The subtree has been traversed in inorder sequence. */ { if (sub_root != NULL) { recursive_inorder(sub_root->left, visit); (*visit)(sub_root->data); recursive_inorder(sub_root->right, visit); } }
◆树形结构是一类重要的非线性结构。树能够很好地描述结构的分支关系和层次特性,它非常类似于自然界中的树。树结构在客观世界中是大量存在的,例如家谱以及行政组织机构都可用树形象地表示。树在计算机领域中也有着广泛的应用,如在编译程序中,用树来表示源程序语法结构;在数据库系统中,可以用树来组织信息。本章重点讨论二叉树的存储表示及其各种运算,要求大家要学会编写实现二叉树的各种运算的算法。◆树形结构是一类重要的非线性结构。树能够很好地描述结构的分支关系和层次特性,它非常类似于自然界中的树。树结构在客观世界中是大量存在的,例如家谱以及行政组织机构都可用树形象地表示。树在计算机领域中也有着广泛的应用,如在编译程序中,用树来表示源程序语法结构;在数据库系统中,可以用树来组织信息。本章重点讨论二叉树的存储表示及其各种运算,要求大家要学会编写实现二叉树的各种运算的算法。 ◆二叉树是递归定义的,因此递归是它的固有特性,本小节的练习中就要求大家完成许多操作二叉树的算法,且是递归算法。 ◆下面我们给出一个以先序方式创建任意二叉树的算法,请 认真学习。
template<class Entry> void Binary_tree<Entry>::CreatBinary_tree() { CreatBinary_tree(root); } template<class Entry> void Binary_tree<Entry>:: CreatBinary_tree(Binary_node<Entry>* &r) { cin>>x; if ( x==endmark ) r = NULL; else{ r = new Binary_Node<Entry>(x); CreatBinary_tree(r->left); CreatBinary_tree(r->right); } } 递归建立以r为 根结点指针的二叉链表 表示的二叉树 建立”空”二叉树 建立根结点 建立根结点 的左子树 建立根结点 的右子树 建立二叉链表 表示的二叉树 Entry data member Entry data member 表示‘空’的 数据
我们再给出一个应用例。 在二叉树tree中搜索 结点x,找到则打印 所有祖先结点及x 否则输出x不存在 要求非递归 因此自行定义 ‘栈结点’类型 指针域(保留从根到 搜索结点路径上 所以结点指针) 标记域 (tag==0左子树 否则是右子树) 栈数组 栈结点类型名 在以二叉链表存储的二叉树中查找值为x的结点,请编写一非递归算法:打印值为x的结点的所有的祖先结点的值。设值为x的结点不多于1个。 取得二叉树tree 的根结点指针 做为t的初始值 二叉树tree 的根结点是x 指针不‘空’而且 其所指结点的 数据域不是x 置空栈 搜索左子树 寻找值为x的结点 栈不‘空’ 进栈 template <class T> printAncestors(Binary_tree<T> tree, T& x) { typedef struct { Binary_node<T>* ptr; int tag; } StackNode; StackNode ST[100]; int i, top = -1 ,find=0; Binary_node<T>* t = tree.GetRoot(); if(t->data==x) { cout<<x<<" is Root\n"; return; } while( t && t->data!=x || top!=-1 ) { while(t && t->data != x) { ST[++ top].ptr=t; ST[top].tag = 0; t=t->left; }
跳出while循环 如果栈顶是右指针 弹出 如果栈不空 栈顶是左指针 转向右子树 重新回到while if (t && t->data==x) { for(i=0; i<=top; i++) cout<<ST[i].ptr->data<<endl; cout<<x<<endl; find=1; break; } else { while( top!=-1 && ST[top].tag==1) top--; if (top!=-1) { ST[top].tag=1; t=ST[top].ptr->right; } } // end_(if-else)block } // end_while if (!find) cout<<" search "<<x<<" not exists\n"; } 没找到值为x的结点 找到值为x的结点 输出所有祖先结点 (栈) 在C++环境下演示创建二叉树及在建立的二叉树上完成各项操作的bt_main.cpp。
10.2 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)? binary search treeDefinitions pg.445 A binary search treeis a binary trees 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 root (if it exists) is greater than the key in any node in the left subtree of the root. 2. The key of the root (if it exists) is less than the key in any node in the right subtree of the root. 3. The left and right subtrees of the root are again binary search trees.
error error binary searchtree not binary searchtree 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 satises 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. 1. Ordered Lists and Implementations pg.446 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 will be derived from the binary tree class; hence all binary tree methods are inherited.
The Binary Search Tree Class pg.446 template <class Record> class Search_tree:public Binary_tree <Record> { public: Error_code insert(const Record &new_data); Error_code remove(const Record &old_data); Error_code tree_search(Record &target) const; protected: // Add auxiliary function prototypes here. }; The inherited methods include the constructors, the destructor,clear, empty, size, height, and the traversals preorder, inorder,and postorder.
A binary search tree also admits specialized methods called insert, remove, and tree search. The class Record has the behavior outlined in Chapter 7: Each Record is associated with a Key. The keys can be compared with the usual comparison operators. By casting records to their corresponding keys, the comparison operators apply to records as well as to keys. 2. Tree Searchpg.447 Error_code Search_tree<Record>:: tree_search(Record &target) const; Post: If there is an entry in the tree whose key matches that in target, the parameter target is replaced by the corresponding record from the tree and a code of success is returned. Otherwise a code of not_present is returned.
This method will often be called with a parameter target that contains only a key value. The method will fill target with the complete data belonging to any corresponding Record in the tree. To search for the target, we first compare it with the entry at the root of the tree. If their keys match, then we are finished. Otherwise, we go to the left subtree or right subtree as appropriate and repeat the search in that subtree. We program this process by calling an auxiliary recursive function.
The process terminates when it either finds the target or hits an empty subtree. The auxiliary search function returns a pointer to the node that contains the target back to the calling program. Since it is private in the class, this pointer manipulation will not compromise tree encapsulation. Binary_node<Record>* Search_tree<Record>:: search_for_node(Binary_node<Record> *sub_root, const Record &target) const Pre: sub root is NULL or points to a subtree of a Search tree Post: Ifthe key of target is not in the subtree, a result of NULL is returned. Otherwisea pointer to the subtree node containing the target is returned.
Recursive auxiliary function:pg.448 Non-recursive version: template <class Record> Binary_node<Record>* Search_tree<Record>::search_for_node(Binary_node<Record> *sub_root, const Record &target) const { while(sub_root!=NULL && sub_root->data!=target) if(sub_root->data<target) sub_root = sub_root->right; else sub_root = sub_root->left; return sub_root; }
Public method for tree search: template <class Record> Error_code Search_tree<Record>:: tree_search(Record &target)const { Error_code result = success; Binary_node<Record> *found; 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 The same keys may be built into binary search trees of many different shapes
Analysis of Tree Search • Draw the comparison tree for a binary search (on an ordered list). Binary search on the list does exactly the same comparisons as tree search will do if it is applied to the comparison tree. By Section 7.4, binary search performs O(㏒n)comparisons for a list of length n. This performance is excellent in comparison to other methods, since ㏒n grows very slowly as n increases. • The same keys may be built into binary search trees of many different shapes. If a binary search tree is nearly completely balanced (“bushy”),then tree search on a tree with n vertices will also do O (㏒n) comparisons of keys.
If the tree degenerates into a long chain, then tree search becomes the same as sequential search, doing (n)comparisons on n vertices. This is the worst case for tree search. The number of vertices between the root and the target, inclusive,is the number of comparisons that must be done to find the target. The bushier the tree, the smaller the number of comparisons that will usually need to be done. It is often not possible to predict (in advance of building it) what shape of binary search tree will occur. In practice, if the keys are built into a binary search tree in random order, then it is extremely unlikely that a binary search tree degenerates badly; tree_search usually performs almost as well as binary search.
3.Insertion into a Binary Search Treepg.451 Error_code Search tree<Record>:: insert(const Record &new data); Post: If a Record with a key matching that of new data already belongs to the Search tree a code of duplicate_error is returned. Otherwise, the Record new data is inserted into the tree in such a way that the properties of a binary search tree are preserved, and a code of success is returned.
Method for Insertion 直至到达一个‘空’BST 子树,脱离循环;这时 插入结点应是parent 的孩子 插入数据小于 当前(子树根)结点数据 继续在左子树中 搜寻插入位置 template <class Record>Error_code Search_tree<Record>::insert(const Record &new_data) { if(root==NULL) { root=new Binary_node<Record>(new_data); return success; } Binary_node<Record> *sub_root=root, *parent; do{ parent=sub_root; if(new_data<sub_root->data) sub_root=sub_root->left; else if(new_data>sub_root->data) sub_root=sub_root->right; else return duplicate_error; }while(sub_root!=NULL); “空”BST 直接插入(根结点) 在右子树中 搜寻插入位置 寻找 插入位置
sub_root=new Binary_node<Record>(new_data); if(new_data<parent->data) parent->left=sub_root; else parent->right = sub_root; return success; } 根据插入结点值 确定应是parent 的左孩子还是右孩子? The method insert can usually insert a new node into a random binary search tree with n nodes in O(㏒n) steps. It is possible, but extremely unlikely, that a random tree may degenerate so that insertions require as many as n steps. If the keys are inserted in sorted order into an empty tree, however, this degenerate case will occur.
4. Treesort pg.453 • 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. Theorem 10.1 Treesort makes exactly the same comparisons of keys as does quicksort when the pivot for each sublist is chosen to be the first key in the sublist. See pg.453 Theorem 10.1 and Corollary 10.2
Corollary 10.2 In the average case, on a randomly ordered list of length n, treesort performs comparisons of keys. • 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.
从二叉查找(搜索/排序)树中删除一个结点,不能把以该结点为根的子树都删除,只能删掉该结点,并且还应该保证删除后得到的二叉树仍然满足二叉查找树的性质,即在二叉查找树中删除一个结点相当于删去有序序列中的一个结点。那么,如何在二叉查找树上删去一个结点呢? 如上页图所示:当被删结点是叶子或仅有一棵子树时,情况较简单,只是修改其双亲结点的指针就可以了;若被删结点既有左子树也有右子树时,应当如何处理呢?假设在二叉查找树上被删结点为P(指向结点的指针为p),其双亲结点为F(结点指针为f),且不失一般性,可设p是f的左指针,即被删结点P是F的左孩子。一种处理方法如下页图所示:以被删结点的左子树中最大结点S(必然是叶子)的数据代替被删结点P的数据,然后删除叶子S。
sub_root只有左 子树,以左孩子做为 删除后子树的新根 sub_root只有右 子树,以右孩子做为 删除后子树的新根 Auxiliary Function to Remove One Node: sub_root既有左 子树,也有右子树. 搜寻它的左子树 中最大结点 template <class Record>Error_code Search_tree<Record>:: remove_root(Binary_node<Record>* &sub_root) { if(sub_root==NULL) return not_present; Binary_node<Record> *to_delete=sub_root->left; if(sub_root->right==NULL) sub_root=sub_root->left; else if(sub_root->left==NULL) sub_root=sub_root->right; else // else(2) { to_delete=sub_root->left; Binary_node<Record> *parent=sub_root; while(to_delete->right!=NULL) { parent=to_delete; to_delete=to_delete->right; } sub_root->data=to_delete->data; 令to_delete移动到 sub_root的左孩子 指针parent 保持跟踪to_delete 是它的双亲结点 在sub_root所指 结点的左子树中 搜寻最大结点 (此结点是它的前驱) 脱离循环to_delete 指向找到的结点 将to_delete所指结点 的数据域转移到 sub_root所指结点 If sub_root is NULL a code of not_present is returned. Otherwise, the root of the subtree is removed in such a way that the properties of a binary search tree are preserved. The parameter sub_root is reset as the root of the modified subtree, and success is returned. 注意:sub_root 一定是引用参数
if(parent==sub_root) sub_root ->left=to_delete->left; else parent->right=to_delete->left; } // end else(2) delete to_delete; return success; } to_delete是sub_root 的左孩子它没有右子树, 因此,将to_delete所指结点 的左子树做为sub_root (parent)的左子树 将to_delete所指结点 的左子树做为parent的右子树 Remove to_delete from tree Removal Method: template <class Record>Error_code Search_tree<Record>::remove(const Record &target) { return search_and_destroy(root, target); }
Recursive function: search_and_destroy template <class Record>Error_code Search_tree<Record>:: search_and_destroy(Binary_node<Record>* &sub_root, const Record &target) { if(sub_root==NULL || sub_root->data==target) return remove_root(sub_root); else if (target<sub_root->data) return search_and_destroy(sub_root->left, target); else return search_and_destroy(sub_root->right, target); } 按target值向左(右)子树递归 调用搜寻被删结点.sub_root的 左(右)孩子域做为递归调用参数, 这样当到达边界调用remove_root时 sub_root的地址就可以由引用参数 带回到其双亲结点的左(右)孩子域
10.3 Building a Balanced Binary Search Tree Problem: Start with an ordered list and build its entries into a binary search tree that is nearly balanced (‘bushy’). If the nodes of a complete binary tree are labeled in inorder sequence, starting with 1, then each node is exactly as many levels above the leaves as the highest power of 2 that divides its label.
由于课时不够,本节删略。 感兴趣的同学可以自学。 See Pg 463-472
10.4 Height Balance: AVL Trees 平衡因子 1.Denition: 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 factorthat 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. 中文教材中,平衡因子=左子树高度-右子树高度;因此AVL定义为BST中任一结点平衡因子的绝对值≤1。
平衡 左高1 本教材中,将平衡因子定义为“枚举”类型。 左高2 右高2 enum Balance_factor { left_higher, equal_height, right_higher };
data member constructor constructor AVL_node: pg.475 inherit template <class Record> struct AVL_node: public Binary_node<Record> { Balance_factor balance; AVL_node() { balance = equal_height; left = right = NULL; } AVL_node(const Record &x); { balance = equal_height; data = x; left = right = NULL; } void set_balance(Balance_factor b) { balance = b; } Balance_factor get_balance() const { return balance; } };
In a Binary_node, left and right have type Binary_node*, so the inherited pointer members of an AVL node have this type too, not the more restricted AVL node *. In the insertion method,we must make sure to insert only genuine AVL nodes. The benefit of implementing AVL nodes with a derived structure is the reuse of all of our functions for processing nodes of binary trees and search trees. We often invoke these methods through pointers to nodes,such as left->get_balance( ). But left could (for the compiler) point to any Binary_node, not just to an AVL_node, and these methods are declared only for AVL_node. A C++ compiler must reject a call such as left->get balance( ), since left might point to a Binary node that is not an AVL_node.
To enable calls such as left->get_balance( ), we include dummy versions of get_balance( ) and set_balance( ) in the underlying Binary_node structure. These do nothing: template <class Entry> void Binary_node<Record>::set_balance(Balance_factor b) { } template <class Entry> Balance_factor Binary_node<Record>::get_balance(Balance_factor b) { return equal_height; }
Class Declaration for AVL Trees template <class Record> class AVL_tree: public Search_tree<Record> { public: Error_code insert(const Record &new_data); Error_code remove(const Record &old_data); private: void prenode(void (*f)(Binary_node<Record> *&)); Error_code avl_insert(Binary_node<Record> *&, const Record &, bool &); void right_balance(Binary_node<Record> *&); void left_balance(Binary_node<Record> *&); void rotate_left(Binary_node<Record> *&); void rotate_right(Binary_node<Record> *&);
Error_code remove_avl(Binary_node<Record> *&, const Record &, bool &); Error_code remove_avl_right(Binary_node<Record> *&, const Record &, bool &); Error_code remove_avl_left(Binary_node<Record> *&, const Record &, bool &); // Add auxiliary function prototypes here. }; 2.Insertions of a Node See pg.478 Fig. 10.18 Simple insertions of nodes into an AVL tree
AVL insertions requiring rotations (pg.483 Fig.10.21)
Public Insertion Method template <class Record>Error_code AVL_tree<Record>:: insert(const Record &new_data) { bool taller; return avl_insert(root, new_data, taller); } Has the tree grown in height? Post: If the key of new_data is already in the AVL_tree, a code of duplicate_error is returned. Otherwise, a code of success is returned and the Record new_data is inserted into the tree in such a way that the properties of an AVL tree are preserved. Uses: avl_insert.