1. Trang chủ
  2. » Tất cả

Cấu trúc dữ liệu và thuật toán dsa ch6 trees

62 3 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

Nội dung

Trees Luong The Nhan, Tran Giang Son Chapter Trees Basic Tree Concepts Data Structures and Algorithms Binary Trees Expression Trees Binary Search Trees Luong The Nhan, Tran Giang Son Faculty of Computer Science and Engineering University of Technology, VNU-HCM 6.1 Contents Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Basic Tree Concepts Binary Trees Binary Trees Expression Trees Expression Trees Binary Search Trees Binary Search Trees 6.2 Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.3 Trees Basic Tree Concepts Luong The Nhan, Tran Giang Son 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 a Binary Trees Expression Trees Binary Search Trees c b e f d g h i 6.4 Trees Basic Tree Concepts • Degree of a node (Bậc nút): the number of Luong The Nhan, Tran Giang Son 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 Basic Tree Concepts Binary Trees For the node d: a • Degree = Expression Trees Binary Search Trees • Indegree branches: ad → indegree = c b d • Outdegree branches: dg, dh, di → outdegree = e f g h i 6.5 Trees Basic Tree Concepts Luong The Nhan, Tran Giang Son • 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 Basic Tree Concepts a root Binary Trees Expression Trees Binary Search Trees c b e f d g h i 6.6 Basic Tree Concepts Terms Trees Luong The Nhan, Tran Giang Son • 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 Basic Tree Concepts • A parent (nút cha) has an outdegree greater than zero Binary Trees • A child (nút con) has an indegree of one Expression Trees → a internal node is both a parent of a node and a child of another one Binary Search Trees • 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 6.7 Basic Tree Concepts Trees Luong The Nhan, Tran Giang Son Terms • A path (đường đi) is a sequence of nodes in which each node is adjacent to the next one • The level (bậc) of a node is its distance from the root → Siblings are always at the same level • The height (độ cao) of a tree is the level of the leaf in Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees the longest path from the root plus • A subtree (cây con) is any connected structure below the root 6.8 Trees Basic Tree Concepts Luong The Nhan, Tran Giang Son a Level Branch ad Level c b d Basic Tree Concepts Branch di Binary Trees Expression Trees Level e f g h i • Parents: a, b, d • Internal nodes: b, d • Children: • Siblings: b, c, d, e, f, g, h, i • Leaves: c, e, f, g, h, i Binary Search Trees {b, c, d}, {e, f }, {g, h, i} • Height = 6.9 Trees Basic Tree Concepts Luong The Nhan, Tran Giang Son a Subtree b Subtree d Basic Tree Concepts c b Binary Trees d Expression Trees Binary Search Trees e f g h i 6.10 Binary Search Trees Luong The Nhan, Tran Giang Son Iterative Search while (root is not NULL) AND (root->data.key target) if target < root->data.key then root = root->left else root = root->right end end return root End iterativeSearchBST Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.48 Insert Node into BST Trees Luong The Nhan, Tran Giang Son 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) 6.49 Insert Node into BST: Iterative Insert Trees Luong The Nhan, Tran Giang Son Algorithm iterativeInsertBST(ref root , val new ) Insert node containing new data into BST using iteration Basic Tree Concepts Binary Trees Expression Trees Pre: root is address of first node in a BST new is address of node containing data to be inserted Binary Search Trees Post: new node inserted into the tree 6.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 pWalk = pWalk->right end end if new->data.key < parent->data.key then parent->left = new else parent->right = new end end End iterativeInsertBST Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.51 Insert Node into BST: Recursive Insert Trees Luong The Nhan, Tran Giang Son Algorithm recursiveInsertBST(ref root , val new ) Insert node containing new data into BST using recursion Basic Tree Concepts Binary Trees Expression Trees Pre: root is address of current node in a BST new is address of node containing data to be inserted Binary Search Trees Post: new node inserted into the tree 6.52 Insert Node into BST: Recursive Insert Trees Luong The Nhan, Tran Giang Son 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 6.53 Delete node from BST Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Deletion of a leaf: Set the deleted node’s parent link to NULL 6.54 Delete node from BST Trees Luong The Nhan, Tran Giang Son 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 6.55 Delete node from BST Trees Luong The Nhan, Tran Giang Son Deletion of a node having both subtrees: Replace the deleted node by its predecessor or by its successor, recycle this node instead Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.56 Delete node from BST Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.57 Delete node from BST Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.58 Delete node from BST 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 Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees Post: node deleted and memory recycled if dltKey not found, root unchanged Return true if node deleted, false if not found 6.59 Delete node from BST Trees Luong The Nhan, Tran Giang Son 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 6.60 Delete node from BST Trees Luong The Nhan, Tran Giang Son 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 6.61 Delete node from BST Trees Luong The Nhan, Tran Giang Son 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 6.62 ... Binary Trees Expression Trees Binary Search Trees 6.12 Trees Luong The Nhan, Tran Giang Son Binary Trees Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.13 Trees Binary Trees. .. Binary Trees Expression Trees Binary Search Trees 6.30 Trees Luong The Nhan, Tran Giang Son Expression Trees Basic Tree Concepts Binary Trees Expression Trees Binary Search Trees 6.31 Expression Trees. ..Contents Trees Luong The Nhan, Tran Giang Son Basic Tree Concepts Basic Tree Concepts Binary Trees Binary Trees Expression Trees Expression Trees Binary Search Trees Binary Search Trees 6.2 Trees

Ngày đăng: 25/03/2023, 07:21

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

TÀI LIỆU LIÊN QUAN

w