Tree
Hanchen Wang • 09.09.2018
1
Structures and Algorithms
Outline
Introduction● Definition & Properties
● Basic Operations
Problem Sets● Traversing Tree● Balancing Binary Tree● Querying Binary Tree
Advanced Structures● Red-Black Tree● Balanced Search Tree● van Emde Boas Tree
2
Introduction
3
Math Definition(Free) Tree; Rooted and Ordered Tree; Binary and Positional Tree;
Practical InstanceSearch;Minimum/Maximum;Successor/PredecessorInsert/Delete
Introduction - Definition & Property - (Free) Tree
(Free) Tree -> Connected, Acyclic, Undirected Graph
(a) Tree (b) Forest (c) Neither
● G = (V, E)● Any two vertices in G are connected by a unique simple path● G is connected, but if any edge is removed from E, the resulting graph is disconnected● G is connected, and |E| = |V| - 1● G is acyclic, and |E| = |V| - 1● G is acyclic, but if any edge is added to E, the resulting graph contains a cycle
4
Introduction - Definition & Property - R* Tree
Rooted Tree -> a free tree in which one of the vertices is distinguished from the others
● Terms: Root/Leaf; Ancestor/Descendent; Parent/Child; Degree/Depth/Height
Ordered Tree -> a rooted tree in which the children of each node are ordered.
5
Introduction - Definition & Property - B* Tree
Binary Tree -> A Structure that defined on a finite set of nodes that:● either contains no nodes,● or is composed of three disjoint sets of nodes: a root node, a binary tree called
its left subtree, and a binary tree called its right subtree.
● Terms: empty(null)/full tree; NIL
Positional Tree -> Extended version of Binary Tree, with more than two children per nodes, with index
● Terms: k-ary tree, complete k-ary tree6
Introduction - Basic Operations (of Binary Search Tree)
Binary Search Tree
● A Binary Tree● Each node contains attributes key, left, right, and p that point to the nodes corresponding to
its stored data, left child, right child, and parent, respectively● For any node X, If Y is a node in the left subtree of X, then y.key < x.key; If Y is a node in the
right subtree of x, then Y.key > X.key
Dynamic-Set Operations
● Search, Minimum, Maximum, Predecessor, Successor, Insert and Delete● Time Complexity -> O(log2 n)● Recursive Method: Inorder/Preorder/Postorder
7
Introduction - Basic Operations (of Binary Search Tree)
Ergodic Method
● Inorder: Left Subtree -> Root -> Right Subtree● Preorder: Root -> Subtrees● Postorder: Subtrees -> Root
Tree Walk
8
How about Preorder/Postorder Tree Walk?
Introduction - Basic Operations (of Binary Search Tree)
Search
9
-Iteratively(better efficiency)-Recursively
Introduction - Basic Operations (of Binary Search Tree)
Minimum
10
Maximum
Introduction - Basic Operations (of Binary Search Tree)
11
Successor
How about Predecessor?
Introduction - Basic Operations (of Binary Search Tree)
12
Insert
Introduction - Basic Operations (of Binary Search Tree)
13
Delete
Introduction - Basic Operations (of Binary Search Tree)
14
Delete
Introduction - *Randomly Built Binary Search Tree
15
● Each of the basic operations on a binary search tree runs in O(h) time, where h is the height of the tree
● Height changes with insertions and deletions● h >=⎣log2n⎦
● randomly built binary search tree on n keys means inserting the keys in random order into an initially empty tree
● It can be proved that the expected height of a such with n distinct key values is O(log2 n).
Problem Sets
16
DefinitionTraversing TreeBalancing Binary TreeQuerying Binary Tree
Problem Sets - 104. Maximum Depth of Binary Tree
17
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Problem Sets - 111. Minimum Depth of Binary Tree
18
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Problem Sets - 94. Binary Tree Inorder Traversal
19
Given a binary tree, return the inorder traversal of its nodes' values.
Problem Sets -144. Binary Tree Preorder Traversal
20
Given a binary tree, return the preorder traversal of its nodes' values.
Problem Sets -145. Binary Tree Postorder Traversal
21
Given a binary tree, return the postorder traversal of its nodes' values.
Problem Sets -102. Binary Tree Level Order Traversal
22
Given a binary tree, return the level order traversal of its nodes' values.
Problem Sets - 889. Construct Binary Tree from Preorder and Postorder Traversal
23
Given preorder and postorder traversal of a tree, construct the binary tree.
Problem Sets - 889. Construct Binary Tree from Preorder and Postorder Traversal
24
Given preorder and postorder traversal of a tree, construct the binary tree.
Problem Sets - 889. Construct Binary Tree from Inorder and Postorder Traversal
25
Given preorder and postorder traversal of a tree, construct the binary tree.
Problem Sets - 105. Construct Binary Tree from Preorder and Inorder Traversal
26
Given preorder and inorder traversal of a tree, construct the binary tree.
Problem Sets - 110. Balanced Binary Tree
27
Given a binary tree, determine if it is height-balanced
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Problem Sets - 109. Convert Sorted List to Binary Search Tree
28
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Problem Sets - 109. Convert Sorted List to Binary Search Tree
29
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Problem Sets - 114. Flatten Binary Tree to Linked List
30
Given a binary tree, flatten it to a linked list in-place.
Problem Sets - 114. Flatten Binary Tree to Linked List
31
Given a binary tree, flatten it to a linked list in-place.
Problem Sets - 257. Binary Tree Path
32
Given a binary tree, return all root-to-leaf paths.
Problem Sets - 257. Binary Tree Path
33
Given a binary tree, return all root-to-leaf paths.
Advanced Structures -to bring more balance
34
● Red-Black Tree● Balanced Search Tree● van Emde Boas Tree
Advanced Structures - Red-Black Tree
Red-Black Tree -> A Binary Tree(each node with 1 more attribute, color) that satisfies red-black properties:
● Every node is either red or black● The root is black● Every leaf(NIL) is black● If a node is red, the both its children are black● For each node, all simple paths from the node to descendant leaves contain
the same number of black nodes
35
Advanced Structures - Red-Black Tree
Balanced Property -> A red-black tree with n internal nodes has height at most 2 lg nDynamic Operation -> Rotation, Insertion, Deletion
36
Advanced Structures - Balanced Search Tree
Balanced Search Tree -> better at minimizing disk I/O operations, it is a rooted tree satisfied several specific properties(pp.488-489, Introduction to Algorithms, 3rd Ed).Balanced Property -> the height h of a n-key, t-degree B-tree satisfies:
H <= logt (n+1)/2 (n>=1, t>=2)Dynamic Operation
37
References
1“Introduction to Algorithms” by Thomas H. Cormen et al.
“Data Structures & Algorithms in Python” by Michael T. Goodrich et al.
“Data Structures and Algorithms with Python” by Kent D. Lee et al.
38
Thanks
39