Download presentation
1
Data Structures TREES
2
What is a tree? In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation Previous data structures placed data in linear order. Some data organizations require categorizing data into groups, subgroups
3
Trees are a hierarchical data structure of nodes
Tree Terminology Trees are a hierarchical data structure of nodes Nodes are linked by edges 4 nodes 2 5 edge 1 3
4
Programming environments
Applications Organization charts File systems Programming environments A class hierarchy in programming languages that support single inheritance (e.g. Java) Document Object Model (for HTML and XML Artificial Intelligence (Decision making etc)
5
tree is hierarchical classification
A tree is hierarchical classification. Data items appear at various levels within the organization E.g., directory structure:
6
tree is hierarchical classification
7
Decision Trees
8
Decision tree based upon expert system
9
Tree Terminology (Parent / Child)
Root: node without parent A parent node references one or more nodes (children nodes) that are “lower” in the tree hierarchy If node u is the parent of node v, then v is the child of u Except for the root (no parent), every node has exactly one parent (by definition) A tree has only one root node root u v
10
Tree Terminology (Siblings)
Two nodes that are children of the same parent.
11
Tree Terminology (Internal node)
A node is internal if it has one or more children
12
Tree Terminology (Leaf /External node)
A node is a leaf if it has no children
13
Tree Terminology (Ancestor / Descendent)
An ancestor of a node is either the node’s parent or any ancestor of the node’s parent (this is recursive) The root is an ancestor of each other node Descendant of a node: child, grandchild, grand-grandchild u v u v
14
Tree Terminology (Subtree)
A tree may be divided into subtrees. A subtree is a tree that has the child of a node as its root. Hence, a subtree is composed of a node and all of that node’s descendants. The first node in a subtree is known as the root of the subtree and is used to name the subtree. Subtrees themselves can be further divided into other subtrees.
15
Tree Terminology (Subtree)
The subtree of T rooted at node v is the tree consisting of all the descendents of v in T (including v) tree consisting of a node and its descendants Root of subtree v v
16
Tree Terminology (Depth of a node)
The depth of a node v in T is the number of ancestors of v, excluding v itself. More formally: If v is the root, the depth of v is 0 v v depth of v = 1 depth of v = 3
17
Tree Terminology( Height /depth of a tree)
The depth of a tree is the maximum depth of any of its leaves maximum levels of a tree tree depth = 3 tree depth = 2 tree depth = 0
18
Terminology … Height of the tree? Depth of node B?
19
Terminology Parents: A, B, F Leaves: C, D, E, G, H, I
Children: B, E, F, C, D, G, H, I Internal Nodes: A, B, F Siblings: {B, E, F}, {C, D}, {G, H, I}
20
Terminology Two nodes are adjacent if a branch connects them. A path is a sequence of nodes in which each node is adjacent to the next one. Every node in the tree can be reached by following a unique path starting from the root. The length of this path is the number of edges on the path. There is a path of length 0 from every node to itself.
21
Terminology The path from the root, A, to the leaf, I, is denoted as AFI and has a length of 2. ABD is the path from the root, A, to the leaf, D, and also has a length of 2.
22
Types of Trees General tree – a node can have any number of children
Binary tree – a node can have at most two children
23
Binary Tree
24
Binary Trees The simplest form of tree is a Binary Tree
A Binary Tree consists of (a) A node (called the root node) and (b) Left and right subtrees Both the subtrees are themselves binary trees Note: this is a recursive definition (A node can’t have more than 2 children) Binary tree General tree
25
Binary Trees Finite set of nodes that is either empty, or consists of a root and two disjoint binary trees called the left subtree and right subtree. Node contains information and two pointers to other nodes Each node has at most two children
26
A binary tree is either empty or has the following form:
Where Tleft and Tright are binary trees. root TL TR
27
Binary Trees Full binary tree: All leaves on the same level and every node has either zero or two children. Complete binary tree: Leaves are filled from left to right on one level before moving to next level.
28
Binary Trees
29
Binary Trees Skewed binary tree: Contains only left or right children. Similar: Two trees with same structure and different data. Copy or identical: Same structure and same data.
30
Tree Height and Full Binary Tree
If h = height of a binary tree, max # of leaves = 2h max # of nodes = 2h A binary tree with height h and 2h nodes (or 2h leaves) is called a full binary tree Binary tree
31
Visiting and Traversing a Node
Many applications require that all of the nodes of a tree be “visited”. Visiting a node may mean printing contents, retrieving information, making a calculation, etc. Traverse: To visit all the nodes in a tree in a systematic fashion. A traversal can pass through a node without visiting it at that moment.
32
Binary Tree Structure The representation of a binary tree structure is relatively straightforward. We need a variable to store the data at the node and 2 pointers to the left and right subtrees. struct Node { int data Node *left Node *right }
33
Binary Tree Structure
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.