0% found this document useful (0 votes)
19 views

Trees in Discrete Structure

Uploaded by

khan.muneebc1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Trees in Discrete Structure

Uploaded by

khan.muneebc1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to Trees

Emerson University Lecturer: Mr Saqlain Ibrahim AI 1st Evening 1


What is a Tree?

Tree - a directed, acyclic structure of linked nodes


□ Directed - one-way links between nodes (no cycles)
□ Acyclic - no path wraps back around to the same node twice (typically
represents hierarchical data)

CS 5002: Discrete Math ©Northeastern University Fall 2018 2


Tree Terminology: Nodes

□ Tree - a directed, acyclic structure of linked nodes


□ Node - an object containing a data value and links to other nodes
□ All the blue circles

CS 5002: Discrete Math ©Northeastern University Fall 2018 3


Tree Terminology: Edges

□ Tree - a directed, acyclic structure of linked nodes


□ Edge - directed link, representing relationships between nodes
□ All the grey lines

CS 5002: Discrete Math ©Northeastern University Fall 2018 4


Root Node

□ Tree - a directed, acyclic structure of linked nodes


□ Root - the start of the tree tree)
□ The top-most node in the tree
□ Node without parents

CS 5002: Discrete Math ©Northeastern University Fall 2018 5


Parent Nodes

□ Tree - a directed, acyclic structure of linked nodes


□ Parent (ancestor) - any node with at least one child
□ The blue nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 6


Children Nodes

□ Tree - a directed, acyclic structure of linked nodes


□ Child (descendant) - any node with a parent
□ The blue nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 7


Sibling Nodes

□ Tree - a directed, acyclic structure of linked nodes


□ Siblings - all nodes on the same level
□ The blue nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 8


Internal Nodes

□ Tree - a directed, acyclic structure of linked nodes


□ Internal node - a node with at least one children (except root)
□ All the orange nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 9


Leaf (External) Nodes

□ Tree - a directed, acyclic structure of linked nodes


□ External node - a node without children
□ All the orange nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 10


Tree Path

□ Tree - a directed, acyclic structure of linked nodes


□ Path - a sequence of edges that connects two nodes
□ All the orange nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 11


Node Level

□ Node level - 1 + [the number of connections between the node and


the root]
□ The level of node 1 is 1
□ The level of node 11 is 4

CS 5002: Discrete Math ©Northeastern University Fall 2018 12


Node Height

□ Node height - the length of the longest path from the node to some
leaf
□ The height of any leaf node is 0
□ The height of node 8 is 1
□ The height of node 1 is 3
□ The height of node 11 is 0

CS 5002: Discrete Math ©Northeastern University Fall 2018 13


Tree Height

Tree height
□ The height of the root of the tree, or
□ The number of levels of a tree -1.
□ The height of the given tree is 3.

CS 5002: Discrete Math ©Northeastern University Fall 2018 14


What is Not a Tree?
Problems:
□ Cycles: the only node has a cycle
□ No root: the only node has a parent (itself, because of the cycle), so
there is no root

CS 5002: Discrete Math ©Northeastern University Fall 2018 31


What is Not a Tree?

Problems:
□ Cycles: there is a cycle in the tree
□ Multiple parents: node 3 has multiple parents on different levels

CS 5002: Discrete Math ©Northeastern University Fall 2018 32


What is Not a Tree?
Problems:
□ Cycles: there is an undirected cycle in the tree
□ Multiple parents: node 5 has multiple parents on different levels

CS 5002: Discrete Math ©Northeastern University Fall 2018 33


What is Not a Tree?
Problems:
□ Disconnected components: there are two unconnected groups of
nodes

CS 5002: Discrete Math ©Northeastern University Fall 2018 34


Summary: What is a Tree?

□ A tree is a set of nodes, and that set can be empty


□ If the tree is not empty, there exists a special node called a root
□ The root can have multiple children, each of which can be the root of a
subtree

CS 5002: Discrete Math ©Northeastern University Fall 2018 35


Binary Trees

Binary tree - a tree where every node has at most two children

CS 5002: Discrete Math ©Northeastern University Fall 2018 36


Binary Search Trees

□ Binary search tree (BST) - a tree where nodes are organized in a


sorted order to make it easier to search
□ At every node, you are guaranteed:

□ All nodes rooted at the left child are smaller than the current node
value
□ All nodes rooted at the right child are smaller than the current node
value

CS 5002: Discrete Math ©Northeastern University Fall 2018 37


Example: Binary Search Trees?
Binary search tree (BST) - a tree where nodes are organized in a sorted
order to make it easier to search

□ Left tree is a BST


□ Right tree is not a BST - node 7 is on the left hand-side of the root
node, and yet it is greater than it
CS 5002: Discrete Math ©Northeastern University Fall 2018 38
CS 5002: Discrete Math ©Northeastern University Fall 2018 39

You might also like