1 / 37

Chapter 10 Trees &Binary Trees

Chapter 10 Trees &Binary Trees. Binary Trees Definitions, Properties, implementation Traversal of Binary Trees & It’s Applications Trees & Forests vs. Binary Trees Huffman Tree ( 哈夫曼树) Binary Search Trees Height Balance: AVL Trees. Trees. A tree is either : (rooted) Empty

sonel
Télécharger la présentation

Chapter 10 Trees &Binary Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10Trees &Binary Trees • Binary Trees • Definitions, Properties, implementation • Traversal of Binary Trees & It’s Applications • Trees & Forests vs. Binary Trees • Huffman Tree (哈夫曼树) • Binary Search Trees • Height Balance: AVL Trees

  2. Trees • A tree is either : (rooted) • Empty • Contains a root and N subtrees (N>=0). A tree is a nonlinear data structure. root subtree

  3. Terms • Child • Parent • Ancestor • Sibling : nodes that share a common parent. The only item which has no Parent.

  4. Terms (continue) • Leaf : a node with no children.(end vertices or external vertices)The remaining vertices are called the internal vertices. • Node depth : the number of links on the path from the root to the node. • Tree height :the number of vertices in the longest path that occurs. (page 286) the depth of the deepest node. An empty tree has height 0. Two rules about parents: • The root has no parent. • Every other node has exactly one parent.

  5. 结点(node/vertex) • 结点的度(degree) • 分支(branch)结点 • 叶(leaf)结点 • 子女(child)结点 • 双亲(parent)结点 • 兄弟(sibling)结点 • 祖先(ancestor)结点 • 子孙(descendant)结点 • 结点所处层次(level) • 树的高度(depth) • 树的度(degree) • 有序树 • 无序树 • 森林

  6. Binary Trees Different binary tree

  7. 2-Trees (p290) • As 2-treeis a tree in which every vertex except the leaves has exactly two children.

  8. Full Binary Trees • A binary tree t is full if t is a two-tree with all its leaves one the same level. a binary tree t is full if t is empty or t’s left and right subtrees have the same height and both are full. Full Binary Tree

  9. Complete Binary Trees • A binary tree t is complete if t is full through level height(t)-1, and all the leaves at lowest level occupy the leftmost positions in the tree. Complete Binary Tree

  10. The Binary Tree Theorem(Properties) • N(i)<=2^(i-1) • If height(t)=k, N(t)<=2^k –1; • If t is a complete binary tree of n nodes, height(t) =

  11. 二叉树的性质 性质1若二叉树的层次从0开始, 则在二叉树的第 i 层最多有 2i 个结点。(i 0) [证明用数学归纳法] 性质2高度为k的二叉树最多有 2k+1-1个结点。 (k -1) [证明用求等比级数前k项和的公式] 性质3对任何一棵二叉树, 如果其叶结点个数为 n0, 度为2的非叶结点个数为 n2, 则有 n0=n2+1

  12. 证明:若设度为1的结点有n1个,总结点个数为n,总边数为e,则根据二叉树的定义,证明:若设度为1的结点有n1个,总结点个数为n,总边数为e,则根据二叉树的定义, n = n0 + n1 + n2e = 2n2 + n1 = n - 1 因此,有 2n2 + n1= n0 + n1 + n2 - 1 n2 = n0 - 1 n0 = n2 + 1 定义1满二叉树(Full Binary Tree) 定义2完全二叉树(Complete Binary Tree) 若设二叉树的高度为h,则共有h+1层。除第h层外,其它各层(0h-1)的结点数都达到最大个数,第h层从右向左连续缺若干结点,这就是完全二叉树。

  13. 性质4具有n个结点的完全二叉树的高度为 log2(n+1)-1 证明:设完全二叉树的高度为h,则有 2h- 1 < n 2h+1- 1 2h < n+1  2h+1 取对数 h < log2(n+1)  h+1

  14. 性质5如果将一棵有n个结点的完全二叉树自顶向下,同一层自左向右连续给结点编号0, 1, 2, …, n-1,然后按此结点编号将树中各结点顺序地存放于一个一维数组中, 并简称编号为i的结点为结点i (0 i n-1。则有以下关系: • 若i == 0, 则 i 无双亲 若i > 0, 则 i 的双亲为(i -1)/2 • 若2*i+1 < n, 则 i 的左子女为2*i+1 若2*i+2 < n, 则 i 的右子女为2*i+2 • 若 i 为偶数, 且i != 0, 则其左兄弟为i-1 若 i 为奇数, 且i != n-1, 则其右兄弟为i+1 • i 所在层次为 log2 (i+1)

  15. Contiguous Binary Trees

  16. Binary tree

  17. Linked Binary Trees

  18. Linked implementation of binary tree:

  19. Binary tree class: template <class Entry> class Binary tree { public: // Add methods here. protected: // Add auxiliary function prototypes here. Binary node<Entry> *root; };

  20. Binary node class: template <class Entry> struct Binary node { // data members: Entry data; Binary node<Entry> *left; Binary node<Entry> *right; // constructors: Binary node( ); Binary node(const Entry &x); };

  21. Constructor & Empty template <class Entry> Binary tree<Entry> :: Binary tree( ) /* Post: An empty binary tree has been created. */ { root = NULL; } template <class Entry> bool Binary tree<Entry> :: empty( ) const /* Post: A result of true is returned if the binary tree is empty. Otherwise,false is returned. */ { return root == NULL; }

  22. Binary Tree Class Specification template <class Entry> class Binary tree { public: Binary tree( ); bool empty( ) const; void preorder(void (*visit)(Entry &)); void inorder(void (*visit)(Entry &)); void postorder(void (*visit)(Entry &)); int size( ) const; void clear( ); int height( ) const; void insert(const Entry &); Binary tree (const Binary tree<Entry> &original); Binary tree & operator = (const Binary tree<Entry> &original); Binary tree( ); protected: // Add auxiliary function prototypes here. Binary node<Entry> *root; };

  23. Traversal of Binary Trees • 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: • V L R L V R L R V • V R L R V L R L V.

  24. Traversal of Binary Trees • By standard convention, these are reduced to three by considering only the ways in which the left subtree is traversed before the right.

  25. Inorder traversal template <class Entry> void Binary tree<Entry> :: recursive_inorder(Binary node<Entry> *sub_root, void (*visit)(Entry &)) { if (sub_root != NULL) { recursive_inorder(sub_root ->left, visit); (*visit)(sub_root ->data); recursive_inorder(sub_root ->right, visit); } }

  26. Preorder traversal template <class Entry> void Binary tree<Entry> :: recursive_preorder(Binary node<Entry> *sub_root, void (*visit)(Entry &)) { if (sub_root != NULL) { (*visit)(sub_root ->data); recursive_preorder(sub_root ->left, visit); recursive_preorder(sub_root ->right, visit); } }

  27. Postorder traversal template <class Entry> void Binary tree<Entry> :: recursive_postorder(Binary node<Entry> *sub_root, void (*visit)(Entry &)) { if (sub_root != NULL) { recursive_postorder(sub_root ->left, visit); recursive_postorder(sub_root ->right, visit); (*visit)(sub_root ->data); } }

  28. Expression Trees

  29. Expression Trees

  30. Traversal of Comparison tree

  31. Applications of Traversal of Binary tree Size and height template <class Entry > int BinaryTree< Entry >:: Size ( constBinTreeNode < Entry > *t ) const { if ( t == NULL ) return 0; elsereturn 1 + Size ( t→left ) + Size ( t→right ); } template <class Entry > intBinaryTree< Entry >:: Height ( const BinTreeNode < Entry > *t ) const { if ( t == NULL ) return0; else return 1 + Max (Height ( t→left ), Height ( t→right ) ); }

  32. Other Applications of Traversal • Copy • Interchange • all left and right subtrees • Width • Traversal level by level

  33. Exercises • P442 • 10.1 E3 • E11, E12 • Binary_tree Create()

  34. Homework • p441 - 442 • Exercise 10.1 E2, E5

More Related