Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- BaseNode: Add diameter property.
- Misc: Testing to include benchmark timings for tree creation, compare benchmark tests across commits, reject pull request if benchmark tests fails.
- Misc: Documentation to include benchmark results.
### Changed
- Misc: Documentation to enable zooming in of images, add navigation section headers, remove some meta tags.
- Misc: Split up testing into multiple conftest files.

## [0.16.2] - 2024-02-06
### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Related Links:
- [Issues](https://github.com/kayjan/bigtree/issues)
/ [Discussions](https://github.com/kayjan/bigtree/discussions)
/ [Changelog](https://github.com/kayjan/bigtree/blob/master/CHANGELOG.md)
/ [Contributing](https://bigtree.readthedocs.io/en/latest/others/contributing.html)
/ [Contributing](https://bigtree.readthedocs.io/en/latest/contributing/)
- Package
- [PyPI](https://pypi.org/project/bigtree/)
/ [Conda](https://anaconda.org/conda-forge/bigtree)
Expand Down
37 changes: 35 additions & 2 deletions bigtree/node/basenode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import copy
import heapq
from typing import Any, Dict, Generator, Iterable, List, Optional, Set, Tuple, TypeVar

from bigtree.globals import ASSERTIONS
Expand Down Expand Up @@ -92,8 +93,9 @@ class BaseNode:
2. ``is_root``: Get indicator if self is root node
3. ``is_leaf``: Get indicator if self is leaf node
4. ``root``: Get root node of tree
5. ``depth``: Get depth of self
6. ``max_depth``: Get maximum depth from root to leaf node
5. ``diameter``: Get diameter of self
6. ``depth``: Get depth of self
7. ``max_depth``: Get maximum depth from root to leaf node

**BaseNode Methods**

Expand Down Expand Up @@ -502,6 +504,37 @@ def root(self: T) -> T:
return self
return self.parent.root

@property
def diameter(self) -> int:
"""Get diameter of tree or subtree, the length of longest path between any two nodes

Returns:
(int)
"""
diameter = 0

if self.is_leaf:
return diameter

def _recursive_diameter(node: T) -> int:
"""Recursively iterate through node and its children to get diameter of node

Args:
node (Node): current node

Returns:
(int)
"""
nonlocal diameter
if node.is_leaf:
return 1
child_length = [_recursive_diameter(child) for child in node.children]
diameter = max(diameter, sum(heapq.nlargest(2, child_length)))
return 1 + max(child_length)

_recursive_diameter(self)
return diameter

@property
def depth(self) -> int:
"""Get depth of self, indexing starts from 1
Expand Down
1 change: 1 addition & 0 deletions docs/demo/tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ Below are the tables of attributes available to `BaseNode` and `Node` classes.
|:------------------------------------:|--------------------|----------------------------|
| Check if root | `root.is_root` | True |
| Check if leaf node | `root.is_leaf` | False |
| Check diameter of tree | `node_b.diameter` | 3 |
| Check depth of node | `node_b.depth` | 2 |
| Check depth of tree | `node_b.max_depth` | 4 |
| Get root of tree | `node_b.root` | Node(/a, ) |
Expand Down
5 changes: 3 additions & 2 deletions docs/dev/bench/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@
const data = window.BENCHMARK_DATA;

// Render footer (repository)
const date = Date(data.lastUpdate).split(" ").slice(0, 4).join(" ");
document.getElementById('last-update').textContent = date;
const date = new Date(data.lastUpdate);
const date_str = date.toLocaleDateString("en-sg", {weekday:"short", day:"2-digit", month:"short", year:"numeric"});
document.getElementById('last-update').textContent = date_str;

const repoResultURL = 'https://kayjan.github.io/bigtree-benchmark/dev/bench/';
const repoResult = document.getElementById('repository-result');
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ theme:
name: Switch to dark mode
- media: "(prefers-color-scheme: dark)"
scheme: slate
primary: green
primary: teal
accent: yellow
toggle:
icon: material/weather-night
Expand Down
Loading