Chap5 treeconcept 123

63 14 0
Chap5 treeconcept 123

Đ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

Đối với người học lập trình nói chung, cấu trúc dữ liệu và giải thuật là một trong những môn quan trọng và thường được dạy vào khoảng năm 2 và năm 3 đại học. Cảm giác của rất nhiều bạn nếu chưa tự tin là dễ bị nản ngay từ giai đoạn đầu và dần dần sẽ khó khăn hơn để bắt nhịp. Đồng thời, học tốt cấu trúc dữ liệu và giải thuật sẽ giúp cho các dòng code của mình trở nên tối ưu hơn. Trong bài viết này, mình sẽ tổng hợp các kiến thức cơ bản cùng các kinh nghiệm của mình để giúp các bạn đi đúng hướng và cảm thấy sự thú vị của môn học này. Tất nhiên xung quanh ta vẫn có rất nhiều cao thủ, việc giới thiệu các kiến thức khó sẽ khiến mọi người bị ngợp nên trong phạm vi bài viết này, mình sẽ giới thiệu các vấn đề cơ bản (ít nhất là trong các bài kiểm tra trên trường). Hãy cùng tham khảo bài viết dưới đây:

Tree concepts Dept Computer Science Tree concepts and Binary Tree Basic Tree Concepts Binary Trees Expression Trees Data Structures and Algorithms Binary Search Trees Dept Computer Science Faculty of Computer Science and Engineering Ho Chi Minh University of Technology, VNU-HCM Tree concepts.1 Overview Tree concepts Dept Computer Science Basic Tree Concepts Basic Tree Concepts Binary Trees Binary Trees Expression Trees Binary Search Trees Expression Trees Binary Search Trees Tree concepts.2 Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Basic Tree Concepts Expression Trees Binary Search Trees Tree concepts.3 Tree concepts Basic Tree Concepts Dept Computer Science Definition A tree (cây) consists of a finite set of elements, called nodes (nút), and a finite set of directed lines, called branches (nhánh), that connect the nodes Basic Tree Concepts Binary Trees Expression Trees a c b e Binary Search Trees f d g h i Tree concepts.4 Tree concepts Basic Tree Concepts • Degree of a node (Bậc nút): the number of branches associated with the node • Indegree branch (Nhánh vào): directed branch toward the node • Outdegree branch (Nhánh ra): directed branch away from the node Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees For the node d: • Degree = a c b e f • Indegree branches: ad → indegree = • Outdegree branches: dg, dh, di → outdegree = d g h i Tree concepts.5 Tree concepts Basic Tree Concepts Dept Computer Science • The first node is called the root • indegree of the root = • Except the root, the indegree of a node = • outdegree of a node = or or more a e f Binary Trees Expression Trees root c b Basic Tree Concepts Binary Search Trees d g h i Tree concepts.6 Basic Tree Concepts Terms • A root (nút gốc) is the first node with an indegree of zero • A leaf (nút lá) is any node with an outdegree of zero • A internal node (nút nội) is not a root or a leaf • A parent (nút cha) has an outdegree greater than zero • A child (nút con) has an indegree of one → a internal node is both a parent of a node and a child of another one • Siblings (nút anh em) are two or more nodes with the same parent • For a given node, an ancestor is any node in the path from the root to the node • For a given node, an descendent is any node in the paths from the node to a leaf Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.7 Basic Tree Concepts Tree concepts Dept Computer Science Terms • A path (đường đi) is a sequence of nodes in which each node is adjacent to the next one Basic Tree Concepts Binary Trees Expression Trees • The level (bậc) of a node is its distance from the root → Siblings are always at the same level Binary Search Trees • The height (độ cao) of a tree is the level of the leaf in the longest path from the root plus • A subtree (cây con) is any connected structure below the root Tree concepts.8 Tree concepts Basic Tree Concepts Dept Computer Science a Level Branch ad Level c b Basic Tree Concepts d Binary Trees Expression Trees Branch di Level e f • Parents: a, b, d • Children: b, c, d, e, f, g, h, i • Leaves: c, e, f, g, h, i g h Binary Search Trees i • Internal nodes: b, d • Siblings: {b, c, d}, {e, f }, {g, h, i} • Height = Tree concepts.9 Tree concepts Basic Tree Concepts Dept Computer Science a Subtree b Subtree d Basic Tree Concepts Binary Trees Expression Trees c b e f Binary Search Trees d g h i Tree concepts.10 Insert Node into BST Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees All BST insertions take place at a leaf or a leaflike node (a node that has only one null branch) Tree concepts.49 Insert Node into BST: Iterative Insert Tree concepts Dept Computer Science Algorithm iterativeInsertBST(ref root , val new ) Insert node containing new data into BST using iteration Pre: root is address of first node in a BST new is address of node containing data to be inserted Post: new node inserted into the tree Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.50 Insert Node into BST: Iterative Insert if root is null then root = new else pWalk = root while pWalk not null parent = pWalk if new->data.key < pWalk->data.key then pWalk = pWalk->left else 10 pWalk = pWalk->right 11 end 12 end 13 if new->data.key < parent->data.key then 14 parent->left = new 15 else 16 parent->right = new 17 end 18 end Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.51 Insert Node into BST: Recursive Insert Tree concepts Dept Computer Science Algorithm recursiveInsertBST(ref root , val new ) Insert node containing new data into BST using recursion Pre: root is address of current node in a BST new is address of node containing data to be inserted Post: new node inserted into the tree Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.52 Insert Node into BST: Recursive Insert Tree concepts Dept Computer Science 10 11 if root is null then root = new else if new->data.key < root->data.key then recursiveInsertBST(root->left, new) else recursiveInsertBST(root->right, new) end end Return End recursiveInsertBST Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.53 Delete node from BST Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Deletion of a leaf: Set the deleted node’s parent link to NULL Tree concepts.54 Delete node from BST Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Deletion of a node having only right subtree or left subtree: Attach the subtree to the deleted node’s parent Tree concepts.55 Delete node from BST Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Deletion of a node having both subtrees: Replace the deleted node by its predecessor or by its successor, recycle this node instead Expression Trees Binary Search Trees Tree concepts.56 Delete node from BST Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.57 Delete node from BST Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.58 Delete node from BST Tree concepts Dept Computer Science Algorithm deleteBST(ref root , val dltKey ) Deletes a node from a BST Pre: root is pointer to tree containing data to be deleted dltKey is key of node to be deleted Post: node deleted and memory recycled if dltKey not found, root unchanged Return true if node deleted, false if not found Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.59 Delete node from BST Tree concepts Dept Computer Science if root is null then return false end if dltKey < root->data.key then return deleteBST(root->left, dltKey) else if dltKey > root->data.key then return deleteBST(root->right, dltKey) Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.60 Delete node from BST Tree concepts Dept Computer Science 10 11 12 else // Deleted node found – Test for leaf node if root->left is null then dltPtr = root root = root->right recycle(dltPtr) return true else if root->right is null then dltPtr = root root = root->left recycle(dltPtr) return true Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Tree concepts.61 Delete node from BST Tree concepts Dept Computer Science 10 11 12 13 14 15 else // else // Deleted node is not a leaf // Find largest node on left subtree dltPtr = root->left while dltPtr->right not null dltPtr = dltPtr->right end // Node found Move data and delete leaf node root->data = dltPtr->data return deleteBST(root->left, dltPtr->data.key) end Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees end End deleteBST Tree concepts.62 Tree concepts Dept Computer Science Basic Tree Concepts Binary Trees THANK YOU Expression Trees Binary Search Trees Tree concepts.63

Ngày đăng: 14/04/2021, 21:36

Từ khóa liên quan

Mục lục

  • Basic Tree Concepts

  • Binary Trees

  • Expression Trees

  • Binary Search Trees

Tài liệu cùng người dùng

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

Tài liệu liên quan