1. Trang chủ
  2. » Giáo án - Bài giảng

cấu trúc dữ liệu và giải thuật - Tree

60 294 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

DATA STRUCTURE AND ALGORITHMS – DSA315 Spring 2010 Lecture #09_10 Nguyen Tuan Anh tuananh@hanu.vn Aims • What it is (conceptual) • Why we use it (applications) • How we implement it (implementation) Today Definition and Terminology Other Representations of Trees Binary Tree • Array Representation, Linked Representation, • Find Depth, Count Leaf Nodes, Print, Compare, Internal Path Length • Traversing Binary Tree • Applications - Represent an expression • Applications - Huffman Code How We View a Tree Nature Lovers View Computer Scientists View Definition and Terminology Definition: Tree is defined as a finite set T of one or more nodes such that a) there is one specially designated node called the root of the tree, root(T) and b) the remaining nodes (excluding the root) are partitioned into m ≥ 0 disjoint sets T 1 , T 2 , . . . , T m and each of these sets in turn is a tree. The trees T 1 , T 2 , . . . , T m are called the subtrees of the root. 4 examples 16 10 16 14 10 9 3 8 7 2 4 2 4 2 4 2 4 16 14 10 9 3 8 2 4 9 16 10 9 2 4 Definition and Terminology Terminology: Level of node : The level of a node with respect to T is defined recursively: The level of root(T) is zero, and the level of any other node is one higher than that node's level with respect to the subtree of root(T) containing it. Depth of a tree: The maximum level of any leaf in the tree Degree of a node : The number of subtrees of a node Terminal node or leaf: A node of degree zero Branch node: A non-terminal node Parent and Siblings: Each root is said to be the parent of the roots of its subtrees, and the latter are said to be siblings; they are children of their parent. Ancestor and Descendant: Ancestor and descendant can also be used to denote the relationship that may span several level of tree. Definition and Terminology Level of node : State the levels of all the nodes: A:____, B:____, C:____, D:____, E:____, F:____, G:____, H:____, I:____ Root of a tree: Root of the tree is: _____ Depth of a tree: Depth of the tree is: _____ Degree of a node : State the degrees of: A:____, B:____, C:____, D:____, E:____, F:____, G:____, H:____, I:____ Terminal node or leaf: State all the leaf nodes: _______________________ Branch node: State all the branch nodes: _______________________ A B C D F H I E G Definition and Terminology Parent and Siblings: State the parents of:A:___, B:___, C:___, D:___, E:___, F:___, G:___, H:___, I:___ State the siblings of: A:_______, B:_______, C:_______, D:_______, E:_______, F:_______, G:_______, H:_______, I:_______ Ancestor and Descendant: State the ancesters of: A:______, B:______, C:______, D:___________, E:___________, F:___________, G:___________, H:___________, I:___________ State the descendants of: A:_________________________, B:___________, C:___________, D:____, E:____, F:____, G:____, H:____, I:____ A B C D F H I E G Binary Trees • A binary tree is a tree with the following properties: – Each internal node has at most two children (exactly two for proper binary trees) – The children of a node are an ordered pair • We call the children of an internal node left child and right child • Alternative recursive definition: a binary tree is either – a tree consisting of a single node, or – a tree whose root has an ordered pair of children, each of which is a binary tree • Applications: – arithmetic expressions – decision processes – searching A B C F G D E H I Arithmetic Expression Tree • Binary tree associated with an arithmetic expression – internal nodes: operators – external nodes: operands • Example: arithmetic expression tree for the expression (2 × (a − 1) + (3 × b)) + ×× − 2 a 1 3 b [...]... Tree Operations - equal // To compare 2 binary trees x int equal(NodePtr tree1 , NodePtr tree2 ) { if ( (tree1 == NULL) && (tree2 == NULL)) return(TRUE) ; if ( (tree1 != NULL) && (tree2 == NULL)) return(FALSE); if ( (tree2 != NULL) && (tree1 == NULL)) return(FALSE); if (tree 1-> info == tree 2-> info) if ( equal (tree 1-> left, tree 2-> left) && equal (tree 1-> right, tree 2-> right)) return(TRUE); return(FALSE); } x ... tree void PrintTree(NodePtr tree) { if (tree != NULL) { PrintTree (tree- >right); printf("%*c%d\n", indent, ' ', tree- >info); PrintTree (tree- >left, indent + 5); } } OR void printTree(struct node* node) { struct node { if (node == NULL) return; int data; printTree(node->left); struct node* left; printf("%d ", node->data); struct node* right; printTree(node->right); } } Binary Tree Operations - equal // To... binary tree A int depth(NodePtr tree) { int DepthOfLeftSubTree, DepthOfRightSubTree; if (tree == NULL) return(0); if ( (tree- >left == NULL) && (tree- >right == NULL)) return(0); // the root is at level 0 DepthOfLeftSubTree = depth (tree- >left); DepthOfRightSubTree = depth (tree- >right); if ( _) return _; else return _; } B D C E G F H Binary Tree Operations - count_leaf... any), is at i-1 Array Representation of Binary Tree 0 1 2 3 4 5 6 7 8 A B - C - - - D - 0 9 10 11 12 13 14 15 - - - - - 1 - E 3 Unused array elements (not exist or is NULL) 15 must be flagged for non-full binary tree 7 A B C D E Solutions:1 put a special value in the location 2 Add a “used” field (true/false) to each node Advantages and Disadvantages of using array to represent binary tree: _ Simpler... binary tree has 0 leaf node A tree with 1 node has 1 leaf node D No of leaf nodes = 1 No of leaf nodes = 3 //To count the number of leaf nodes int count_leaf(NodePtr tree) { if (tree == NULL) return(0); else if ( (tree- >left == NULL) && (tree- >right == NULL)) return(1); else return(count_leaf (tree- >left) + count_leaf (tree- >right)); } C E G F H Binary Tree Operations - PrintTree //To print a binary tree. .. Binary Tree Maximum number of nodes 1 2 4 Consider the levels of a binary tree: level 0, level 1, level 2, Maximum number of nodes on a level is 2level_id 3 5 Maximum number of nodes in a binary tree is 2depth_of _tree+ 1 - 1 Full Binary Tree: Example: No of nodes = 2depth_of _tree + 1-1 1 Depth of tree = 3 2 4 8 5 9 No of nodes = 2depth_of _tree + 1-1 = _ 3 10 6 11 12 7 13 14 15 Properties of Binary Tree. .. 2 disjoint binary trees called the left and right subtrees of the root Examples of Binary tree: Binary Tree Comparison: Tree Binary tree • A tree must have at least 1 node • A binary tree may be empty • Each node has 0, 1, 2, or many subtrees • Each node has 0, 1, or 2 subtrees • We don’t distinguish subtrees according to their orders • We distinguish between the left and right subtree For example,... T=maketree(seq[0]); for(i=1;iinfo!=seq[i]) the same value { if (seq[i] < p->info) if (p->left==NULL) At each node, { setleft(p,seq[i]); If input no < current node bNewNodeCreated=TRUE; (we’ll look at the left sub -tree) break; } If the left sub -tree. .. is duplicated!) Method 2 - Use a special binary tree (Binary Search Tree) , T: • Read number by number • Each time compare the number with the contents of T • If it is found duplicated, then output, otherwise add it to T Array Representation of Binary Tree Using Binary Search Tree to Find All Duplicates in a List of Numbers Method 2 - Use a special binary tree (Binary Search Tree) , T: • Read number... Binary Tree Operations - depth Level of node : The level of root(T) is zero The level of any other node is one higher than that node's level with respect to the subtree of root(T) containing it Depth of a tree: The maximum level of any leaf in the tree A B D C E G F H Example: Depth of a NULL binary tree is 0 Depth of a tree with 1 node is 0 Depth = 1 Depth = 2 Binary Tree Operations - depth //To determine . represented node of i (if any), is at i-1. Array Representation of Binary Tree A B C D E 0 1 3 7 15 A B - C - - - D - - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - - - - - E Unused array elements (not exist. binary tree is 2 depth_of _tree+ 1 - 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Depth of tree = 3 No. of nodes = 2 depth_of _tree +1 -1 = _____ 1 2 3 4 5 Properties of Binary Tree Complete Binary Tree: … • The. right subtrees of the root. Examples of Binary tree: Binary Tree Comparison: 1 2 3 4 5 1 23 54 Tree Binary tree • Each node has 0, 1, or 2 subtrees. • Each node has 0, 1, 2, or many subtrees. • We

Ngày đăng: 18/07/2014, 19:00

Xem thêm: cấu trúc dữ liệu và giải thuật - Tree

TỪ KHÓA LIÊN QUAN

Mục lục

    DATA STRUCTURE AND ALGORITHMS – DSA315 Spring 2010

    How We View a Tree

    Properties of Binary Tree

    Array Representation of Binary Tree

    Array Representation of Binary Tree Using Binary Search Tree to Find All Duplicates in a List of Numbers

    Array Representation of Binary Tree Using Binary Search Tree to Find All Duplicates in a List of Numbers

    Link Representation of Binary Tree

    Link Representation of Binary Tree Using Binary Search Tree to Find All Duplicates in a List of Numbers

    Link Representation of Binary Tree Using Binary Search Tree to Find All Duplicates in a List of Numbers

    Binary Tree Operations - depth

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w