Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

zinchse/py-avl-tree-visualizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

18 Commits

Repository files navigation

AVL Tree implementation with pretty visualization and built-in search for k-th element

To my surprise, I couldn't find on the Internet any implementation of AVLTree with a nice tree visualization. So I've decided to do it (and support of k-th statistics) myself. I used as a base the following implementation. Also for a complete understanding of all rotations and other operations, I advise reader to become familiar with the following article.

πŸ“¦ Installation

git clone https://github.com/zinchse/AVLTree
cd AVLTree
pip install sortedcontainers
python3 test.py

🧩 Interface

1. Basics

>>> from AVLTree import AVLTree
>>> tree = AVLTree([1,2,3,5,6])
>>> print(tree)
 2 
 / \ 
 / \ 
 / \ 
 1 5 
 / \ 
 3 6 
>>> tree.insert(4)
>>> print(tree)
 3 
 / \ 
 / \ 
 / \ 
 2 5 
 / / \ 
 1 4 6 
>>> tree.remove(3)
>>> print(tree)
 4 
 / \ 
 / \ 
 / \ 
 2 5 
 / \ 
 1 6 

2. Search

>>> print(tree)
 4 
 / \ 
 / \ 
 / \ 
 2 5 
 / \ 
 1 6 
>>> print(tree.findkth(2), type(tree.findkth(2)))
2 <class 'Node.Node'>
>>> print(tree.find(4), type(tree.findkth(4)))
4 <class 'Node.Node'>
>>> print(tree.find(-1), type(tree.find(-1)))
None <class 'NoneType'> 

3. Traversal

>>> print(tree)
 4 
 / \ 
 / \ 
 / \ 
 2 5 
 / \ 
 1 6 
>>> # traversal_type: 0=preorder, 1=inorder, 2=postorder
>>> print(tree.as_list(traversal_type=0))
[4, 2, 1, 5, 6]
>>> print(tree.as_list(traversal_type=1))
[1, 2, 4, 5, 6]
>>> print(tree.as_list(traversal_type=2))
[1, 2, 6, 5, 4]

πŸ—‚οΈ File structure

+-- Node.py/ - defines the Node class (key, child/parent pointers, height & subtree size)
+-- AVLTree.py/ - implements the AVLTree API (insert, delete, search, k-th element, pretty-print)
+-- test.py/ - basic verification of k-th-smallest lookup and logarithmic height maintenance

About

AVL Tree implementation with pretty print and built-in search for k-th element

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /