如何在python中实现树排序算法?在二叉搜索树中

时间:2018-05-14 22:49:45

标签: python binary-search-tree

我尝试在python中实现树排序算法。 这是我目前所做的工作。 我不太确定树排序算法是什么以及如何实现它。 任何帮助将不胜感激。 谢谢

add_filter( 'woocommerce_output_related_products_args', function( $args ) { 
    $args = wp_parse_args( array(  "post__not_in" => array('502','281') ), $args );
    return $args;
});

1 个答案:

答案 0 :(得分:0)

您的代码已关闭,但<template> <div> <v-layout class="mt-5"> <v-flex md8> <h2 class="text-md-left ml-2">Place related to {{tagName}}</h2> </v-flex> <v-spacer></v-spacer> <v-flex md4> <h2 class="text-md-right"> <v-btn icon v-show="true" @click="nextLeft"> <v-icon>arrow_left</v-icon> </v-btn> <v-btn icon v-show="true" @click="nextRight"> <v-icon>arrow_right</v-icon> </v-btn> </h2> </v-flex> </v-layout> <v-layout fluid> <transition-group name="list-slide"> <v-flex xs12 md4 v-for="place in places" :key="place.id" class="list-slide-item" tag="div"> <v-card class="mr-2 ml-2 mt-2"> <v-card-media :src="place.picture" height="180px"> </v-card-media> <v-card-title primary-title> <div> <h4 class="headline mb-0">{{ place.name }}</h4> <div> {{ place.description }}</div> </div> </v-card-title> <v-card-actions> <v-btn flat color="teal">Share</v-btn> <v-btn flat color="teal">Explore</v-btn> </v-card-actions> </v-card> </v-flex> </transition-group> </v-layout> </div> </template> <script> export default { props: ['tagName'], data () { return { hasLeft: false, // True if there are some elements that are already been displayed hasRight: true, // True if there are some elements to display in right pageNumber: 1, // The page number on dataset we are currently on bufferEnded: false, // While bufferEnded is True and fetching the API still returns results places: [ { name: 'Luna park obala', id: 1, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ-yl-Ve7691Xp1ydOs8uxP78wt3xvfsJEVJt4vMe8FZMZHnUt6KQ' }, { name: 'Luna park obala', id: 2, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT88qzjKE_X8nS97t-K4z10h6iOSBFjB4YRB_U2DtloUTtoaYpAtA' }, { name: 'Luna park obala', id: 3, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLFZUJQ62W_jdyuwpd9A7nqLlIgVgebgHxdaDDL9MF-ih9_p7L' } ], bufferLeft: [], bufferRight: [ { name: 'Luna park obala', id: 4, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ-yl-Ve7691Xp1ydOs8uxP78wt3xvfsJEVJt4vMe8FZMZHnUt6KQ' }, { name: 'Luna park obala', id: 5, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT88qzjKE_X8nS97t-K4z10h6iOSBFjB4YRB_U2DtloUTtoaYpAtA' }, { name: 'Luna park obala', id: 6, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLFZUJQ62W_jdyuwpd9A7nqLlIgVgebgHxdaDDL9MF-ih9_p7L' }, { name: 'Luna park obala', id: 7, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLFZUJQ62W_jdyuwpd9A7nqLlIgVgebgHxdaDDL9MF-ih9_p7L' }, { name: 'Luna park obala', id: 8, description: "Luna park dans la ville d'obala", picture: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTLFZUJQ62W_jdyuwpd9A7nqLlIgVgebgHxdaDDL9MF-ih9_p7L' } ] } }, methods: { nextRight: function () { }, nextLeft: function () { var previousFirstPlace = this.places.shift() this.bufferLeft.push(previousFirstPlace) var newLastPlace = this.bufferRight.shift() this.places.push(newLastPlace) } } } </script> <style> .list-slide-item { transition: all 1s; display: inline-block; } .list-slide-enter, .list-slide-leave-to /* .list-complete-leave-active below version 2.1.8 */ { opacity: 0; transform: translateX(-30px); } .list-slide-leave-active { position: absolute; } </style> 需要作为tree_insertleft成员变量的属性调用:

right

输出:

class SortTree:
  def __init__(self, value):
    self.left = None
    self.value = value
    self.right = None
  def insert_val(self, _value):
    if _value < self.value:
       if self.left is None:
           self.left = SortTree(_value)
       else:
           self.left.insert_val(_value)
    else:
       if self.right is None:
          self.right = SortTree(_value)
       else:
          self.right.insert_val(_value)
  @classmethod
  def display(cls, _node):
     return list(filter(None, [i for b in [cls.display(_node.left) if isinstance(_node.left, SortTree) else [getattr(_node.left, 'value', None)], [_node.value], cls.display(_node.right) if isinstance(_node.right, SortTree) else [getattr(_node.right, 'value', None)]] for i in b]))


tree = SortTree(4)
for i in [5, 3, 1, 2, 8, 7, 4]:
  tree.insert_val(i)

print(SortTree.display(tree))

修改:不含[1, 2, 3, 4, 4, 5, 7, 8]

classmethod

输出:

class SortTree:
  def __init__(self, value):
    self.left = None
    self.value = value
    self.right = None
  def insert_val(self, _value):
    if _value < self.value:
       if self.left is None:
         self.left = SortTree(_value)
       else:
         self.left.insert_val(_value)
    else:
       if self.right is None:
         self.right = SortTree(_value)
       else:
         self.right.insert_val(_value)

def display(_node):
   return list(filter(None, [i for b in [display(_node.left) if isinstance(_node.left, SortTree) else [getattr(_node.left, 'value', None)], [_node.value], display(_node.right) if isinstance(_node.right, SortTree) else [getattr(_node.right, 'value', None)]] for i in b]))

tree = SortTree(4)
for i in [5, 3, 1, 2, 8, 7, 4]:
  tree.insert_val(i)

print(display(tree))