Python binary search tree

Source: Internet
Author: User
This is a binary tree, a simple tree structure. I also don't need to talk about using python. let's go to the code. #-*-Coding: cp936-*-# --------------------------------------------- # authorchile...
#-*-Coding: cp936-*-# --------------------------------------------- # author chile # version 1.0 # date 2014-02-17 # desc binary tree #### parse class bintree: def _ init _ (self): self. root = None # Pioneer node def treePredecessor (self, entry): if entry. left! = None: return self. maxTree (entry. left) preNode = entry. parent tempNode = entry while preNode! = None and preNode. right. value! = Entry. value: tempNode = preNode. parent return preNode # successor node def treeSuccessor (self, entry): if entry. right! = None: return self. minTree (entry. right) preNode = entry. parent tempNode = entry while preNode! = None and preNode. left. value! = Entry. value: tempNode = preNode. parent return preNode def add (self, value): tempNode = self. root parentNode = None entry = bintree. entry (value = value) while tempNode! = None: parentNode = tempNode if cmp (value, parentNode. value) <0: tempNode = tempNode. left else: tempNode = tempNode. right if parentNode = None: self. root = entry elif cmp (value, parentNode. value) <0: parentNode. left = entry. parent = parentNode else: parentNode. right = entry. parent = parentNode # Delete all nodes (including all sub-nodes) def remove (self, value): root = self. root parentNode = None if value = Root. value: root. left = None root. right = None while root! = None: parentNode = root. parent if value = root. value: root. left = None root. right = None if parentNode. left! = None and parentNode. left. value = value: parentNode. left = None break else: parentNode. right = None break elif cmp (value, root. value) <0: root = root. left else: root = root. right # search node def search (self, value): root = self. root while root! = None and root. value! = Value: if cmp (value, root. value) <0: root = root. left else: root = root. right return root # The minimum value of the node, in fact, is to find the left leaf node def minTree (self, root): entry = root while entry. left! = None: entry = entry. left return entry # maximum node def maxTree (self, root): entry = root while entry. right! = None: entry = entry. right return entry # traverse def iterator (self, root) in the middle order: if root! = None: self. iterator (root. left) print root. value self. iterator (root. right) class entry: def _ init _ (self, value = None): self. left = None self. right = None self. parent = None self. value = value arr = [15, 5, 3, 12, 10, 13, 6, 7, 16, 20, 18, 23] tree = bintree () for val in arr: tree. add (val) tree. iterator (tree. root) node = tree. search (16) print node. left, node. right, node. parent. valueprint tree. maxTree (node ). valueprint tree. treePredecessor (node ). valueprint tree. treeSuccessor (node ). value # print tree. maxTree (), tree. minTree ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.