Data Structures Lecture 8: Trees

EnchantingLead2872 avatar
EnchantingLead2872
·
·
Download

Start Quiz

Study Flashcards

9 Questions

What is the primary purpose of a postorder traversal in a file directory?

To compute the total space used by files in a directory and its subdirectories

In a binary tree, what is the order of visiting nodes in an inorder traversal?

Left-Root-Right

What is the purpose of the BinaryNode class?

To represent a node in a binary tree

What is the main difference between a preorder traversal and an inorder traversal?

The order of visiting nodes

What is the name of the algorithm used to perform a postorder traversal on a binary tree?

postOrder

What is the purpose of the insert function in a binary search tree?

To insert a new node into a binary search tree

What is the type of tree that is required for an inorder traversal?

Binary tree

What is the name of the function used to perform a preorder traversal on a binary tree?

preorder

What is the main advantage of using a binary search tree?

It allows for fast sorting

Study Notes

What is a Tree?

  • A tree is a collection of nodes with specific properties
  • A tree can be empty or consist of a root node and zero or more non-empty subtrees connected by directed edges
  • The root node is the parent of each subtree root, and each subtree root is a child of the root node
  • A tree with N nodes has N-1 edges

Preliminaries

  • A node can have multiple children, and a child node has only one parent
  • Leaf nodes have no children
  • Sibling nodes have the same parent node
  • A path is a sequence of nodes where each node is the parent of the next node
  • The length of a path is the number of edges in the path
  • The depth of a node is the length of the path from the root to the node
  • The height of a node is the length of the longest path from the node to a leaf node

Implementation of Trees

  • A tree can be implemented using a struct with pointers to the first child and next sibling nodes
  • Each node has an element and pointers to its first child and next sibling nodes

Binary Trees

  • A binary tree is a tree where each node has at most two children
  • The depth of a node can be as large as N-1 in the worst case
  • A binary tree consists of a root node and two subtrees, which can be empty

Tree Traversals

Postorder Traversal

  • A node is visited after its descendants
  • Application: compute space used by files in a directory and its subdirectories

Inorder Traversal

  • Left node is visited, then the Root node, and finally the right node (for binary trees)
  • Application: fast sorting on binary search trees

Binary Tree Traversals

  • There are three types of traversals: preorder, inorder, and postorder

The BinaryNode Class

  • A template class for binary nodes with an element, left child, and right child

Preorder, Inorder, and Postorder Traversal Functions

  • Each function has a specific order of visiting nodes in a tree

Learn about trees in data structures, including their properties and characteristics, and how they are structured with nodes and sub-trees.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Trees in Data Structures
12 questions
Tree Data Structure Quiz
10 questions

Tree Data Structure Quiz

AffirmativeMeteor avatar
AffirmativeMeteor
Basic Tree Data Structure Concepts
18 questions
Use Quizgecko on...
Browser
Browser